0

I'm using an ASP.NET drop-down box and have hooked up a javascript function to "OnChange" event.

The function actually redirects the control back to the page load function in server, and refreshes the page. I'm not supposed to set the auto-post back property to true.

I'd like to make sure that the drop-down holds the selected value after the page has been refreshed.

Should I use any hidden variables or is there a property in asp.net that can help me achieve this ?

Any help would be much appreciated. Thanks

My code would look something like this..

<asp:DropDownList ID="CountriesDropDown" Width="100" runat="server" OnChange = "Send(this);"/>

function Send(dropdown)
{
//Should I write any code here ? 
refreshpage(); //separately implemented to redirect the url back to server
}

In server side

PageLoad()
{
CountriesDropDown.DataSource = CountriesList;
ContriesDropDown.DataBind();

//are there any properties that I could use so that the drop-down retains the selected value ?


}
4

3 回答 3

0

我应该使用任何隐藏变量还是 asp.net 中有一个属性可以帮助我实现这一点?

是的。Viewstate 在 ASP.Net 中可帮助您实现这一目标。但是,你不想使用它!

您现在可以做的是在您的函数refreshpage通过onchange处理程序中附加一个包含所选选项的查询字符串:

function Send($dropdown) {
    //Should I write any code here ? 
    refreshpage(dropdown); //separately implemented to redirect the url back to server
}

function refreshpage($dropdown) {
    window.location = "MyPage.aspx?value=" + $dropdown.val();
}

现在在您的代码隐藏页面加载中,首先检查一个名为“value”的查询字符串参数是否可用。如果是,则在数据绑定后将其设置为您选择的值。

MyAspDropDown.DataBind()
MyAspDropDown.SelectedValue = Request.Querystring("value")
于 2013-07-30T08:46:03.187 回答
0

根据您刷新页面的方式:

  • GET :您必须将所选值添加到您的 URL 参数
  • POST :您应该使用隐藏字段并在 Request.Params[yourHiddenField.UniqueID] 中取回其值

在绑定下拉列表后执行此操作。

于 2013-07-30T08:42:12.850 回答
-1

尝试使用更新面板控件。

请查看http://msdn.microsoft.com/en-us/library/bb399001(v=vs.100).aspx

于 2013-07-30T08:41:53.413 回答