Accessing HTML Elements in ASP.Net

后端 未结 3 922
情话喂你
情话喂你 2021-01-12 09:08

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

How can I change it from ASP.NET?

Thanks

相关标签:
3条回答
  • 2021-01-12 09:11

    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.

    0 讨论(0)
  • 2021-01-12 09:16

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

    0 讨论(0)
  • 2021-01-12 09:36

    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

    0 讨论(0)
提交回复
热议问题