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

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?

Answers:

Answer

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>
Answer

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.

Answer

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.

Answer

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.

Answer

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)

Answer

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

}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.