回发后如何在gridview内设置dropdownlist的选定值? [英] How to set the selected value of dropdownlist inside gridview after postback?

查看:65
本文介绍了回发后如何在gridview内设置dropdownlist的选定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Gridview的项目模板内有一个下拉列表.
下拉列表项是动态添加到Girdview RowDataBound的.
我使用命令字段编辑将下拉列表的选定值更新到数据库中.更新过程很好.我的问题是,当用户选择
时 值,然后单击更新",下拉列表中的所选值将丢失并更改为列表的第一项.我要设置下拉列表的选定值,除非用户更新.请有人帮我设置下拉列表的选定值.

先感谢您.寻找帮助. :)

I have dropdown list inside item template of Gridview.
Dropdown list items are added dynamically at Girdview RowDataBound.
I used the command field Edit to update the selected value of dropdown list into database.Update process is fine. My problem is when user select the
value and click update, the selected value inside the dropdown list is lost and changed to first item of the list. I want to set the selected value of the dropdown list unless user update. Please somebody help me in how to set the selected value of dropdown list.

Thank you in advance. Looking for the help. :)

推荐答案

也许您需要在编辑回发中重新绑定网格-在使用更改更新数据集之后?
Maybe you need to rebind the grid in the edit postback--after you''ve updated the dataset with the change?


private void SetPreviousData()
    {
        int rowindex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
               
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList ddl = (DropDownList)grdRoomDetails.Rows[rowindex].Cells[1].FindControl("ddlRoomCategory");
                    DropDownList ddlRoom = (DropDownList)grdRoomDetails.Rows[rowindex].Cells[2].FindControl("ddlRoom");
                    
                    //Againg binding the previous rows grid COntrols and Retaining their values
                    CategoryBLL obj = new CategoryBLL();
                    DataTable dt1 = new DataTable(); 
                    dt1 = obj.Show_Category();
                    ddl.DataSource = dt1;
                    ddl.DataTextField = "CategoryName";
                    ddl.DataValueField = "CategoryID";
                    ddl.DataBind();
                    ddl.Items.Insert(0, "--Select--");
                    ddl.Items[0].Value = "0";
                    ddl.SelectedValue = dt.Rows[rowindex]["RoomCategory"].ToString();

                    ReservationBLL objres = new ReservationBLL();
                    objres.CategoryID = Convert.ToInt32(ddl.SelectedValue);
                    ddlRoom.DataSource = objres.Show_Room();
                    ddlRoom.DataValueField = "RoomID";
                    ddlRoom.DataTextField = "RoomNo";
                    ddlRoom.DataBind();
                    ddlRoom.Items.Insert(0, "--Select--");
                    ddlRoom.Items[0].Value = "0";
                    ddlRoom.SelectedValue = dt.Rows[rowindex]["Room"].ToString();

                    rowindex++;
                }
            }


在Gridview RowdataBound事件中,动态添加项目之后,找到该下拉列表的选定值,并将selectedindex设置为该值.



在Gridview中,RowdataBound事件伪代码:

In your Gridview RowdataBound event, after you have added the items dynamically, find the selected value for that dropdown and set the selectedindex to that value.



In Gridview RowdataBound event pseudo code:

'code to extract the selected value from the database/dataset/wherever
selectedvalue = getSelectedValue(Primary key) ' change as appropriate

'your code to fill the completed drop down
do while eof
  add the item to dropdown
  if item.value = selectedvalue then  
      item.selected=true ' set selection
  end if
loop


这篇关于回发后如何在gridview内设置dropdownlist的选定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆