【问题标题】:Accessing data in RowCommand在 RowCommand 中访问数据
【发布时间】:2010-01-11 16:40:23
【问题描述】:

我是 C# 新手,在 VB 中我可以执行以下操作:

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

在 C# 和 RowCommand 中,我无法使用 findcontrol 访问控件值。我想获取两个标签的值,以便在调用 rowcommand 中的方法时可以使用它们

更新: 当我这样做时在 C# 中

Label label1 = (Label)e.Item.FindControl("label1"); 

Label label1 = (Label)e.Row.FindControl("label1"); 

我没有可用的行或项目

【问题讨论】:

  • 您在 C# 中处理与在 VB 中相同的事件吗?

标签: c# asp.net findcontrol


【解决方案1】:

Label1 存在于哪里?您可以发布您的 C# 示例吗?它也应该是 DataGridCommandEventArgs 类型,所以也许它是一个不同的参数?我看不出,作为相同的事件 arg 类型,Item 是如何不存在的。没有看到完整的 C# 示例就很难判断。

【讨论】:

    【解决方案2】:

    这是我的代码:

    <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);
                    }
                }
    

    【讨论】:

      【解决方案3】:

      我在按钮中添加了一个 CommandArgument 并且能够得到我需要的东西:

      网格视图中的.aspx

        <asp:Button ID="btnActive" CommandArgument='<%# Eval("Id")%>' CommandName='<%# Eval("Activity")%>' Text='<%# Eval("Activity")%>' runat="server" />
      

      然后在.aspx.cs中的RowCammand

        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);
      
                          }
      

      【讨论】:

        猜你喜欢
        • 2011-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-19
        • 2016-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多