当我更改DropDownList选择时,下面的代码在这行href.NavigateUrl = "foo.aspx?id=" + id;
上产生空引用错误,但当我输入id作为QueryString参数时不会。这似乎与事件的顺序有关,但我不确定是什么,也不知道如何解决它。
是
<asp:Panel ID="Panel1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
</asp:Panel>
<br />
<asp:Panel ID="Panel2" runat="server" Visible="false">
<asp:LoginView ID="LoginView1" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="superadmin">
<ContentTemplate>
<asp:HyperLink runat="server" ID="HyperLink1">HyperLink</asp:HyperLink>
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
</asp:Panel>
.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null)
{
if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
Panel1.Visible = false;
SetHref(Request.QueryString["id"]);
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SetHref(DropDownList1.SelectedValue);
}
protected void SetHref(string id)
{
Panel2.Visible = true;
HyperLink href = (HyperLink)LoginView1.FindControl("HyperLink1");
href.NavigateUrl = "foo.aspx?id=" + id;
href.Text = href.NavigateUrl;
}
我发现了一些解决方法:将Panel2的默认可见性设置为true是一种,将Panel2的超链接移到Panel2之外并直接改变其可见性是另一种,但这两种方法都不是我想要的,因为我正在尝试使超链接可见/不可见的其他控件。
任何想法吗?
而不是使用以下行:
Panel2.Visible = true;
尝试使用:
Panel2.Attributes["style"] = "display:none";
我认为当你将Visible
设置为false
时,面板的内容可能不会呈现