vamsivanka
vamsivanka

Reputation: 792

Gridview with TextBox

i have an gridview control with a comment text, link button, and an

invisible (text box and a button to post to database.)

when i click on the link button i want to show the textbox.

can any one help me how to do this.

my gridview code:

<asp:GridView ID="grdComments" runat="server" AutoGenerateColumns="False">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <table width="500px" cellpadding="3" cellspacing="3">
                <tr/>
                    <td/>
                        <asp:Label runat="server" ID="lblLeftPad"></asp:Label>
                        <asp:Label runat="server" ID="lblComment" Text='<%# container.dataitem("CommentText") %>'></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:LinkButton ID="lbtnReply" Text="Reply" runat="server" CommandName="CommentReply"></asp:LinkButton>
                    </td>
                </tr>
                <tr>
                        <td>
                            </asp:TextBox ID="txtReply" runat="server" Height="50px" Width="500px" Visible="false"></asp:TextBox>
                        </td>
                    </tr>
               </table>

        </ItemTemplate>
    </asp:TemplateField>
</Columns>

Upvotes: 1

Views: 1635

Answers (1)

Kevin LaBranche
Kevin LaBranche

Reputation: 21088

If you are not using Javascript / AJAX then on the link button's click event set the textbox's visible value to true. The link button automatically sends a postback so this would work unless you have the link button set to not auto postback.

EDIT: To access link button

There are a few ways depending on how you setup your grid. If this is a Command Field or a button field that you are using then you can use the RowCommand and the e.CommandArgument to now which row you are on and then set the textbox to true. Below is a sample:

    row = Integer.Parse(e.CommandArgument)
    GridView1.Rows(row).Cells(1).Controls(1).Visible = True 

The cell is set to the column you are wanting to work on and the controls # is set to the control you want to work with in the cell. There is more than one control created in a cell even if you only put a textbox. You can use the FindControl syntax to more reliably get at your control.

If you created a templated field with the link button then on the command argument for the link button set it's value to: =<%# CType(Container,GridViewRow).RowIndex %>

and the above code in the gridview's rowcommand will work.

OR you can set the link buttons click event to something like:

gridview1.rows(directcast(sender,LinkButton).CommandArgument).cells(1).Controls(1).visible = true

You can get to the link buttons click event in a templated field by editing the template from the GUI and double clicking on the link button.

I would recommend using the RowCommand option and using the FindControl Syntax to make your app more readable and easier to maintain.

Upvotes: 2

Related Questions