我无法从 aspx.cs 文件中获取单选按钮列表控件的选定值。 radiobuttonlist 控件位于 .ascx 文件中。我不断收到 System.NullReferenceException 对象引用未设置为对象的实例。
有什么想法吗?
我尝试多次使用 FindControl 方法更改它,因为它会失败。这是我尝试的最后一件事:
protected void ClientsDropDownList_Selected(object sender, EventArgs e)
{
this.ConsultationFormControl.LoadClient(int.Parse(ClientsDropDownList.SelectedValue));
if (ClientsDropDownList.SelectedValue != "Please Select One")
{
UserControl US = FindControl("ConsultationFormControl") as UserControl;
RadioButtonList rblMarStat = US.FindControl("rblMaritalStatus") as RadioButtonList;
if (rblMarStat.SelectedValue == "Married")
{
Response.Write("perfect");
}
}
}
希望这对您有所帮助。
詹姆斯
好的,伙计们,谢谢你的帮助。看起来我们成功了。再次感谢,AVD。我记得过去曾为一些事情创建公共(public)属性(property)。在所有这些不间断的编码之后,我今晚无法思考而且已经很晚了。这很有帮助。 和平,兄弟。和平伙伴。
最佳答案
您可以在用户控件中定义一个 public
属性/方法,返回一个 selected
值。
编辑:
在用户控件后面的代码中添加如下属性,
public string SelectedValue
{
get
{
return RadioButtonList1.SelectedValue;
}
}
要从 .aspx 页面访问 SelectedValue 属性,
string value=YourControlID1.SelectedValue;
或使用 FindControl 方法,
RadioButtonList rad = (RadioButtonList)YourControlID1.FindControl("RadioButtonList1");
Response.Write(rad.SelectedValue);
关于c# - 如何从 aspx.cs 文件访问用户控件的单选按钮列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7818240/