简体   繁体   English

如何设置从后端C#代码中选择下拉列表?

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

I have one DropDownList and Button on my page. 我的页面上有一个DropDownListButton

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 1.一旦我选择222 ,我可以在网址栏中看到如下

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

2.Still drop down list is showing 111 2.Still下拉列表显示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 3.当我从下拉列表中选择第二次或第三次不同的选项时,它将在URL中显示相同的旧选项222并且下拉列表永远不会更改111

Update 更新

When I try this below code, it throws System.NullReferenceException 当我在下面的代码中尝试这个时,会抛出System.NullReferenceException

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

Which it returns null for ReqID . 它为ReqID返回null。 How it is possible, Because, I am passing parameter like "GPApproveCheque.aspx?ReqID="+ddlRequestNo.SelectedItem.Text.ToString() 怎么可能,因为,我传递的参数如"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. 您的Page_Load应如下所示,因为您在按钮单击时重定向到页面,因此页面再次加载,您的值分配应在!IsPostback块中完成。

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. 您可以尝试Shah推荐的相同方法,但如果查询字符串导致问题,您可以将其更改为使用ViewState存储下拉列表选择,然后在PageLoad上重新绑定它。

Replace Request.QueryString["ReqID"] with ViewState["ReqID"] and reassign it on postback 用ViewState [“ReqID”]替换Request.QueryString [“ReqID”]并在回发时重新分配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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