views:

65

answers:

3
+1  Q: 

Asp.net gridview

I have a text box inside gridview. I need to get the id of textbox in javascript. When i use like this '<%= txtNewQty.ClientID %>'; it gives the compilation error.

A: 

Controls inside Gridviews are not members of the Page, they just exist in the .aspx . They are gonna be repeated in each row so they are dinamically created.

To get a reference of the Control inside a row of the Gridview you can use FindControl

foreach(GridViewRow row in gridview.Rows)
{
    Textbox tb = row.FindControl("txtNewQtv") as Textbox;
    string id = tb.ClientID;
    //Generate your javascript here so it can be callable on the client
}
Carlos Muñoz
A: 

txtNewQty does not exist at the page level and therefore you will not be able to reference it in that manner. you'll have to do something like generate the js on the server side or work your way down from the gridview element. post some code and a description of what you want to happen.

lincolnk
A: 

Here is a bad way to do it as it is hardcoded to the column index. It works but there must be a better way using jQuery. Unfortunately I'm not an expert with that.

<script type="text/javascript">
    function Update(btn) {
        alert(btn.parentNode.parentNode.children[0].children[0].id);
    }
</script>
<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txtNewQtv" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <input type="button" onclick="Update(this)" value="Update" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
JumpingJezza