页面重定向后,下拉列表选择变为空 [英] dropdownlist selection becoming empty after page redirection

查看:85
本文介绍了页面重定向后,下拉列表选择变为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用母版页的项目。主页上有三个下拉列表,列出了类别。当我选择一个类别时,它会重定向到另一个aspx页面。但是在页面重定向之后,下拉列表的选定值变为空。我希望它保持其状态并将所选类别显示为selectedvalue。

I have a project that uses masterpage. There is three dropdownlists on masterpage that lists categories. When I select a categories, it redirects to another aspx page. But after page redirects, the dropdownlist's selected values becoming empty . I want it to keep its state and show the selected category as selectedvalue.

推荐答案

将此代码放入您的代码中



protected void Page_Load(object sender,EventArgs e)

{



if(!IsPostBack)

{

//在这里调用dropdownlist绑定代码

BindDriopdownlist();

}

希望它能工作
put this in your code

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
// call dropdownlist bind code here
BindDriopdownlist();
}
hope it will work


您可以使用查询字符串发送选定的下拉列表索引,当您重定向时只需在Querysting中查找值,如果您获得值,只需选择下拉列表的值。



只是一个片段供您参考:



You can use querystrings for sending selected index of dropdown and when you redirect just look for the value in Querysting if you get the value just select the value of dropdown.

just a snippet for your reference:

protected void btnSubmitProductFinder_Click(object sender, ImageClickEventArgs e)
        {
           
            var MakerId = drpVehicleMaker.SelectedItem.Value;
          

           
                var strUrl = "";
                strUrl = "productfinder.aspx?MakerId=" + MakerId ;
              
                Response.Redirect(strUrl);
            }
        }







和您的页面加载:






and on your pageload:

if (Request.QueryString["MakerId"] != null)
                       {

                           //drpVehicleMaker
                           try
                           {
                               int drpVehicleMakerId = int.Parse(Request.QueryString["MakerId"].ToString());   
  drpVehicleMaker.Items.FindByValue(drpVehicleMakerId.ToString()).Selected = true;





让我知道它是否有效。



let me know if it works.


当你重定向到新页面时,它将再次加载主页面和内容页面。您需要使用会话,查询字符串等技术将下拉列表的选定值传递到下一页。例如,在您的母版页中执行如下操作,此处会话用于存储值。

When you redirecting to new page it will load both master page and content page again. You need to pass the selected value of drop down list to next page by using techniques like sessions, query strings etc.. for example in your master page do as below, here session is used to store the value.
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["YourDropdown"] != null)
            {
                dropdown.SelectedIndex = (int)Session["YourDropdown"];
            }
        }
    }

    protected void dropdown_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["YourDropdown"] = dropdown.SelectedIndex;
        Response.Redirect("to somewhere");
    }
}


这篇关于页面重定向后,下拉列表选择变为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆