2016-01-23 63 views
2

我有這樣的控制:如何在Page_Load方法中獲取參數?

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

在這裏,我如何在服務器端的數據綁定到上面的控制:

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

當我做出選擇的DropDownList的回發應計及此功能被激發:

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

當Page_Load方法被激發時,我需要在下拉列表中獲取所選項目。

我試着這樣說:

 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"); 

但結果是空的。

雖然當我得到控制的ID是這樣的:

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

它的作品完美!

所以我的問題是,如何在Page_Load方法中的下拉列表中選擇項目?

預先感謝您。

回答

2

我建議你不要在Page_Load中這樣做。在DropDownList類中有一個SelectedIndexChanged事件正是爲了這個。

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

然後在你的代碼隱藏:

protected void MyEventHandler(object sender, EventArgs e) 
{ 
    var selectedId = _ddlMyDdl.SelectedIndex; // or ((DropDownList) sender).SelectedIndex 
} 
相關問題