2012-03-27 49 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做到这一点。胡克的变化处理程序下拉列表并让它处理的知名度和文本分配..

相关问题