【问题标题】:Cannot get Values from Gridview无法从 Gridview 获取值
【发布时间】:2014-03-16 20:28:41
【问题描述】:

您好,我有一个名为 FolderGridView 的 gridview。在 GridView 中有一个模板字段,在模板字段内我指定了一个链接按钮。现在我无法从 codeBehind 中的 linkBut​​ton 获取值。

<asp:GridView ID="FolderGridView" runat="server"
        AutoGenerateColumns = "False" 
        AllowPaging ="True"  OnPageIndexChanging ="FolderGridView_PageIndexChanging" CellPadding="4" ForeColor="#333333" GridLines="None" 
        >
            <AlternatingRowStyle BackColor="White" />
        <Columns>

            <asp:TemplateField HeaderText="Select">
                <ItemTemplate>
                    <asp:CheckBox ID="FolderCheckBox" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>



            <asp:TemplateField HeaderText="Folder Name">
                <ItemTemplate>
                    <asp:LinkButton Text='<%#Eval("File Name")%>' PostBackUrl='<%# String.Format("InsideFolder.aspx?FolderName={0}",  Eval("File Name")   ) %>' runat="server" />
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
</asp:GridView>

我的代码在后面

for (int i = 0; i < FolderGridView.Rows.Count; i++)
    {
        CheckBox chk = (CheckBox)FolderGridView.Rows[i].FindControl("FolderCheckBox");
        if (chk.Checked == true)
        {
            string FileName = (string)FolderGridView.Rows[i].Cells[1].Text.ToString();

        }
    }

我已经调试了后面的代码部分。在 FileName 中,当我到达这一点时,我得到一个空字符串。那么如何从模板字段中获取值呢?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    单元格中的文本在 LinkBut​​ton 控件内部,所以首先要获取 GridView 行的 LinkBut​​ton 控件,然后才能访问 Text 属性。以下代码应该适用于您的情况:

    for (int i = 0; i < FolderGridView.Rows.Count; i++){
        CheckBox chk = (CheckBox)FolderGridView.Rows[i].FindControl("FolderCheckBox");
        if (chk.Checked == true){
           foreach (Control ctl in FolderGridView.Rows(i).Cells(1).Controls) {
               if (ctl is LinkButton) {
               string filename = ((LinkButton)ctl).Text;
               }
        }
    }
    

    【讨论】:

      【解决方案2】:

      将链接按钮替换为

      编辑: 对于链接按钮:

      <asp:LinkButton ID="LinkButton1"  AutoPostBack="True"  OnClick="someMethod" Text='<%#Eval("File Name")%>' PostBackUrl='<%# String.Format("InsideFolder.aspx?FolderName={0}",  Eval("File Name")   ) %>' runat="server" />
      

      和后面的代码

      protected  void someMethod(object sender, EventArgs e)
              {
                  foreach (GridViewRow item in GridView5.Rows)
                  {
                      CheckBox chk = (CheckBox)item.FindControl("FolderCheckBox");
                      if (chk.Checked == true)
                      {
      
                          string probname = chk.Text;
                      }
      
                  }
              }
      

      【讨论】:

      • 但我想用链接按钮来做。我正在尝试从链接按钮中检索文本。我可以这样做吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 2013-11-08
      相关资源
      最近更新 更多