2012-03-27 48 views
0

我創建了一個下拉列表和一個與日曆擴展程序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; 
    } 
} 

回答

1

或者你可以用客戶端代碼與<select>做到這一點並更換<asp:DropDownList>。然後,您可以使用jQuery將事件處理程序附加到並在選擇更改時觸發一個函數。

簡單的例子:

在後面或網頁的部分中的JavaScript代碼:

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#mySelect").bind("change", function() { 
      var val = $(this).val(); 
      alert("Selection was " + val); 
     }); 
    }); 
</script> 

那麼,你想要的下拉列表中呈現:

<select id="mySelect"> 
    <option value="1">a</option> 
    <option value="2">b</option> 
    <option value="3">c</option> 
</select> 

我堅信不使用服務器端資源來呈現可以寫成客戶端代碼的html。那<asp:DropDownList>將剛剛得到呈現爲幾乎相同的HTML首先使用<select>

該頁面上的所有html元素都可以使用html標記來代替將被轉換爲html的asp標記。對使用jQuery進行客戶端代碼的事件處理做一點研究。它將改變您對Web應用程序編程的看法。

0

您可以使用JQuery做到這一點。胡克的變化處理程序下拉列表並讓它處理的知名度和文本分配..

相關問題