How do I find the Client ID of control within an ASP.NET GridView?
Asked Answered
B

6

19

I have a asp:GridView which contains a asp:TextBox within a TemplateField. I would like to obtain it's ID for use in javascript. Something like this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="textDateSent" runat="server" />
        <input type="button" value='Today' 
            onclick="setToday('<%# textDateSent.ClientID %>');" />
    </ItemTemplate>
</asp:TemplateField>

But when I compile, I get an error:

The name 'textDateSent' does not exist in the current context

Anybody know how to get the client ID of this TextBox?

Burnoose answered 15/7, 2009 at 0:11 Comment(0)
A
32

Try this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="textDateSent" runat="server">
        </asp:TextBox>                      
       <input type="button" value='Today' onclick="setToday('<%# ((GridViewRow)Container).FindControl("textDateSent").ClientID %>');" /> 
    </ItemTemplate>
</asp:TemplateField>
Anorthosite answered 15/7, 2009 at 2:13 Comment(4)
Hi Chris as per your I did the following in my code but unable to return value as needed curTexbox.Attributes.Add("onBlur", "return multiplication('" + curTexbox.ClientID + "','" + curTexbox1.ClientID + "','<%# ((GridViewRow)Container).FindControl(txtAmount).ClientID %>')"); can you correct if I am wrongIndebtedness
User, nice username by are you missing quotes around txtAmount?, maybe it is a variable name though. The code is a little confusing because it is odd I see the data binding syntax combined with Attributes.Add which makes it look like something getting called from the row data bound event. If you are in a databinding event then to get the row container for the FindControl you can use e.Row.FindControl where e is the GridViewRowEventArgs passed into the method.Anorthosite
Parser Error Message: The server tag is not well formed. Arrghh how did you guys get this to work!? VS2005 asp.net2 hereTransfigure
Not sure what the problem is, but I would start by checking that all quotes match, and that you have all the begin and end tags correct.Anorthosite
D
2

Maybe you don't want to do it where you need the ClientID. Check out this post here where the controls in a row are referenced in a generic way.

Deceleron answered 15/7, 2009 at 3:27 Comment(0)
E
1

Change <%# textDateSent.ClientID %> to <%= textDateSent.ClientID %>.

Argh, you may need to use the OnDataBinding event of the grid view. Then put a literal control in your javascript. Then you can get the clientID of the text box and feed that into your literal control.

protected void GridViewName_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Create an instance of the datarow
            DataRowView rowData = (DataRowView)e.Row.DataItem;

            //locate your text box
            //locate your literal control
            //insert the clientID of the textbox into the literal control
        }
    }

Look here for a great detailed tutorial on working within this context.

Erubescent answered 15/7, 2009 at 0:12 Comment(2)
Boy...wouldn't direct communication with the question poster be so much easier! :P Check my update.Erubescent
up voted because it works, but I prefer Chris's answer because it's not done in code-behind.Burnoose
L
1

You can get client id like this:

protected void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string strClientID = ((TextBox)e.Row.FindControl("txtName")).ClientID;
    }
}

This will give unique client ID for each textbox in all rows.

Lamori answered 9/8, 2013 at 11:38 Comment(0)
N
0

I just do this...

var tbl = document.getElementById('<%=GridView.ClientID%>');
var checkBox = tbl.rows[i].cells[11].getElementsByTagName("input")[0].id;

the cell should always be the same and it gets rendered into an input. You may have to change the number at the end if you have more then one input in that cell. This will give you the new clientid/id of the input object (checkbox or whatever)

Numbing answered 28/7, 2015 at 15:19 Comment(1)
Clear and simple.Mansoor
H
0

This is what I did. In the aspx page I just passed the entire object to the javascript function, so I didn't even meed to client id. In my case the object was a drop down list in the EditItemTemplate of the GridView. I added an html onchange(this) event in the aspx code.

<asp:DropDownList ID="custReqRegionsDDL" runat="server" onchange='custReqRegionsDDLOnChange(this)'> </asp:DropDownList>

here is my javascript

function custReqRegionsDDLOnChange(myDDL)
    {
        alert('selected text=' + myDDL.options[myDDL.selectedIndex].text);

}

Heirdom answered 29/9, 2017 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.