简体   繁体   中英

How to set to select drop down list from back end C# code?

I have one DropDownList and Button on my page.

After I select the dropdownlist and click the button, it will redirect to same page, and It has to show in dropdownlist what I select before redirect

Here is my code

 protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //ASPxGridView1.Visible = false;
        }
        else
        {
            if(Request.QueryString["ReqID"] != null)
                ddlRequestNo.SelectedValue = Request.QueryString["ReqID"].ToString();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("GPApproveCheque.aspx?ReqID="+ddlRequestNo.SelectedItem.Text.ToString());
    }

When I see the url, It is showing what is choose on first time

for example

On my drop down list if I have value

111
222
333
444

1.Once I select 222 , I can see in the url bar like below

http://localhost:55047/GPApproveCheque.aspx?ReqID=222

2.Still drop down list is showing 111

3.When I select second or third time a different options from dropdown, it will show in the url the same old one 222 and dropdownlist never change 111

Update

When I try this below code, it throws System.NullReferenceException

ddlRequestNo.SelectedIndex = ddlRequestNo.Items.IndexOf(ddlRequestNo.Items.FindByText(Request.QueryString["ReqID"].ToString())); 

Which it returns null for ReqID . How it is possible, Because, I am passing parameter like "GPApproveCheque.aspx?ReqID="+ddlRequestNo.SelectedItem.Text.ToString()

Page load function execute before it receive parameter

Your Page_Load should be as below because you are redirecting to page on button click and so page loads again and your value assignment should be done in !IsPostback block.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        if(Request.QueryString["ReqID"] != null)
            ddlRequestNo.SelectedValue = Request.QueryString["ReqID"].ToString();
    }
}
<asp:DropDownList runat="server" ID="ddlRequestNo" AutoPostBack="true">
        <asp:ListItem Text="111" />
        <asp:ListItem Text="222" />
        <asp:ListItem Text="333" />
        <asp:ListItem Text="444" />
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
        {
            ddlRequestNo.SelectedIndexChanged += DdlRequestNo_SelectedIndexChanged;
        }
private void DdlRequestNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri+ "?ReqID=" + ddlRequestNo.SelectedItem.Text.ToString());
        }
protected void btnSearch_Click(object sender, EventArgs e)
{
            ddlRequestNo.SelectedIndex = ddlRequestNo.Items.IndexOf(ddlRequestNo.Items.FindByText(Request.QueryString["ReqID"].ToString()));
    Response.Redirect("GPApproveCheque.aspx?ReqID="+ddlRequestNo.SelectedItem.Text.ToString());

        }

You can try the same method Shah has recommended but if Query string is causing an issue you can change it to using ViewState to store the drop-down list selection and then rebind it on PageLoad.

Replace Request.QueryString["ReqID"] with ViewState["ReqID"] and reassign it on postback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM