Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing HTML Elements in ASP.Net

Tags:

html

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?

like image 398
David Bonnici Avatar asked Dec 29 '08 16:12

David Bonnici


People also ask

How to access HTML element in ASP net?

By default, HTML elements on an ASP.NET Web page are not available to the server. These components are treated as simple text and pass through to the browser. We can convert an HTML element to server control by adding a runat="server" and an id attribute to the component. Now, we can easily access it at code behind.

How to access HTML elements in c#?

You can access them from code behind by adding runat="server" on the html elements.

How to use HTML controls in ASP net?

The HTML server controls are HTML elements that include a runat=server attribute. The HTML server controls have the same HTML output and the same properties as their corresponding HTML tags. In addition, HTML server controls provide automatic state management and server-side events.

Is HTML used in asp net?

ASP can use any scripting language, so as to embed programming and server side directives into a HTML web page. HTML allows web browsers to interpret display content written between tags. It allows images and objects to be embedded in the webpage. ASP is used to design user-interactive or dynamic web pages.


3 Answers

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."

like image 143
Dillie-O Avatar answered Sep 22 '22 22:09

Dillie-O


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

like image 35
steve_c Avatar answered Sep 22 '22 22:09

steve_c


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.

like image 28
Kon Avatar answered Sep 18 '22 22:09

Kon