1

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

3 Answers 3

3

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
<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());

        }

1 Comment

it throws nullreference Exception bro
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.