Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

id and name attributes of HTML elements manipulated by ASP.NET

I have a problem in my C# ASP.NET application, where the id and name tags are modified at runtime with a prefix "MainView_" and "MainView$" respectively. So my code:

<asp:Button ID="OKToContinueCheckInButton" runat="server" CausesValidation="False" Visibility="false" Style="display: none" OnClick="btnOKToContinueCheckIn" />

<script type="text/javascript">
<!--
    var answer = confirm("Some Warning");
    if (answer)
        document.getElementById('OKToContinueCheckInButton').click();
// -->
</script> 

becomes:

<input type="submit" name="MainView$OKToContinueCheckInButton" value="" id="MainView_OKToContinueCheckInButton" Visibility="false" style="display: none" />

<script type="text/javascript">
<!--
    var answer = confirm("Some Warning");
    if (answer)
        document.getElementById('OKToContinueCheckInButton').click();
// -->
</script>

getElementID() returns null because the name has changed. Can anyone tell me why this occurs and if there is a way to possibly disable it from changing the id and name values. Thanks!

-Sephrial

like image 966
Sephrial Avatar asked Jan 29 '09 02:01

Sephrial


2 Answers

I think this would help you...

<script type="text/javascript">
<!--
    var answer = confirm("Some Warning");
    if (answer)
        document.getElementById('<%= OKToContinueCheckInButton.ClientID %>').click();
// -->
</script>

This is the part that does the trick

<%= OKToContinueCheckInButton.ClientID %>
like image 166
Cyril Gupta Avatar answered Nov 05 '22 21:11

Cyril Gupta


This is a fundamental part of the way the Asp .Net Forms model work.

The IDs are changed so that you don't have to worry about keeping IDs unique across user controls, custom controls, repeaters etc.

You can use Cyril's method. I also find JQuery suits this model very well to because you can easily reference controls by their class or position in the document.

like image 25
cbp Avatar answered Nov 05 '22 19:11

cbp