我有一个不可能的时间从aspx.cs文件获得一个单选按钮列表控件的选定值。radiobuttonlist控件位于.ascx文件中。我一直在搞系统。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。我记得在过去为一些事情创建了一个公共财产。今晚写了这么多代码还不休息,我简直无法思考,而且已经很晚了。这很有帮助。和平,兄弟。和平奖学生。您可以在用户控件中定义一个返回selected
值的public
属性/方法。
在用户控件后面的代码中添加以下属性,
public string SelectedValue
{
get
{
return RadioButtonList1.SelectedValue;
}
}
要从.aspx页面中访问SelectedValue属性,
string value=YourControlID1.SelectedValue;
或者使用FindControl方法,
RadioButtonList rad = (RadioButtonList)YourControlID1.FindControl("RadioButtonList1");
Response.Write(rad.SelectedValue);
您可以通过以下方式访问RadioButtonList
UserControlName.FindControl("RadioButtonList1");