如何通过RowCommand事件在gridview中启用单个文本框? [英] How to enable a single textbox in gridview through RowCommand event?

查看:94
本文介绍了如何通过RowCommand事件在gridview中启用单个文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

所以情况就是这样。我有一个Gridview,其中包含一个包含文本框的字段。我隐藏了字段并禁用了文本框,即Enabled = False。 Gridview中还有另一个包含LinkBut​​ton的字段。在单击任何行的链接按钮时,我想使文本框的列可见,并仅启用单击链接按钮的行的文本框。我使用下面的代码。它显示文本框列,但所有文本框都被禁用。请帮忙。



Hi all,
So here is the situation. I have a Gridview with a field containing textbox. I have hidden the field and made the textbox disabled, that is Enabled = False. There is another field in the Gridview containing LinkButton. On clicking on a linkbutton of any row, i want to make the column with textbox visible and enable only the textbox of the row from which the linkbutton was clicked. I used the below code. It is showing the textbox column, but all the textbox are disabled. Please help.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "due_payment")
        {
            GridView1.Columns[8].Visible = true;

            int rowIndex = Convert.ToInt32(e.CommandArgument);
            TextBox txt =(TextBox)GridView1.Rows[rowIndex].FindControl("TextBox4");
            txt.Enabled = true;
        }
    }





谢谢..



Thanks..

推荐答案

我认为你必须在网格视图中获取所选行的Id,然后使用该行的Id,你可以启用你的文本框。我希望你在GridView中通过这种类型得到Command参数



CommandArgument ='<%#DataBinder.Eval(Container,RowIndex)%>'



如果不适用它。



再次检查你的文本框ID在网格视图中必须是相同的。





}
I think you must get the Id of selected row in grid view and then using Id of that row you can enable your text box.I hope you are getting Command argument by this type in GridView

CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'

if not apply it.

Also check again your textbox Id it must be same in the grid view.


}


选中此项。我不知道你的HTML代码是什么,所以我发布了这个工作示例作为你的要求。

你只需要绑定Gridview。并且它将按照您的要求工作

Gridview代码。



Check This. I don't know what is your HTML code is, so i posted this working example as your requiremnts.
You Just have to bind the Gridview. and it will work as your requirement
the Gridview Code.

<asp:GridView ID="GridView1" runat="server" GridLines="None"

         EmptyDataText="No Rcords Found" AutoGenerateColumns="False"

         onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound">
         <Columns>
             <asp:TemplateField>
                 <EditItemTemplate>
                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                 </EditItemTemplate>
                 <ItemTemplate>
                     <asp:LinkButton CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' ID="LinkButton1" runat="server" CommandName="sel">Select</asp:LinkButton>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="ID">
                 <EditItemTemplate>
                 </EditItemTemplate>
                 <ItemTemplate>
                     <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("ID") %>' Enabled="false"></asp:TextBox>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="name" Visible="false">
                 <EditItemTemplate>
                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                 </EditItemTemplate>
                 <ItemTemplate>
                     <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("name") %>' Enabled="false"></asp:TextBox>
                 </ItemTemplate>
             </asp:TemplateField>
         </Columns>





和行指令





and on row command

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName == "sel")
       {
           int rowIndex = Convert.ToInt32(e.CommandArgument);
           GridView1.Columns[2].Visible = true;
           TextBox txt = (TextBox)GridView1.Rows[rowIndex].FindControl("TextBox1");
           txt.Enabled = true;
       }
   }


这篇关于如何通过RowCommand事件在gridview中启用单个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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