c# - 在行命令中获取 Gridview 的内容

标签 c# asp.net

这是UI中的代码

<asp:TemplateField>
     <ItemTemplate>  
          <asp:Button ID="btn" runat="server" CommandName="Edit" Text="AFE" />
     </ItemTemplate>
</asp:TemplateField>
--</Columns>

当我单击“编辑”按钮时,我想获取同一页面中文本框中所有字段的详细信息。我尝试使用:

  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
     {
    if (e.CommandName == "Edit")
    { 

    GridViewRow Row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);

    string text = Row.Cells[2].Text;

    }

但我在字符串文本中得到“”..

这是我的 GridView 的截图 enter image description here

最佳答案

  1. 您可以使用存储在命令参数中的行索引来查找点击编辑按钮的行。
  2. 现在您可以在单元格索引位置为 2 的那一行中找到 TextBox
  3. 然后获取 TextBoxText

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName == "Edit")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow clickedRow = CustomersGridView.Rows[index];
    
            TextBox myTextBox = (TextBox)clickedRow.Cells[2].FindControl("TextBoxName");
            string text = myTextBox.Text;
        }
    }
    

在ASPX页面中,你需要像这样处理行命令事件:

    <asp:gridview id="GridView1" 
        datasourceid="DataSource" 
        autogeneratecolumns="false"
        onrowcommand="GridView1_RowCommand"
        runat="server">
    </asp:gridview>

关于c# - 在行命令中获取 Gridview 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22394919/

相关文章:

c# - 在字符串中查找字符串可以为空的单词(有时)

c# - 为什么我的代码在 VB.NET 中编译但 C# 中的等效代码失败

c# - CSS 在设计 View 中显示而不是在运行时

.net - cacheRolesInCookie 不缓存角色

c# - 不区分大小写的查询表达式

javascript - GridView RowUpdating 在第二页中不起作用

asp.net - 向 asp.net 添加样式表(使用 Visual Studio 2010)

asp.net - 当文件位于不同的服务器上而不先将其下载到我的服务器上时,使用 ASP.Net MVC 在浏览器中强制下载文件

android - 何时从 Asp.net 关闭与 oracle 数据库的连接

c# - ushort 数组到 Image 对象