I've a control
<asp:Button ID="btnAjax" runat="server" Text="Ajaxified" OnClick="btnAjax_Click" />
Now from the code behind I want to change the ID of the button
btnAjax.ID = "newButtonID";
But it is now working. Is it possible at the first place?
EDIT
I've created the control in the HTML mark up and not through code.
NET Web Server Controls ASP. NET Web Server Controls and CSS Styles You can set style properties of an ASP.NET server control programmatically, which allows you to change the appearance of a control conditionally. Use the following hierarchical convention for specifying the style object and property you want to set:
Filter Config contains all the filters getting applied to action methods and controllers. Identity Config file holds all ASP.NET identity related details like, how user authentication process happens. Route Config file defines ASP.NET routes in a web application, It has a default route to manage the urls in the application.
The ASP.NET engine sets the ID values at runtime for those controls whose IDs have not been explicitly set. It uses the naming pattern ctlXX, where XX is a sequentially increasing integer value. Because the master page itself serves as a naming container, the Web controls defined in the master page also have altered rendered id attribute values.
Most server controls in an ASP.NET page are added explicitly through the page's declarative markup. The MainContent ContentPlaceHolder control was explicitly specified in the markup of Site.master; the Age TextBox was defined IDIssues.aspx 's markup.
Yes it is possible and your code you posted will work.
I have a button and a text field
<asp:Button ID="button" runat="server" />
<input type="text" id="result" name="result" runat="server" />
On page load I change the ID of the button and then output the result to the text field.
protected void Page_Load(object sender, EventArgs e)
{
button.ID = "OtherButton";
result.Value = button.ID;
}
The result in the text field is "OtherButton"
Yes you can that Property is both read and write [able].
I tried and yes the ID can be changed and its also reflected in rendered HTML.
Update
There is a ClientIDMode attribute which you can set to 'Static' that you can use to force asp.net into not changing the ID in rendered HTML.
<asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With