【问题标题】:How to get the non visible gridview cell value in ASP.net?如何在 ASP.net 中获取不可见的 gridview 单元格值?
【发布时间】:2012-07-24 05:10:19
【问题描述】:

我有一个像下面这样的网格。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" s                                      
                                    onrowcommand="GridView1_RowCommand">
  <Columns>
      <asp:ButtonField DataTextField="Name" HeaderText="Name" />
      <asp:BoundField DataField="ArrDate" DataFormatString="{0:MM/dd/yyyy}" 
                              HeaderText="Arr Date" />
      <asp:BoundField HeaderText="Dep Date" DataField="DepDate" 
                           DataFormatString="{0:MM/dd/yyyy}" />
      <asp:BoundField HeaderText="Mail" DataField="Mail" />
      <asp:BoundField HeaderText="Status" DataField="Status" />
      <asp:BoundField DataField="ResId" HeaderText="ResId" Visible="False" />                                        
  </Columns>
</asp:GridView>

在代码后面:-

try
{
  string text = GridView1.Rows[2].Cells[5].Text;
  ScriptManager.RegisterStartupScript(this, GetType(), "Message", "alert('ResId = " + text  + ".');", true);             
}
catch { }

现在消息显示 - RegId =。

我无法获得价值。所以我将 RedId BoundField 更改为可见。现在我得到了价值。

即 RegId =6。

我现在有两个问题 -

1) 如何获取不可见列中的 RegId 值。 2)我如何找到我点击的行值... bzs唯一我可以在代码中更改 ROWVALUE ..

 string text = GridView1.Rows[ROWVALUE].Cells[5].Text;

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    这当然不是正确的做法。您可能想看看使用 DataKeys 来实现这一点。使用当前方法,当您向网格添加新列时,您的代码将失败。

    1. 在 gridview 的 DataKeys 属性中添加 RegId 列
    2. 像这样在代码隐藏中为当前行引用您的 gridview 数据键

       int regId= Convert.ToInt32(YourGridview.DataKeys[rowIndex]["RegId"].ToString());
      

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 2021-06-28
    • 2015-02-15
    • 1970-01-01
    相关资源
    最近更新 更多