在ASP:RadioButton上设置值

Jim*_*son 2 asp.net

我知道这在过去一直对我有用,但我radiobutton忽略了设置my值的尝试,并将值设置为我的单选按钮的ID.

ASP.Net

<asp:RadioButton ID="rb" runat="server" />
Run Code Online (Sandbox Code Playgroud)

代码背后

//Test 1
rb.InputAttributes.Add("value", "foo");

//Test 2
rb.InputAttributes["value"] = "foo";
Run Code Online (Sandbox Code Playgroud)

HTML输出

<input id="rb" type="radio" name="rb" value="rb" />
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

在ASP.Net标记中设置值是有效的,但我宁愿从代码隐藏中执行此操作.

Ale*_*har 5

你可以简单地做:

rb.Attributes.Add("value", "foo");
Run Code Online (Sandbox Code Playgroud)

HTML输出:

<input id="ContentPlaceHolder1_rb" type="radio" name="ctl00$ContentPlaceHolder1$rb" value="foo">
Run Code Online (Sandbox Code Playgroud)