Accessing data in RowCommand
Im new to C# and in VB i could do the following:开发者_Go百科
Protected Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
If e.CommandName = "CommandName" Then
Dim label1 As Label = e.Item.FindControl("label1")
Response.Write(label1.Text))
End If
End Sub
in C# and the RowCommand, I cannot use findcontrol to access a controls value. I want to get the value of two label's so I can use them when I call a method in the rowcommand
Update: In C# when I do
Label label1 = (Label)e.Item.FindControl("label1");
or
Label label1 = (Label)e.Row.FindControl("label1");
I do not have Row or Item available
Where does Label1 exist? Could you post your C# example? It should also be DataGridCommandEventArgs type, so maybe it's a different argument? I don't see, as the same event arg type, how Item couldn't exist. It's hard to tell without seeing the full C# example.
Here is my code:
<asp:GridView ID="gridview1" runat="server" Width="98%" AutoGenerateColumns="false"
AllowPaging="True" PageSize="10" PagerStyle-HorizontalAlign="center"
OnRowCommand="gridView_RowCommand"
>
<columns>
<asp:TemplateField HeaderText="Active" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblArticleId" Text='<%# Eval("Id")%>' Visible="false" runat="server"></asp:Label>
<asp:Button ID="btnActive" CommandName='<%# Eval("Activity")%>' Text='<%# Eval("Activity")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
.cs:
protected void gridView_RowCommand(object source, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Disable")
{
UpdateArticleVisibility(true, [lblArticleID.Text value], gOrgId);
}
if (e.CommandName == "Enable")
{
UpdateArticleVisibility(false, [lblArticleID.Text value], gOrgId);
}
}
I added a CommandArgument in the button and was able to get what I needed:
.aspx in the gridview
<asp:Button ID="btnActive" CommandArgument='<%# Eval("Id")%>' CommandName='<%# Eval("Activity")%>' Text='<%# Eval("Activity")%>' runat="server" />
then in the RowCammand in the .aspx.cs
protected void gridview_RowCommand(object source, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Disable")
{
string[] args = e.CommandArgument.ToString().Split(',');
Guid gArticleId = new Guid(args[0]);
Response.Write(gArticleId);
}
精彩评论