0

Scene : I have a ASP.Net 2.0 app that I need to add functionality to. I need to loop through a gridview's items and compare them to another gridview, If they exist in the other, I must pop up a confirm message to increment the qty. I couldnt find alot on ASP 2.0 so i decided to user a hidden asp field to store what i am processing and based on that registering a clientside script to change the hidden field value and then simulate a postback (I have tried _doPostBack()). So whats happening now is I am trying to access the asp button to simulate a click, but the javascript gets a Null instance everytime. Please advise. (For the testing, i try to alert the button instance, which returns null)

ClientScript.RegisterStartupScript(GetType(String), "ConfirmationScript", "if (confirm('This item already exists, Increment the qty?') == true) {alert(document.getElementById('<%=btnAddSpecificLine.ClientID%>'));}", True)
2
  • 1
    That string will literally be put on the page. By that point, there's nothing to evaluate the ASP code <%=btnAddSpecificLine.ClientID%>. I believe you'll have to concatenate that script string with btnAddSpecificLine.ClientID instead of trying to evaluate it with <%= %> Commented May 31, 2013 at 14:28
  • It seems to return the actual <%=btnAddSpecificLine.ClientID%> in this case, So it doesnt resolve the clientid of the server control as it should Commented May 31, 2013 at 14:31

1 Answer 1

2

Try your test like this:

ClientScript.RegisterStartupScript(GetType(String), "ConfirmationScript", "if (confirm('This item already exists, Increment the qty?') == true) {alert(document.getElementById('" & btnAddSpecificLine.ClientID & "'));}", True)

Since you generate this code server-side - pass ClientID directly

Sign up to request clarification or add additional context in comments.

3 Comments

You are the man! Thank you, Amazing how one could miss the obvious like this at times.
Very odd though as the clientid's stayed the exact same as specified in the html... would have thought that they would change since its server controls.
It changes if your control is hosted inside of some kind of a container - then the id becomes a combined ID of container ID + Control ID. Otherwise it remains the same, but it is always a good idea to use Client ID

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.