简体   繁体   English

Dropdownlist回发中的选定值

[英]Dropdownlist Selected value in postback

I have 2 dropdownlists(ddl1,ddl2) and a gridview with 2 dropdown lists(gddl1,gddl2). 我有2个dropdownlists(ddl1,ddl2)和一个带有2个下拉列表(gddl1,gddl2)的gridview。 On SelectedIndexChanged event of ddl1 am changing SelectedIndex of gddl1 in postback. 在ddl1的SelectedIndexChanged事件上,正在回发中更改gddl1的SelectedIndex

My problem is ddl1.databind() occurs at a button's click event . 我的问题是ddl1.databind()发生在按钮的click事件中 So once selectedindex of ddl1 changes, the selected value losts and returns back to initial value. 因此,一旦ddl1的selectedindex更改,所选值就会丢失并返回到初始值。

I cant use !IsPostback because am binding ddl1 on button click. 我不能使用!IsPostback因为在单击按钮时绑定了ddl1。 How can I retain ddl1 and ddl2 selected index.? 如何保留ddl1和ddl2选择的索引。

protected void btnProceed_Click(object sender, EventArgs e)
{
    if(ddlLocation.SelectedIndex > -1) {
        empDS = ws_service.GetEmpList(ddlLocation.SelectedValue, ((ddlDept.SelectedValue == "All") ? "" : ddlDept.SelectedValue), ((ddlGrade.SelectedValue == "All") ? "" : ddlGrade.SelectedValue));
        appraiserDS = ws_service.GetAppList();
        grdDetails.DataSource = empDS.Tables[ 0 ].DefaultView;
        grdDetails.DataBind();

        ddlAppraiserAll.DataSource = appraiserDS.Tables[ 0 ].DefaultView;
        ddlAppraiserAll.DataTextField = "APPRAISER_NAME";
        ddlAppraiserAll.DataValueField = "APPRAISER_ID";
        ddlAppraiserAll.DataBind();
    }
}


protected void ddlAppraiserAll_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach(GridViewRow gvRow in grdDetails.Rows) {
        Control ctrl = gvRow.FindControl("ddlAppraiserId");
        DropDownList ddl = ctrl as DropDownList;
        if(ddl != null)
            ddl.SelectedIndex = ddlAppraiserAll.SelectedIndex;
    }
}

这里的问题是同步及其在何处以及在何处进行绑定,但是您也可以使用Request.Form直接获取值。

Request.Form[DropDownListID.UniqueID]

I'm not sure if i understood your problem since it's difficult to see what's ddl1 , ddl2 , gddl1 and so on and when each event is handled. 我不确定我是否理解您的问题,因为很难看到ddl1ddl2gddl1等是什么以及何时处理每个事件。

But my guess is: 但是我的猜测是:

  • DataBind your GridView in btnProceed_Click btnProceed_Click您的GridView数据btnProceed_Click
  • Bind the two DropDownLists of the GridView only in RowDataBound 仅在RowDataBound中绑定GridView的两个DropDownList

Then your "GridView-DropDownLists" are always up-to-date according to the selected value of ddl1 然后,根据选择的ddl1值,您的“ GridView-DropDownLists”始终是ddl1

you can retain ddl1 and ddl2 selected index by storing them in viewstate as properties. 您可以通过将ddl1和ddl2所选索引存储在viewstate中作为属性来保留它们。

        private string ddlSelectedIndex
    {
        set { ViewState["SelectedIndex"] = value; }
        get { return ViewState["SelectedIndex"] == null ? string.Empty : ViewState["SelectedIndex"].ToString(); }
    }

The above property is in string, you can create an int property in similar way or use the same and cast index as string. 上面的属性是字符串形式的,您可以以类似的方式创建一个int属性,或者使用相同的类型和类型的索引作为字符串。 Your selected index will be retained on subsequent postbacks. 您选择的索引将保留在以后的回发中。

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

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