c# - 如何从 aspx.cs 文件访问用户控件的单选按钮列表

标签 c# asp.net ascx

我无法从 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/

相关文章:

c# - File.Exists() 在 IIS 上总是返回 false

c# - 在 asp.net ascx 中使用变量 DataFormatString

asp.net - 如何在VS中为ASPX运行ASP.NET验证?

c# - Windows 10 x :Bind to SelectedItem

c# - 网页生命周期是什么意思?

c# - 在 C# 中尝试/捕获所有异常

c# - 使用 Asp.net 核心创建另一个 web api 的代理

jquery - 为什么 Validator jQuery 插件不使用此配置进行验证?

c# - 在具有约束的目录中搜索文件

c# - 您如何让 XAML 元素缩放以适合其容器?