ASP.NET 切换控制隐藏代码的可见性

发布于 2024-12-12 09:58:19 字数 1082 浏览 0 评论 0原文

我有两个问题,我不确定是否相关:

我有两个 DropDownList 控件(最初设置为不可见)和一个 RadioButtonList 控件,其自动回发行为设置为 true。

每当回发发生时,我想从 RadioButtonList 控件中读取 SelectedValue 属性 - 并根据该属性,使 DropDownList 之一可见。

这是我的问题:

1)我无法通过 ID 直接引用 RadioButtonList 控件。 Designer.cs 文件似乎没有生成 RadioButtonList 或 DropDownList 控件。即使我手动将控件添加到 Designer.cs 文件中,它们也会在重新生成时丢失。这是预期的行为吗?

2)我尝试在 Page_Load() 方法中使用 Page.FindControl 属性。

if(Page.PostBack==true)
{
    RadioButtonList rbl = (RadioButtonList)Page.FindControl("RadioButtonList1");
    if(rbl.SelectedValue=="optionA")
    {
         DropDownList ddA = (DropDownList)Page.FindControl("DropDownListA");
         ddA.Visible = true;   
    }
    else
    {
         DropDownList ddB = (DropDownList)Page.FindControl("DropDownListB");
         ddB.Visible=true;
    }
}

但我在 if 条件下收到 NullReferenceException 。

我完全走错路了吗?有人会指导我实现我想做的事情的最佳方法吗?

另外,我该怎么做才能使 Designer.cs 文件加载控件?


编辑:/facepalm

我自己想出来了。我忘记了我正在使用表格内的控件。

一旦我将控件移到表之外,我就可以直接引用 ID。

睡眠不足和咖啡是罪魁祸首。我很抱歉。谢谢您的帮助!

I have two problems that I'm not sure are related:

I have two DropDownList controls (initially set to not visible) and a RadioButtonList control with autopostback behaviour set to true.

Whenever Postback occurs, I want to read the SelectedValue property from the RadioButtonList control - and depending on that, make one of the DropDownLists visible.

Here's my problem:

1) I can't directly refer to the RadioButtonList control by its ID. The designer.cs file doesn't seem to generate either the RadioButtonList or DropDownList controls. Even if I manually add the controls to the designer.cs file, they are lost upon regenerating. Is this expected behaviour?

2) I tried using the Page.FindControl property in the Page_Load() method.

if(Page.PostBack==true)
{
    RadioButtonList rbl = (RadioButtonList)Page.FindControl("RadioButtonList1");
    if(rbl.SelectedValue=="optionA")
    {
         DropDownList ddA = (DropDownList)Page.FindControl("DropDownListA");
         ddA.Visible = true;   
    }
    else
    {
         DropDownList ddB = (DropDownList)Page.FindControl("DropDownListB");
         ddB.Visible=true;
    }
}

But I'm getting a NullReferenceException at the if condition.

Am I completely on the wrong track? Will someone guide me about the best way to achieve what I want to do?

Also, what can I do to make designer.cs file load the Controls?


EDIT: /facepalm

I figured it out myself. I'd forgotten than I'm using the control inside a table.

Once I moved the Control outside the Table, I could refer to the ID directly.

A lack of sleep and coffee to blame. My apologies. Thank you for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好菇凉咱不稀罕他 2024-12-19 09:58:19

Page.FindControl 不是递归的,即如果 dropdownlist1 控件位于其他控件中,它将返回 null。
检查此链接了解详细信息,看看您是否能够正确找到下拉列表。

http://www.mha.dk/post/Recursive-PageFindControl.aspx< /a>

Page.FindControl is not recursive, i.e it'll return null if the dropdownlist1 controls is in some other control.
check this link for detail and see will you be able to find the dropdownlist correctly.

http://www.mha.dk/post/Recursive-PageFindControl.aspx

恏ㄋ傷疤忘ㄋ疼 2024-12-19 09:58:19

您正在为 rb1.SelectedValue 分配一个值,并且应该检查是否找到该控件。

if(rb1.SelectedValue="optionA") 

应该是

if(rb1.SelectedValue != null)
{ 
   if(rb1.SelectedValue == "optionA")
   {
       DropDownList ddA = (DropDownList)Page.FindControl("DropDownListA"); 

       if (ddA != null)
                 ddA.Visible = true;  
   }
} 

You are assigning a value to rb1.SelectedValue and you should check if the control was found.

if(rb1.SelectedValue="optionA") 

should be

if(rb1.SelectedValue != null)
{ 
   if(rb1.SelectedValue == "optionA")
   {
       DropDownList ddA = (DropDownList)Page.FindControl("DropDownListA"); 

       if (ddA != null)
                 ddA.Visible = true;  
   }
} 
放血 2024-12-19 09:58:19

如果您无法通过 ID 引用该控件,并且 FindControl 失败,则可能是因为您的控件在 aspx 文件中缺少 runat="server" 标记。

是这样吗?

If you can't reference the control by its ID, and FindControl fails, it could be that in the aspx file your controls are missing the runat="server" tag.

Is this the case?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文