【问题标题】:How to get argument in Page_Load method?如何在 Page_Load 方法中获取参数?
【发布时间】:2016-04-30 04:32:12
【问题描述】:

我有这个控制:

<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 postBack 中进行选择并触发此功能时:

        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 方法的下拉列表中获取选定项目?

【问题讨论】:

    标签: c# asp.net pageload asp.net-controls


    【解决方案1】:

    我建议您不要在 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      • 2010-09-18
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      相关资源
      最近更新 更多