What is the most efficient and robust way to program ASP.NET with javascript?
I wanted to know what is the best approach to program ASP.NET with JS? So far, I've been trying t开发者_如何学Pythono code with the Codebehind attributes.add("onclick," ...."); is there a better more efficient way to use JS with ASP.NET?
thanks
writing all your function in a separate js file is far more flexible that what you are doing now. The problem might be getting the control ID's in your js file. Well there are many methods.
Hardcoding
Just view the source and figure out the ID of the control which you want to use. this is easy but if you you may want to change the ID of the control at some point of time the respective functions will not work
Something like This
Declare a variable with all the the ClientIDs in the aspx file use it in the js file
var MyControlIDs = {"Textbox1":"<%= Textbox1.ClientID %>","Label1":"<%= Label1.ClentID %>"};
and in your js file var mycontrol= window.MyControlIDs
and use it wherever you want
var textbox = document.getElementById(mycontrol.TextBox1);
Many WebControls have an OnClientClick property, which is equivalent of what you're doing. Also you can always set it in your markup for controls that don't have the property.
精彩评论