Accessing HTML Elements in ASP.NET

I want to change the text of a radio button (HTML element), not an ASP.NET component.

How can I change it from ASP.NET?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Add a simple:

runat="server"

to your HTML tag and it will allow a few of the properties to be modified through code behind.

These are known as “hybrid controls.”

Method 2

You would need to add a runat=”server” attribute to the HTML for that element.

<input type="radio" id="someRadioId" value="bleh" runat="server">

This will allow you to access the element via its ID, someRadioId. This element in your code behind will be of type HtmlInputRadioButton.

See this article on MSDN

Method 3

A simple RadioButtonList, when initialized like this:

list.Items.Add(new ListItem("item 1", "1"));
list.Items.Add(new ListItem("item 2", "2"));
list.Items.Add(new ListItem("item 3", "3"));

renders to the following HTML:

<table id="list" border="0">
    <tr>
        <td><input id="list_0" type="radio" name="list" value="1" /><label for="list_0">item 1</label></td>
    </tr><tr>
        <td><input id="list_1" type="radio" name="list" value="2" /><label for="list_1">item 2</label></td>
    </tr><tr>
        <td><input id="list_2" type="radio" name="list" value="3" /><label for="list_2">item 3</label></td>
    </tr>
</table>

So via JavaScript you can loop through the elements with the type “radio”, grab their id and then look for label elements that have the id as the ‘for’ value. And update their innerHTML.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x