views:

1994

answers:

4

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?

+1  A: 

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.

Andrew Siemer
That doesn't work either.
Keltex
Boy...wouldn't direct communication with the question poster be so much easier! :P Check my update.
Andrew Siemer
up voted because it works, but I prefer Chris's answer because it's not done in code-behind.
Keltex
+7  A: 

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>
Chris Mullins
+1 Very nice! Short and sweet.
Andrew Siemer
+2  A: 

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.

JBrooks
+1  A: 

Hi,

I have a asp:GridView which contains a asp:DropDownList and a asp:TextBox within a TemplateField. My DDL has 2 fields, 'yes' and 'no' and i want the textbox to be hidden when clicking 'no' (and when no selected) and if possible with display:none instead of visibility:hidden in css...

I can't do it without knowing ClientID of my textbox... Can anyone help me ?

I've tried the answer above but i got this: "The server tag is not well formed"

here is my code:

    <ItemTemplate>
<table><tr><td>
<asp:    ID="ddlTypeComposant" AutoPostBack="True"
runat="server" OnSelectedIndexChanged="ddlTypeComposant_SelectedIndexChanged(this,<%# ((GridViewRow)Container).FindControl("TextBoxFacteurs").ClientID %>)" >
<asp:ListItem Value="Yes" Text="Yes"></asp:ListItem>
<asp:ListItem Value="No" Text="No"></asp:ListItem>
</asp:DropDownList>
</td></tr>      
<tr><td>
          <asp:TextBox ID="TextBoxFacteurs" runat="server" Text='' 
              Width="100%" TextMode="MultiLine" Rows="2"></asp:TextBox>
</td></tr>            
</table>
</ItemTemplate>
Anoriel