Why Javascript for ASP.net developers is different ?

The first difference between ASP.net and other web technologies that it has its own web controls which sometimes map to standard HTML controls, so sometimes what you see in ASP.net as a control doesn't mean that it will be rendered as one control in the final HTML which means that Javascript can't deal with it directly as it does with basic control.
Taking the ASP.net Calendar control as an example, in ASP.net you use it as one
control just drag it and drop it in the page and boom! you have a full functional
 calendar, but in reality this calendar renders as a Table with rows and columns
and links and also lots of Javascript, so you can't expect that you will deal with
 it as a calendar in the Javascript side, this also applies to Repeater, Grid,
 DataList ... etc

The second difference is that even with asp.net server controls that maps directly
to standard HTML controls like TextBox or Button, the asp.net runtime changes the
control Id's so if placed a code like this in your ASP.net page directly

<asp:TextBox ID="myTextBox" runat="server"></asp:TextBox>

the rendered equivalent HTML will look like this

<input type="text" id="myTextBox" name="myTextBox"/>

As you can see the asp:TextBox id is the same as the input text id which is
"myTextBox", but watch out, if you put the previous asp:TextBox in a UserControl
instead of adding it directly to the page, and then added the UseControl to the page
 like this lets name our usercontrol "MyControl"

<uc1:MyControl ID="MyControl1" runat="server" />

the rendered equivalent HTML will look like this

<input type="text" id="MyControl1_myTextBox" name="MyControl1$myTextBox"/>

As you can see the rendered input field Id is not "myTextBox" but it is a combination
 of the usercontrol id and the textbox id separated by underscore "_" and the
name of the control is similar but separated by dollar sign "$".
So that's why you will face some difficulties when you try to use
document.getElementById on the "myTextBox" because the name is not known until
the page renders.
Reactions

Post a Comment

0 Comments

Close Menu