Set value property of RadioButton

旧城冷巷雨未停 提交于 2019-12-30 17:30:16

问题


I need to built a list of radio buttons, based on data I return from my DB. Each button needs to have a value associated with it that I can get out based on the selected button.

Ideally I would just use the RadioButtonList control, however, I need to have a very custom layout which a RadioButtonList doesn't appear to be able to handle.

An alternative would be to create individual RadioButtons and wrap them in a Panel to group them. However, there doesn't appear to be a Value property on a RadioButton?

Is there an alternative way to set a value to a RadioButton control? Alternatively, a way to completely customise the RadioButtonList output.

At the moment, I'm thinking I might have to resort to using HTML radio buttons with runat="server", must be a better way...?


回答1:


You could create your own radio button class which extends the standard one and adds a value property:

public class ValueCheckBox : System.Web.UI.WebControls.RadioButton
{
    public string Value { get; set; }
}



回答2:


You can always try using attributes to save the associated value. eg)

radioButton.Attributes.Add("Key", "Value");

Set the Group property to be the same for all the radio buttons and you should be good to go. Just remember, ASP .Net has a slight problem if these individual radio buttons are in different rows of a repeater, gridview or some such grid-style.




回答3:


For a quick and dirty set of STATIC radio buttons.
I used the Tag field in the Properties window to manually define a value.

If you are using a DB you should probably bind your data to it. You never know when you'll change a key or name.




回答4:


RadioButton control doesn't have Value property, that's right. You have to use Checked instead.



来源:https://stackoverflow.com/questions/9368353/set-value-property-of-radiobutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!