无法隐藏和显示面板

时间:2014-12-12 05:40:53

标签: c# asp.net

页面加载时,我有两个面板Panel1panel2,两个面板都被隐藏。我有一个下拉列表,当我选择下拉值时,面板应该可见。现在根据我的下拉选择不可见。我的页面加载

 protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
         {
            Panel1.Visible = false;
            Panel2.Visible = false;
         }

    }

下拉点击功能

 protected void ddmode_SelectedIndexChanged(object sender, EventArgs e)
    {  
        if (ddmode.SelectedItem.Value == "4")
        {
            Panel1.Visible = true;
            Panel2.Visible = false; 

        }
    }

但是现在当我选择值为4的下拉列表时,panel1不可见。我的UI部分

 <tr>
 <td align="left" class="style2">
 Mode</td>
 <td align="left">
 <asp:UpdatePanel ID="UpdatePanel4" runat="server">
 <ContentTemplate >
 <div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
 <asp:DropDownList ID="ddmode" runat="server" AutoPostBack="True" 
  onselectedindexchanged="ddmode_SelectedIndexChanged"> 
 </asp:DropDownList>
 <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
 ErrorMessage="RequiredFieldValidator" ControlToValidate="ddmode" ForeColor="Red">*
 </asp:RequiredFieldValidator>
 </ContentTemplate>
 </asp:UpdatePanel>
 </td>
 </tr>
 <asp:Panel ID="Panel1" runat="server">
 <tr>
 <td align="left" class="style2">
 <asp:Label ID="lblfromdate" runat="server" Text="From Date"></asp:Label>
 </td>
 <td align="left">
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
 <ContentTemplate>
 <div class="input-prepend" title="Select Date" data-rel="tooltip">
 <asp:TextBox ID="txtfromdate" ClientIDMode="Static" runat="server" 
 TextMode="SingleLine" ></asp:TextBox>
 <ajaxToolkit:CalendarExtender ID="CalendarExtender2" TargetControlID ="txtfromdate"
 Format="dd/MM/yyyy" runat="server">
 </ajaxToolkit:CalendarExtender>  
 <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" 
                                        ErrorMessage="RequiredFieldValidator" 
 ControlToValidate="txtfromdate" ForeColor="Red">*</asp:RequiredFieldValidator>
 </ContentTemplate></asp:UpdatePanel>
 </td>
 </tr>
 <tr>
 <td align="left" class="style2">
 <asp:Label ID="lbltodate" runat="server" Text="To Date"></asp:Label>
 </td>
 <td align="left">
 <asp:UpdatePanel ID="UpdatePanel11" runat="server">
 <ContentTemplate>
 <div class="input-prepend" title="Select Date" data-rel="tooltip">
 <asp:TextBox ID="txttodate" ClientIDMode="Static" runat="server" 
 TextMode="SingleLine" ></asp:TextBox>
 <ajaxToolkit:CalendarExtender ID="CalendarExtender3" TargetControlID ="txttodate"
 Format="dd/MM/yyyy" runat="server">
 </ajaxToolkit:CalendarExtender>  
 <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" 
                                        ErrorMessage="RequiredFieldValidator"
 ControlToValidate="txttodate" ForeColor="Red">*</asp:RequiredFieldValidator>
                                   </ContentTemplate></asp:UpdatePanel>
 </td>
 </tr>
 </asp:Panel>
 <asp:Panel ID="Panel2" runat="server">
 <tr>
 <td align="left" class="style2">
 <asp:UpdatePanel ID="UpdatePanel14" runat="server">
 <ContentTemplate>
 <asp:Label ID="lbldept" runat="server" Text="From Department" ></asp:Label>
 </ContentTemplate></asp:UpdatePanel>
 </td>
 <td align="left">
 <asp:UpdatePanel ID="UpdatePanel12" runat="server">
 <ContentTemplate>
 <div class="input-prepend" title="Select Date" data-rel="tooltip">
 <asp:TextBox ID="txtfromdept" ClientIDMode="Static" runat="server" 
 TextMode="SingleLine" ></asp:TextBox>
 </ContentTemplate></asp:UpdatePanel>
 </td>
 </tr>
 <tr>
 <td align="left" class="style2">
 <asp:UpdatePanel ID="UpdatePanel15" runat="server"><ContentTemplate>
 <asp:Label ID="lbltodept" runat="server" Text="To Deprtment" ></asp:Label>
 </ContentTemplate></asp:UpdatePanel>
 </td>
 <td align="left">
 <asp:UpdatePanel ID="UpdatePanel13" runat="server">
 <ContentTemplate>
 <div class="input-prepend" title="Select Date" data-rel="tooltip">
 <asp:TextBox ID="txttodept" ClientIDMode="Static" runat="server" 
 TextMode="SingleLine"></asp:TextBox>
 </ContentTemplate></asp:UpdatePanel>
 </td>
 </tr>
</asp:Panel>

2 个答案:

答案 0 :(得分:1)

在这种情况下,看起来您的下拉列表在updatepanel4中进行了部分回发,但Panel1不在更新面板中,因此无法在部分回发中更新。删除所有updatePanel或将Panel1放在更新面板中。

答案 1 :(得分:0)

您需要将面板放在更新面板中才能使其正常工作。外部更新面板控件无法用于更新面板内的控件事件。

<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate >
<div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
<asp:DropDownList ID="ddmode" runat="server" AutoPostBack="True" 
 onselectedindexchanged="ddmode_SelectedIndexChanged"> 
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
ErrorMessage="RequiredFieldValidator" ControlToValidate="ddmode" ForeColor="Red">*
</asp:RequiredFieldValidator>

<asp:Panel ID="Panel1" runat="server">
....
</asp:Panel>
 </ContentTemplate>
 </asp:UpdatePanel>