ASP.NET Nested FormView - asp.net

I have this HTML.
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1">
<asp:FormView ID="FormView2" runat="server" DefaultMode="Insert" DataSourceID="SqlDataSource2">
<asp:TextBox runat="Server" Text='<%# Eval("Terms") %>'></asp:TextBox>
</asp:FormView>
</asp:FormView>
The code above works without any error but I want to get terms in the textbox fetched from SqlDataSource1 of FormView1 instead of FormView2 (SqlDataSource2). What I am missing here?

You can access the value of Parent formView DataSource value in child formview as what you are currently doing. But there is another way you set value. like..
protected void ChildFormWiew_DataBound(object sender, EventArgs e)
{
if (ChildFormView.CurrentMode == FormViewMode.Edit)
{
TextBox txtTemrs = ParentFormView.FindControl("Terms") as TextBox;
((TextBox)ChildFormView.FindControl("Terms")).Text = txtTemrs.Text;
}
}

Related

Repeater inside a ListView: how to bind a command and call it ?

Scenario: I've a Repeater inside a ListView.
Inside the repeater i've a LinkButton; This is the syntax
<asp:LinkButton runat="server" ID="lnkRemoveComment" Text="Delete" Visible='<%# CheckVisibility() %>' CommandName="DeleteComment" CommandArgument='<%# Eval("id")%>'> </asp:LinkButton>
I don't know the syntax, in server side code, to call:
DeleteComment
CheckVisibility
I don't know because repeater is nested in an ListView.
And inserting code inside ListView ItemCommand not works!
Thanks
Example :
.aspx
------------------
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# Eval("erid") %>'>LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
.cs
------------------
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "MyUpdate"){
//e.CommandArgument --> contain the erid value
//Do something
}
}

Get index from selected Checkbox in DataGrid

I am using DataGrid in Asp.net
<asp:DataGrid>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkDetail" runat="server" OnCheckedChanged="chkDetail_CheckedChanged" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
As you can see, there is a Checkbox inside DataGrid.
Everytime I check in checkbox, I would like to fire 2 events
1. Other checkboxes will be unselected (I can do it with JS - so it's ok)
2. Context will store DataGrid Name field like this
protected void chkDetail_CheckedChanged(object sender, EventArgs e)
{
Context["Name"] = ??;
}
Because DataGrid doesn't have "Rows" like GridView, I have no idea how to get the Name from the same row.
Thanks in advance.
DataGridViewRow dgvrows = TrGrid.SelectedRows;
string c = dgvrows.Cells("Late_Time").value.toString();
OR
DataGridViewRow dgvrow = this.dataGridView1.Rows[index];
DataRowView drvrow = (DataRowView)dgvrow.DataBoundItem;
You can try this code
protected void chkDetail_CheckedChanged(object sender, EventArgs e)
{
Context["Name"] = (((sender as CheckBox).NamingContainer as DataGridItem).FindControl("label_id") as Label).Text;
}
Your sender will be a CheckBox as the event is triggered by it.
Every control will have a property called NamingContainer which gives the nearest server control that inherits INamingContainer interface. In your case it is DataGridItem. And you can use FindControl method to find child control and cast it to appropriate control type to access its properties.

Bind Data to DropDownList in GridView asp.net

I have web form with a GridView and a few controls in the GridView. I have a DropDownList in the EdtItemTemplate of the gridview.
I am needing to bind this DropDownList with some method in my CodeBehind File that returns a Array of type LisItems.
The problem I am facing is this. Since the Control is sitting in the EditItemTemplate, using the FindControl("MyControlID") does not seem to work in any of the GridView events, it returns null, in other words it cannot seem to find the control, unless I use the OnRowUpdating event, but I cannot use this event as the Control needs to be Data binded before that.
Is there anyway I can use the <%# Bind("MyMethodName") %> to bind the control?
Try this
Create a class of your data in App_Code, Like this
public static class Fruits
{
public static List<string> GetFruits()
{
return new string[] { "Apple", "Mango", "Banana", "Grapes" }.ToList();
}
}
Add a grid to your page, Which I guess you all ready have
<asp:GridView runat="server" ID="grid" AutoGenerateColumns="false" OnRowEditing="grid_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Selected Fruit">
<ItemTemplate>
<asp:Label runat="server" ID="Fruit" Text='<%# Eval("Fruits") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="fruits" DataSourceID="fruitsDS" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button runat="server" ID="btnEdit" Text="Edit" CommandName="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And add an Object Datasource to bind your DropDowns of edit template
<asp:ObjectDataSource ID="fruitsDS" runat="server" SelectMethod="GetFruits" TypeName="Fruits" />
Hope this could help.
Try this
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList dl = (DropDownList)e.Row.FindControl("myList");
dl.DataSource = new string[] { "A", "B" };
dl.DataBind();
}
}
gridview rowdatabound will use to bind the data to dropdown in gridview.
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlcity");
ddl.DataSource = s;

ASP.NET GridView DropDownList Posts Empty Value

I'm having trouble with a DropDownList within a GridView that posts a NULL value when in fact a value is selected from the list when using inline editing.
The problem is that I can't use this method to bind the value to the UpdateCommand in my SqlDataSource:
SelectedValue='<%# Bind("Value") %>'
The reason for this is because the value might not exist in the list so it throws an exception.
Is there a way I can bind the value to the UpdateCommand without using SelectedValue?
Thanks in advanced.
You may use RowDataBound to set SelectedValue;
in .cs file
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView grid = (GridView)sender;
DropDownList DropDownList1 = (e.Row.FindControl("DropDownList1") as DropDownList);
HiddenField HiddenField1 = (e.Row.FindControl("HiddenField1") as HiddenField);
DropDownList1.SelectedValue = HiddenField1.Value;
}
in .aspx file;
<Columns>
...
<asp:TemplateField HeaderText="Column Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Value") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("Value") %>'></asp:HiddenField>
<asp:DropDownList ID="DropDownList1" runat="server">
...
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>

Nested ListView control

i m having the following hierarchy in my aspx page
<asp:ListView ID="ListView1" DataSourceID="SqlDataSource1" runat="server">
<ItemTemplate>
...
<asp:ListView ID="ListView2" DataKeyNames="statusID" runat="server"
DataSourceID="SqlDataSource2"
ItemPlaceholderID="pl"
OnItemCommand="ListView2_ItemCommand">
<LayoutTemplate>
<asp:PlaceHolder ID="pl" runat="server"/>
...
<asp:Button ID="Button2" runat="server" Text="Post"/>
</LayoutTemplate>
<ItemTemplate>
...
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
now if i click Button2 then ListView2_ItemCommand event is fired.
protected void ListView2_ItemCommand(object sender, ListViewCommandEventArgs e)
inside the handler e.item is null, why?
there should be ItemTemplate with CommandArgument
E.g.
<ItemTemplate>
<asp:ImageButton ID="ImgBtn" runat="server" CommandName="Select" CommandArgument='<%#Eval("ProductId")%>' />
</ItemTemplate>
ListView.ItemCommand [MSDN]
How do you bind that controls? Check does ListView2 is not bind again after PostBack. If this is bind again Command will be canceled.
Why are you using nested list view? Did you consider using Groups http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.groups.aspx?
In general, if you have a ListView lvInner nested inside a ListView lvOuter, use the handler lvOuter_ItemCommand and the CommandSource property of the ListViewCommandEventArgs.
The following worked for me:
protected void lvOuter_ItemCommand(object sender, ListViewCommandEventArgs e)
{
ListView lvInner = (ListView)e.Item.FindControl("lvInner");
lvInner.SelectedIndex = lvInner.Items.IndexOf((ListViewDataItem)e.CommandSource);
int innerID = (int)lvInner.SelectedDataKey["InnerID"];
// etc.
}
Please note that in this case lvInner is nested inside the ItemTemplate of lvOuter.

Resources