Can I change the identifier of an asp.net control programmatically - ASP.NET

I have a control

<asp:Button ID="btnAjax" runat="server" Text="Ajaxified" OnClick="btnAjax_Click" /> 

Now from the code behind I want to change the button id

 btnAjax.ID = "newButtonID"; 

But now it works. Is this possible in the first place?

EDIT

I created the control in the HTML mark, not through the code.

+4
source share
3 answers

Yes, it is possible that your code you posted will work.

I have a button and text box

 <asp:Button ID="button" runat="server" /> <input type="text" id="result" name="result" runat="server" /> 

Uploading to the page I change the identifier of the button and then displaying the result in the text box.

 protected void Page_Load(object sender, EventArgs e) { button.ID = "OtherButton"; result.Value = button.ID; } 

The result in the text box is "OtherButton"

+5
source

Yes, you can read and write this property [in state].

I tried and yes, the ID can be changed as well as reflected in the displayed HTML.

Update

There is a ClientIDMode attribute that you can set to "Static", which you can use to make asp.net not change the identifier in the displayed HTML.

  <asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static" /> 
+3
source

The only reason I can imagine that it would be necessary to achieve something like this is to safely allow the client script to interact with the button. If so, then I prefer to pass the client control identifier to the script:

 var btnAjax = document.getElementById('<%=btnAjax.ClientID%>') 

Or using jQuery:

 $('#<%=btnAjax.ClientID%>') 
0
source

Source: https://habr.com/ru/post/1341644/


All Articles