0

I have a variable in javascript:

var activeSearchButton = document.getElementById('<%=btnSearch.ClientID %>');

I want to set it like this:

<asp:TextBox ID="WFTo" runat="server" onchange="activeSearchButton = document.getElementById('<%=btnWFSearch.ClientID %>');"></asp:TextBox>

When I am trying to use the varible in javascript function, it is null. And the source shows:

onchange="activeSearchButton = document.getElementById('&lt;%=btnSearch.ClientID %>');"

I noticed '& lt;' instead of "<". Is this the reason of my problem? Why does this happen? Or maybe there is another reason?

Thank you very much.

2
  • Server side controls are replaced by the server, that is what asp.net is all about :D that is the reason you cannot find that control. Commented Sep 12, 2011 at 16:48
  • So why doesn't it replace it for me? I expected onchange="activeSearchButton = document.getElementById('ctl00_CPHDefault_tcTPS_TPProd_ctl01_tcProduction_TPTitleProgress_btnSearch'');" Commented Sep 12, 2011 at 17:08

2 Answers 2

4

Just create a function in <script /> section

function setActiveSearchButton()
{
    activeSearchButton = document.getElementById('<%=btnSearch.ClientID %>');
}

and use

<asp:TextBox ID="WFTo" runat="server" onchange="setActiveSearchButton()"></asp:TextBox>

EDIT:

As it turns out, you cannot use <%= .. %> for server side controls. You'll have to resolve to using a regular html input like this

<input type="text" onchange="setActiveSearchButton('<%= btnSearch.ClientID %>')" />

and

function setActiveSearchButton(buttonId)
{
    activeSearchButton = document.getElementById(buttonId);
}

or leave the <asp:TextBox /> as it is and on Page_Load do

 protected void Page_Load(object sender, EventArgs e)
 {
     WFTo.Attributes.Add("onchange", "setActiveSearchButton('" + btnSearch.ClientID +"')");
 }
Sign up to request clarification or add additional context in comments.

9 Comments

I could create such a function, but I have more than one button, and I would prefer not to create a function for each of them. Can I pass the button's ClientID into the function? I tried this: onchange="setActiveSearchButton(document.getElementById('<%=btnSearch.ClientID %>'));", but it gives me the same problem as before. Is what I want impossible? Thanks.
Thank you for your answer. But I want to set activeSearchButton to btnSearch, not to the textbox whose change fires the event.
I made setActiveSearchButton(button) exactly like you did. But the problem is how to pass the button's id. As I have already said, "btnSearch" is not replaced with the ClientID.
I don't see any changes. Am I missing something? Thanks.
Still doesn't work: onchange="setActiveSearchButton('&lt;%=btnSearch.ClientID %>')
|
0

You can do this:

onchange='<% = string.Format("activeSearchButton = document.getElementById(\"{0}\");", btnWFSearch.ClientID) %>'

EDIT: As our friend said, we can only do this in a "input" control. For a TextBox the best way is add a parameter in codebehind.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.