从 RadioButtonList 子类中获取 SelectedValue
我对 RadioButtonList 控件进行了子类化,以便创建一个控制适配器,该适配器可以完全按照我想要的方式输出 HTML(这是我第一次编写适配器)。
我面临的问题是访问“SelectedValue”属性时,我总是得到一个空字符串。如果我切换到超类,我会得到正确的值,所以我真的很困惑......我在这里缺少什么?
我的子类再简单不过了:
namespace Internet.Webapp.Controls
{
public class RadioButtonList : System.Web.UI.WebControls.RadioButtonList
{
}
}
我的适配器也非常简单:
public class RadioButtonListAdapter : System.Web.UI.Adapters.ControlAdapter
{
// Return a strongly-typed reference
public new Internet.Webapp.Controls.RadioButtonList Control
{
get
{
return (Internet.Webapp.Controls.RadioButtonList) base.Control;
}
}
protected override void Render(HtmlTextWriter writer)
{
RadioButtonList radioButtonList = Control;
writer.WriteBeginTag("div");
writer.WriteAttribute("id", radioButtonList.ClientID);
writer.Write(HtmlTextWriter.TagRightChar);
var counter = 0;
foreach (ListItem item in radioButtonList.Items)
{
writer.WriteFullBeginTag("div");
var itemId = radioButtonList.ClientID + "_" + counter++;
writer.WriteBeginTag("label");
writer.WriteAttribute("for", itemId);
writer.WriteAttribute("value", item.Text);
writer.WriteAttribute("class", "long");
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteBeginTag("input");
writer.WriteAttribute("type", "radio");
writer.WriteAttribute("id", itemId);
writer.WriteAttribute("name", radioButtonList.UniqueID);
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEndTag("input");
writer.Write(item.Text);
writer.WriteEndTag("label");
writer.WriteEndTag("div");
}
writer.WriteEndTag("div");
}
}
填充单选按钮列表的代码如下:
var radioButtonList = new Controls.RadioButtonList();
optionsList.ForEach(option => radioButtonList.Items.Add(option));
生成的 html:
<div id="ctl00_MainSection_CordaanForm_ctl14">
<div>
<label for="ctl00_MainSection_CordaanForm_ctl14_0" value="male" class="long">
<input type="radio" id="ctl00_MainSection_CordaanForm_ctl14_0" name="ctl00$MainSection$CordaanForm$ctl14"></input>
male
</label>
</div>
<div>
<label for="ctl00_MainSection_CordaanForm_ctl14_1" value="female" class="long">
<input type="radio" id="ctl00_MainSection_CordaanForm_ctl14_1" name="ctl00$MainSection$CordaanForm$ctl14"></input>
female
</label>
</div>
</div>
I have subclassed the RadioButtonList control in order to create a Control Adapter that outputs the HTML exactly the way I want (it is the first time I write an adapter).
The problem that I am facing is when accessing the "SelectedValue" property, I always get an empty string. If I switch to the superclass I get the proper value, so I am really puzzled... What am I missing here?
My subclass couldn't be simpler:
namespace Internet.Webapp.Controls
{
public class RadioButtonList : System.Web.UI.WebControls.RadioButtonList
{
}
}
My adapter is also very simple:
public class RadioButtonListAdapter : System.Web.UI.Adapters.ControlAdapter
{
// Return a strongly-typed reference
public new Internet.Webapp.Controls.RadioButtonList Control
{
get
{
return (Internet.Webapp.Controls.RadioButtonList) base.Control;
}
}
protected override void Render(HtmlTextWriter writer)
{
RadioButtonList radioButtonList = Control;
writer.WriteBeginTag("div");
writer.WriteAttribute("id", radioButtonList.ClientID);
writer.Write(HtmlTextWriter.TagRightChar);
var counter = 0;
foreach (ListItem item in radioButtonList.Items)
{
writer.WriteFullBeginTag("div");
var itemId = radioButtonList.ClientID + "_" + counter++;
writer.WriteBeginTag("label");
writer.WriteAttribute("for", itemId);
writer.WriteAttribute("value", item.Text);
writer.WriteAttribute("class", "long");
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteBeginTag("input");
writer.WriteAttribute("type", "radio");
writer.WriteAttribute("id", itemId);
writer.WriteAttribute("name", radioButtonList.UniqueID);
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEndTag("input");
writer.Write(item.Text);
writer.WriteEndTag("label");
writer.WriteEndTag("div");
}
writer.WriteEndTag("div");
}
}
The code to populate the radio button list is the following:
var radioButtonList = new Controls.RadioButtonList();
optionsList.ForEach(option => radioButtonList.Items.Add(option));
And the generated html:
<div id="ctl00_MainSection_CordaanForm_ctl14">
<div>
<label for="ctl00_MainSection_CordaanForm_ctl14_0" value="male" class="long">
<input type="radio" id="ctl00_MainSection_CordaanForm_ctl14_0" name="ctl00$MainSection$CordaanForm$ctl14"></input>
male
</label>
</div>
<div>
<label for="ctl00_MainSection_CordaanForm_ctl14_1" value="female" class="long">
<input type="radio" id="ctl00_MainSection_CordaanForm_ctl14_1" name="ctl00$MainSection$CordaanForm$ctl14"></input>
female
</label>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ASP.NET 通过 ID 来识别控件。您在重写 Render 方法时更改了该 ID,因此框架无法再识别它。
ASP.NET identifies controls by their ID. You have changed that ID in your override of the Render method, so the framework can't recognise it anymore.