获取一个GridView的内容的命令行 [英] Getting the Contents of a Gridview in Row Command

查看:156
本文介绍了获取一个GridView的内容的命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code在UI

 < ASP:的TemplateField>
     <&ItemTemplate中GT;
          < ASP:按钮的ID =BTN=服务器的CommandName =编辑文本=AFE/>
     < / ItemTemplate中>
< / ASP:的TemplateField>
- < /专栏>

我想获得所有字段的详细文本框在同一页当过我点击按钮编辑。我试着使用:

 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
     {
    如果(e.CommandName ==编辑)
    {    GridViewRow行=(GridViewRow)(((按钮)e.CommandSource).NamingContainer);    字符串文本= Row.Cells [2]。文本;    }

但我在字符串文本得到..

这是我的网格视图的屏幕截图


解决方案

  1. 您可以使用存储在命令参数的行索引来找到在哪里点击修改按钮一行。

  2. 现在,你可以找到文本框该行在细胞指数的位置是2

  3. 然后获得文本你的文本框

     保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
    {
        如果(e.CommandName ==编辑)
        {
            INT指数= Convert.ToInt32(e.CommandArgument);
            GridViewRow clickedRow = CustomersGridView.Rows [指数]        文本框my​​TextBox =(文本框)clickedRow.Cells [2] .FindControl(TextBoxName);
            字符串文本= myTextBox.Text;
        }
    }


在ASPX页面,则需要处理这样一行命令事件:

 < ASP:GridView控件ID =GridView1
        的DataSourceID =数据源
        的AutoGenerateColumns =假
        onrowcommand =GridView1_RowCommand
        =服务器>
    < / ASP:GridView的>

This is the code in UI

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

I Want to Get the Details of All Fields in Text boxes in Same page when ever I click on the Button Edit. I Tried using :

  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;

    }

But I am getting "" in string text..

This is screenshot of my grid view

解决方案

  1. You can use the row index stored in command argument to find a row where Edit button is clicked.
  2. Now you can find the TextBox in that row where cell index location is 2.
  3. Then get the Text of your TextBox.

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

In ASPX page, you need to handle row command event like this:

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

这篇关于获取一个GridView的内容的命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆