访问网格视图行';s来自该行内LinkButton的数据

本文关键字:LinkButton 数据 视图 网格 访问 | 更新日期: 2023-09-27 18:24:05

GridView的每一行都是从一个SQL数据库中填充的。

每一行都有一个LinkButton,它会弹出一个窗口。

在后面的代码中,我想访问DataField="RCID"

我想我想把RCID字段附加到Upload链接上,这样当它被点击时,我就可以访问处理onclick的函数中的RCID。

如何获取这些行RCID?

            <asp:GridView ID="GridView1" runat="server" 
            OnPageIndexChanging="GridView1_PageIndexChanging" 
            GridLines="Horizontal" AllowPaging="true" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField HeaderText="RCID" DataField="RCID" Visible="false"></asp:BoundField>
                <asp:BoundField HeaderText="RC Type" DataField="RCType"></asp:BoundField>
                <asp:BoundField HeaderText="Channel" DataField="Channel"></asp:BoundField>
                <asp:BoundField HeaderText="Total" DataField="Total"></asp:BoundField>
                <asp:BoundField HeaderText="Expired In 30 Days" DataField="ExpiredIn30Days"></asp:BoundField>
                <asp:BoundField HeaderText="Expired In 60 Days" DataField="ExpiredIn60Days"></asp:BoundField>
                <asp:BoundField HeaderText="Expired In 90 Days" DataField="ExpiredIn90Days"></asp:BoundField>
                <asp:BoundField HeaderText="Last Updated" DataField="LastUpdated"></asp:BoundField>
                <asp:TemplateField HeaderText="Management">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="Upload" Text="Upload" CommandName="Upload" ></asp:LinkButton> |
                        <asp:LinkButton runat="server" ID="Details" Text="Details" CommandName="Details"></asp:LinkButton> |
                        <asp:LinkButton runat="server" ID="Files" Text="Files" CommandName="Files"></asp:LinkButton> |
                        <asp:LinkButton runat="server" ID="Edit" Text="Edit" CommandName="Edit"></asp:LinkButton> |
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>

        </asp:GridView>

访问网格视图行';s来自该行内LinkButton的数据

可能的解决方案如下:

  1. 为GridView添加RowCreated和RowCommand事件
  2. 在RowCreated事件中,我已将标题行和数据行的第一列的"Visible"属性设置为"false"
  3. 在RowCommand事件中,首先检查CommandName,然后检索select命令的索引。我检索了该索引的行,然后检索了指定行单元格中的文本

检查这个例子:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Visible = false;
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            Label1.Text = (row.Cells[0].Text);
        }
    }

第二种方法:将命令参数指定给隐藏字段值。这将减少另一项工作。检查方式:

<asp:TemplateField HeaderText="Action3" Visible="false">
 <ItemStyle HorizontalAlign="Center"></ItemStyle>
 <ItemTemplate>
<asp:LinkButton ID="lnkretqty" runat="server" Text="Return Qty" CommandName="RETQTY" ToolTip="Click here to Add Return Qty Entry"
 CommandArgument='<%# Container.DataItemIndex %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Code Behind code
protected void gvsearch_RowCommand(object sender, GridViewCommandEventArgs e)
 {
 try
 {
 if (e.CommandName == "SRCSELREC")
 {
Int32 rowind = Convert.ToInt32(e.CommandArgument.ToString());
string val = ((Label)gvgpitemdtl.Rows[rowind].FindControl("d")).Text.ToString();

 }
}
catch (Exception ex)
 {
 General.MessageBox(this.Page, "Error at Gridview Row Command : " + ex.Message.ToString());
 return;
 }
}

参考文献:
在GridView中获取隐藏列值的方法
如何在GridView中获取隐藏列值
在asp.net中RowCommand事件的GridView中查找控件
如何在ASP.NET 中隐藏GridView列并从隐藏的列单元格中检索值

在链接按钮的标记中添加CommandArgument='<%# Eval("RCID") %>'

<asp:LinkButton runat="server" ID="Upload" Text="Upload" CommandName="Upload" CommandArgument='<%# Eval("RCID") %>'/>

现在,在处理程序的代码后面,只需读取传递的GridViewCommandEventArgs参数的CommandArgument属性:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Upload")
    {
        var valueOfRCID = e.CommandArgument;
    }
}

mshsayem可能是对的,但请注意,您也可以从点击事件中获取值,如下所示

<asp:LinkButton runat="server" ID="Upload" Text="Upload" OnClick="Upload_Click" CommandArgument='<%# Eval("RCID") %>'/>
  protected void Upload_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)(sender);
        string RCID = btn.CommandArgument;
    }