2

I have this control:

<asp:DropDownList ID="ddlPaging" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPaging_SelectedIndexChanged">
    </asp:DropDownList>

Here how I bind the data on server side to the control above:

    ddlPaging.Visible = true;
    ddlPaging.DataSource = Enumerable.Range(0, features.Count()).ToList();
    ddlPaging.DataBind();

when I make selection in DropDownList postBack accrued and this function is fired:

        protected void Page_Load(object sender, EventArgs e)
        {
            string controlId= this.FindControl(this.Request.Params.Get("__EVENTTARGET")).ID

        //always empty
        string ctrlarg1 = this.Request.Params.Get("__EVENTARGUMENT");
        string ctrlarg2 = Request.Form["__EVENTARGUMENT"];
        string ctrlarg3 = Request.Params["__EVENTARGUMENT"];
        string ctrlarg4 = this.Request["__EVENTARGUMENT"];
        string ctrlarg5 = this.Request.Params.Get("__EVENTARGUMENT");

        if (!isPaging)
        {
                ddlPaging.Visible = true;
                ddlPaging.DataSource = Enumerable.Range(0, features.Count()).ToList();
                ddlPaging.DataBind();
        }
}

When Page_Load method is fired I need to get the selected item in dropdownlist.

I try this way:

        string ctrlarg1 = this.Request.Params.Get("__EVENTARGUMENT");
        string ctrlarg2 = Request.Form["__EVENTARGUMENT"];
        string ctrlarg3 = Request.Params["__EVENTARGUMENT"];
        string ctrlarg4 = this.Request["__EVENTARGUMENT"];
        string ctrlarg5 = this.Request.Params.Get("__EVENTARGUMENT");

but the result is empty.

While when I get ID of control this way:

            string controlId= this.FindControl(this.Request.Params.Get("__EVENTTARGET")).ID

it works perfect!

So my question is, how do I get selected item in dropdownlist in Page_Load method ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Michael
  • 13,950
  • 57
  • 145
  • 288

1 Answers1

2

I'd recommend you DON'T do this in Page_Load. There is a SelectedIndexChanged event on the DropDownList class meant precisely for this.

<asp:DropDownList runat="server"
                  ID="_ddlMyDdl"
                  AutoPostBack="True"
                  OnSelectedIndexChanged="MyEventHandler"/>

And then in your codebehind:

protected void MyEventHandler(object sender, EventArgs e)
{
    var selectedId = _ddlMyDdl.SelectedIndex; // or ((DropDownList) sender).SelectedIndex
}
sara
  • 3,521
  • 14
  • 34