-1

我的 aspx 页面中有一个下拉列表,其值为:

//DropDownlist value: 1--> show notice in one day ago; 2--> 7 days ago;3--> 30 days ago.

<asp:DropDownList ID="DropDownListTime" runat="server">
                    AutoPostBack="true" >
   <asp:ListItem Selected="True"></asp:ListItem>
   <asp:ListItem Value="1"> 1 day ago  </asp:ListItem>
   <asp:ListItem Value="2"> 7 days ago </asp:ListItem>
   <asp:ListItem Value="3"> 30 days ago </asp:ListItem>
</asp:DropDownList> 

和cs页面中的代码:

protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
     {
         BindData();
     }              
}

public void BindData()
{
   string key="";
   if (string.IsNullOrEmpty(DropDownListTime.SelectedValue))
   {
       key = "3";
   }
   else
   {
       key = DropDownListTime.SelectedValue.ToString();
   }

   HyperLink1.NavigateUrl = string.Format("Allnotice.aspx?key={0}",key);
  // go to page to show all notices with `1 day`,`7days`,`30 days` ago depend on the `key`
}

public  void IndexNotice_Changed(Object sender, EventArgs e)
{
   BindData();
}

调试的时候,关键就在我选择的那个选项上。但是在下拉列表中选择一个选项后,我单击超链接,它通过Allnotice.aspxkey="3" 传递到页面。总是而且总是甚至我选择了什么选项。

有关详细信息:我选择7 days ago---> 调试:key= 2-> 然后单击超链接 ---> 下一页收到了key=3.

帮助!!!!

更新:我已经问过这个问题,但没有人可以提供帮助。所以我试着用简单的方式来描述它,如果它是重复的,希望你不要介意。

DropDownList 不会改变它的值

4

2 回答 2

0

您尚未为 DropdownList 控件分配事件处理程序,因此即使页面正在回发,它也不会IndexNotice_Changed像您期望的那样触发事件。

//Assign IndexNotice_Changed event to the OnSelectedIndexChanged 
<asp:DropDownList ID="DropDownListTime" 
                  Runat="server" AutoPostBack="true" 
                  OnSelectedIndexChanged="IndexNotice_Changed" > //NOTE HERE
     <asp:ListItem Selected="True"></asp:ListItem>
     <asp:ListItem Value="1"> 1 day ago  </asp:ListItem>
     <asp:ListItem Value="2"> 7 days ago </asp:ListItem>
     <asp:ListItem Value="3"> 30 days ago </asp:ListItem>
</asp:DropDownList>

<asp:HyperLink ID="HyperLink1" runat="server"></asp:HyperLink>
于 2013-10-05T17:28:31.613 回答
0

一个选项可能是在 javascript 中使用 hiddenfield 值来始终保存选定的选项

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js' type='text/javascript'></script>
<script type="text/javascript">
$("#<%=DropDownListTime.ClientID%>").change(function(e) {
    var dpdVal=$("#<%=DropDownListTime.ClientID%>").val();
    document.getElementById("<%=hiddenFieldControl.ClientID%>").value=dpdVal;
 });
</script>

因此,您可以为此替换:

public void BindData()
{
   string key="";
   if (string.IsNullOrEmpty(hiddenFieldControl.Value))
   {
       key = "3";
   }
   else
   {
       key = hiddenFieldControl.Value.ToString();
   }

   HyperLink1.NavigateUrl = string.Format("Allnotice.aspx?key={0}",key);
  // go to page to show all notices with `1 day`,`7days`,`30 days` ago depend on the `key`
}
于 2013-10-05T17:53:58.407 回答