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.
Related
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
}
}
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.
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;
I have this simplified code:
<asp:ListView ID="ListView1" runat="server" DataSourceID="sqldatasource1">
<ItemTemplate>
<asp:Button ID="ButtonTest" runat="server" BackColor="Silver" Text="Add to Cart" />
</ItemTemplate>
</asp:ListView>
I am trying to run some code behind the button but I don't know how. I can't access it within the listview. Not that this is important, but im trying to get information from the current listview(the product ID) and pass it to the next page.
Anybody know how to approach this, with the exact code? Thanks!
You want the ItemCommand event. You would give your button a command name, a command argument (if you want), then listen for the ItemCommand event on the ListView
<asp:ListView ID="ListView1" runat="server" OnItemCommand="DoTheCommand" DataSourceID="sqldatasource1">
<ItemTemplate>
<asp:Button CommandName="Foo" CommandArgument='<%#Eval("SomeDataBoundProperty")%>' ID="ButtonTest" runat="server" BackColor="Silver" Text="Add to Cart" />
</ItemTemplate>
</asp:ListView>
And in your code behind:
void DoTheCommand(object sender, ListViewCommandEventArgs e) {
string commandName = e.CommandName;
object commandArg = e.CommandArgument;
ListViewItem selectedItem = e.Item;
int dataItemIndex = selectedItem.DataItemIndex;
if (commandName == "Foo") {
//and so on
}
}
U can use ListView's ItemCommand Event in codebehind with commandName and command argument set in aspx.
<asp:ListView ID="ListView1" runat="server" DataSourceID="sqldatasource1">
<ItemTemplate>
<asp:Button ID="ButtonAdd" runat="server" BackColor="Silver" CommandName="cAdd" CommandArgument= '<%# Eval("ProductId") %>' Text="Add to Cart" />
Protected Sub ListView1_OnItemCommand(ByVal sender As Object, _
ByVal e As ListViewCommandEventArgs) Handles Listview1.ItemCommand
If String.Equals(e.CommandName, "cAdd") Then
Dim ProductId as integer = e.CommandArgument
'your code
End If
End Sub
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;
}
}