我的Gridview下拉列表是刷新 [英] My Gridview Dropdown list is Refresh

查看:77
本文介绍了我的Gridview下拉列表是刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Gridview DropDownList,它具有DataTextField和DataValueField.

下拉列表---> CMBITem

当我选择项目时,费率"将显​​示在Gridview文本框上.但是每次我选择项目"时,下拉列表都会刷新,已选择的项目"不会显示.

I have Gridview DropDownList, which has DataTextField and DataValueField.

Dropdownlist---> CMBITem

When I Select the Item, The Rate will be display on Gridview TextBox. But everytime the I select the Item the Dropdown list is refresh, Already Selected Item is not display.

protected void CMBItem_SelectedIndexChanged(object sender, EventArgs e)
   {
       DropDownList lb = (DropDownList)sender;
       GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
     int rowID = gvRow.RowIndex;
       if (ViewState["CurrentTable"] != null)
       {
           DataTable dt = (DataTable)ViewState["CurrentTable"];

           connect.cn();
           SqlCommand sqlcmd = new SqlCommand("Get_Item_PurchasePrice", connect.cn());
           sqlcmd.Parameters.AddWithValue("@IMSNO", lb.SelectedValue);
          sqlcmd.CommandType = CommandType.StoredProcedure;
          SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
           DataTable dt1 = new DataTable();
           da.Fill(dt1);
           if (dt1.Rows.Count > 0)
           {
               dt.Rows[rowID][4] = dt1.Rows[0][0].ToString();
               dt.Rows[rowID][5] = dt1.Rows[0][1].ToString();

         }
           ViewState["CurrentTable"] = dt;
           //Re bind the GridView for the updated data
           Gridview2.DataSource = dt;
           Gridview2.DataBind();
       }
     SetPreviousData();
   }


 private void SetPreviousData()
   {

       int rowIndex = 0;

       if (ViewState["CurrentTable"] != null)
       {

           DataTable dt = (DataTable)ViewState["CurrentTable"];

           if (dt.Rows.Count > 0)
           {
               AddGridcombo();
               LoadItemList();

               for (int i = 0; i < dt.Rows.Count; i++)
               {
                  DropDownList box4 = (DropDownList)Gridview2.Rows[rowIndex].Cells[1].FindControl("CMBGodown");
                  DropDownList box7 = (DropDownList)Gridview2.Rows[rowIndex].Cells[1].FindControl("CMBItem");

                   TextBox box1 = (TextBox)Gridview2.Rows[rowIndex].Cells[2].FindControl("txtqty");
                   TextBox box2 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("txtrate");
                   TextBox box3 = (TextBox)Gridview2.Rows[rowIndex].Cells[4].FindControl("txttaxpercent");
                   TextBox box5 = (TextBox)Gridview2.Rows[rowIndex].Cells[5].FindControl("txttaxvalue");
                   TextBox box6 = (TextBox)Gridview2.Rows[rowIndex].Cells[6].FindControl("txtamt");
                   box4.Text = dt.Rows[i]["Column1"].ToString();
                   box1.Text = dt.Rows[i]["Column3"].ToString();
                   box2.Text = dt.Rows[i]["Column4"].ToString();
                   box3.Text = dt.Rows[i]["Column5"].ToString();
                   box5.Text = dt.Rows[i]["Column6"].ToString();
                   box6.Text = dt.Rows[i]["Column7"].ToString();
                   box7.Text = dt.Rows[i]["Column2"].ToString();

                   rowIndex++;


              }

             // dtCurrentTable.Rows.Add(drCurrentRow);
             ViewState["CurrentTable"] = dt;
            //  Gridview2.DataSource = dt;
              //Gridview2.DataBind();
             // AddGridcombo();

           }
       }

private void LoadItemList()
   {
       try
       {
            connect.cnopen();
       foreach (GridViewRow grdRow in Gridview2.Rows)
       {
          DropDownList drdlist = (DropDownList)(Gridview2.Rows[grdRow.RowIndex].Cells[2].FindControl("CMBItem"));

        //   BeginTransaction("LoadItemList");
          // SqlCommand sqlcmd = new SqlCommand("FETCH_PURCHASE_DTL_SP",connect.cn());
           SqlCommand sqlcmd = new SqlCommand("FETCH_ITEM_LIST_SP", connect.cn());
                sqlcmd.CommandType=CommandType.StoredProcedure;
         SqlDataAdapter da=new SqlDataAdapter(sqlcmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
               drdlist.DataSource = dt;
              drdlist.DataValueField = "IM_SERIALNO";
               drdlist.DataTextField = "IM_ITEMNAME";
           drdlist.DataBind();

       }
       }
       catch { }
   }

推荐答案

听起来像您在每次回发中重新排列网格,从而重新加载了下拉列表.

您是否使用VS DEBUGGER并看到执行情况?我确定一旦完成,您将绑定下拉数据,使其重新绑定.

确保使用Page.IsPostBack避免在每次回发中绑定网格数据.然后确保下拉菜单也没有重新绑定.
Sounds like you are relaoding the grid on every postback which in turn reloads the dropdownlist.

Did you use VS DEBUGGER and saw the execution? I am sure once you do it, you will bind that the dropdown data is getting rebind.

Make sure, Grid data bind on every postback is avoided using Page.IsPostBack. Then make sure that the dropdown is not re-bind either.


当您从中选择一个项目时,似乎整个页面都会呈现,

请确认您的Dropdownlist的AutoPostBack = false.

并且请在Page_laod(!IsPostBack){……}
It seems that the whole page renders when you choose an Item from it,

Please confirm that you r Dropdownlist''s AutoPostBack = false.

And please bind the datasource for these DropDownlists in the Page_laod (!IsPostBack){……}


这篇关于我的Gridview下拉列表是刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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