Jaggu
Jaggu

Reputation: 6428

Why is not ClientID not evaluating?

I have this inside a usercontrol in my aspx:

<asp:TextBox TextMode="MultiLine" onkeydown="textCounterLatest('<%=txtContent.ClientID%>' , '<%=remLen.ClientID %>', 500);"
                onkeyup="textCounterLatest( '<%=txtContent.ClientID%>' , '<%=remLen.ClientID %>', 500);" ID="txtContent" MaxLength="500"
                runat="server" Height="85px" Width="100%"></asp:TextBox>

But this clientID isn't evaluating. Instead the HTML generated is:

<textarea style="height:85px;width:100%;" onkeyup="textCounterLatest( '&lt;%=txtContent.ClientID%&gt;' , '&lt;%=remLen.ClientID %&gt;', 500);" onkeydown="textCounterLatest('&lt;%=txtContent.ClientID%&gt;' , '&lt;%=remLen.ClientID %&gt;', 500);" id="ctl00_ContentPlaceHolder1_GridView2_ctl02_ucTaxAnswer_txtContent" cols="20" rows="2" name="ctl00$ContentPlaceHolder1$GridView2$ctl02$ucTaxAnswer$txtContent"></textarea>

Can anybody help me out?

Upvotes: 0

Views: 487

Answers (4)

John Culviner
John Culviner

Reputation: 22964

You could do something like this in true webforms fashion in the code behind:

txtContent.Attributes.Add("onkeyup", string.Format("textCounterLatest('{0}' , '{1}', 500);", txtContent.ClientID, remLen.ClientID));

I'll admit its extremely awkward in comparison to any other web development language but welcome to webforms!

Upvotes: 0

fenix2222
fenix2222

Reputation: 4730

Try this

<asp:TextBox TextMode="MultiLine" onkeydown='<%= "textCounterLatest(\"" + txtContent.ClientID + "\", \"" + remLen.ClientID + "\")" %>'                 ID="txtContent" MaxLength="500" runat="server" Height="85px" Width="100%"></asp:TextBox>

Upvotes: 1

John Culviner
John Culviner

Reputation: 22964

since you are already in an ASP.NET code block it is escaping the XML. since you are in a code block just do normal string concatenation and you should be fine:

onkeyup="textCounterLatest( '<%=txtContent.ClientID%>' , '<%=remLen.ClientID %>', 500);"

to

onkeyup="textCounterLatest('" + txtContent.ClientID + "' , '" + remLen.ClientID + "', 500);"

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415931

The code isn't evaluated because it's inside a text literal in your markup.

My first choice for a work-around is the "static" ClientIDMode. Failing that (say, inside a databound item or in older code), I would have a javascript object in the header devoted mainly to clientIDs, and reference that everywhere in my html/javascript markup.

Upvotes: 0

Related Questions