Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the ID of a asp.net control programatically - ASP.NET

Tags:

c#

asp.net

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.

like image 647
NLV Avatar asked Feb 28 '11 12:02

NLV


People also ask

How do I change the appearance of an ASP NET control programmatically?

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:

What is difference between filter config and identity config in ASP NET?

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.

How does the web engine set the id values for controls?

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.

How are server controls added to an ASP NET page?

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.


2 Answers

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"

like image 157
Peter Kelly Avatar answered Oct 10 '22 16:10

Peter Kelly


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" />
like image 22
Shekhar_Pro Avatar answered Oct 10 '22 17:10

Shekhar_Pro