我創建了一個下拉列表和一個與日曆擴展程序w相對應的下拉列表。根據選擇的不同,下拉列表中的每個值都會對樣式可見性產生不同的影響。就目前而言,功能是有效的;然而,每次嘗試選擇不同的列表項時,它會刷新整個頁面,我不想設置AutoPostBack =「false」。 請讓我知道什麼是解決此問題的最佳方法。當autopostback必須設置爲true(包含代碼)時,無回傳的Dropdownlist
<asp:DropDownList ID="dropdownlist" runat="server" OnSelectedIndexChanged="dropdownlist_SelectedIndexChanged" AutoPostBack="True" >
<asp:ListItem Value="1">a</asp:ListItem>
<asp:ListItem Value="2">b</asp:ListItem>
<asp:ListItem Value="3">c</asp:ListItem>
<asp:ListItem Value="4">d</asp:ListItem>
<asp:ListItem Value="5">e</asp:ListItem>
<asp:ListItem Value="6">f</asp:ListItem>
<asp:ListItem Value="7">g</asp:ListItem>
</asp:DropDownList> <asp:Panel runat="server" ID="StartDate" >
<asp:Label ID="lblStartDate" runat="server" Text="Start Date:"></asp:Label>
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidatorStartDate" runat="server" ErrorMessage="Please enter a validate date" ControlToValidate="txtStartDate" Type="Date" Operator="DataTypeCheck" Display="Static" Font-Italic="False" SetFocusOnError="True"></asp:CompareValidator>
</asp:Panel>
<cc1:CalendarExtender ID="CalendarExtenderStartDate" TargetControlID="txtStartDate" runat="server"></cc1:CalendarExtender>
<asp:Panel runat="server" ID="EndDate" >
<asp:Label ID="lblEndDate" runat="server" Text="End Date:"></asp:Label>
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidatorEndDate" runat="server" ErrorMessage="Please enter a validate date" ControlToValidate="txtEndDate" Type="Date" Operator="DataTypeCheck" Display="Static" Font-Italic="False" SetFocusOnError="True"></asp:CompareValidator>
</asp:Panel>
<cc1:CalendarExtender ID="CalendarExtenderEndDate" TargetControlID="txtEndDate" runat="server"></cc1:CalendarExtender>
代碼隱藏
if (!IsPostBack)
{
SetDateFields();
}
}
protected void dropdownlist_SelectedIndexChanged(object sender, EventArgs e)
{
SetDateFields();
}
private void SetDateFields()
{
switch (dropdownlist.SelectedValue)
{
case "1":
case "3":
case "5":
EndDate.Visible = false;
StartDate.Visible = true;
lblStartDate.Text = "As Of Date:";
break;
case "7":
EndDate.Visible = false;
StartDate.Visible = false;
break;
default:
EndDate.Visible = true;
StartDate.Visible = true;
lblStartDate.Text = "Start Date:";
lblEndDate.Text = "End Date:";
break;
}
}