访问 RowCommand 中的数据

发布于 2024-08-18 00:18:48 字数 718 浏览 4 评论 0原文

我是 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"); 

没有可用的 Row 或 Item时

Im new to C# and in VB i could do the following:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十雾 2024-08-25 00:18:48

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

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.

伴我心暖 2024-08-25 00:18:48

这是我的代码:

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

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);
                }
            }
说好的呢 2024-08-25 00:18:48

我在按钮中添加了一个 CommandArgument,并且能够获得我需要的内容:

gridview 中的 .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);

                    }

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

                    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文