Datagrid编辑按钮无法正常工作 [英] Datagrid edit button is not working properly

查看:73
本文介绍了Datagrid编辑按钮无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击编辑"按钮时,它的下拉列表控件变为空
请发送它的C#解决方案.. !!


代码:=

when i click on EDIT button the dropdownlist control of it is become empty
please send the C#solution of it..!!


Code:=

SqlConnection conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.AppSettings["ConnectionString"]);
    SqlDataAdapter da =new SqlDataAdapter();
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
        if (!IsPostBack)
        {
            populatedata();
            //BindData();
        }
    }
    catch (Exception ex)
    {
    }
                                                
    }

    public void populatedata()
    {
        try
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            cmd.CommandText = "select O.Order_ID,O.Package_ID,O.No_Of_Images,SC.Package_Name,SC.Package_ID,O.Remarks,O.Original_Quote_Amount,O.Revised_Quote_Amount,O.Amount_Received,(O.Revised_Quote_Amount-O.Amount_Received) As Balance_Amount,O.Status_Code from tbl_mst_Order O,tbl_mst_Service_Catalogue SC where O.Package_ID=SC.Package_ID";
            cmd.Connection = conn;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "tbl_mst_Order");
            cmd.ExecuteNonQuery();
            //Gdordermgmt.DataSource = ds;
           // Gdordermgmt.DataBind();
            cmd.CommandText = "Select Status_Id, Status_Code,Status_Description from tbl_mst_Order_Status where Is_Status_Active=1";
            cmd.Connection = conn;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "tbl_mst_Order_Status");
            cmd.ExecuteNonQuery();
            BindData();
        }
        catch (Exception ex)
        {
        }
            finally
            {
                conn.Close();
            }
    }
    protected void BindData()
    {
        Gdordermgmt.DataSource = ds.Tables["tbl_mst_Order"];
        Gdordermgmt.DataBind();
    }
    protected void Gdordermgmt_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
        try
        {
            Gdordermgmt.CurrentPageIndex = e.NewPageIndex;
            populatedata();
            //BindData();
        }
        catch(Exception ex)
        {
        }
       // Gdordermgmt.DataSource = ds;
        //Gdordermgmt.DataBind();
      // BindData();
    }
    public void Gdordermgmt_editcmd(object sender, DataGridCommandEventArgs e)
    {          
      
      Gdordermgmt.EditItemIndex = e.Item.ItemIndex;
      populatedata();
      //BindData();
       //ResetPageIndex(Gdordermgmt,View);
      //BindData();
    
    }
   
    protected void Gdordermgmt_cancelcmd(object sender, DataGridCommandEventArgs e)
    {
        Gdordermgmt.EditItemIndex = -1;
        populatedata();
        //BindData();
      
      //  Gdordermgmt.DataSource = ds;
       // Gdordermgmt.DataBind();
       // BindData();
    }
        protected void Gdordermgmt_updatecmd(object sender, DataGridCommandEventArgs e)
      {
          TextBox txtamtrecieved = (TextBox)e.Item.FindControl("txtamtrecieved");
          TextBox txtrevisedqtamt = (TextBox)e.Item.FindControl("txtrevisedqtamt");
          TextBox tx = new TextBox();
          tx = (TextBox)e.Item.Cells[8].Controls[0];
          //cmd.CommandType = CommandType.Text;
          cmd.CommandText = "Update tbl_mst_Order" +
                           " Set Revised_Quote_Amount= ''" + txtrevisedqtamt.Text + "'', Received_Amount=''" + txtamtrecieved.Text + "''where Order_ID=''" + e.Item.ItemIndex + "''";
          cmd.Parameters.Add(new SqlParameter("@txtrevisedqtamt", SqlDbType.Text));
          cmd.Parameters["@txtrevisedqtamt"].Value = tx.Text;
          cmd.ExecuteNonQuery();
          ds.AcceptChanges();
          Gdordermgmt.EditItemIndex = -1;
          Gdordermgmt.DataBind();
          //da.Update(ds);
   
       }
    protected void Gdordermgmt_Bounddata(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DropDownList list = (DropDownList)e.Item.FindControl("ddlorderstatus");
            if (list != null)
            {
                list.DataSource = ds.Tables["tbl_mst_Order_Status"];
                list.DataValueField = ds.Tables["tbl_mst_Order_Status"].Columns[0].ToString();
                list.DataTextField= ds.Tables["tbl_mst_Order_Status"].Columns[1].ToString();
                list.DataBind();
            }
        }
        else
        {
        }
    }

推荐答案

我认为首先您需要了解Web应用程序的工作方式,ASP.NET不会维护状态,您已经从数据库中加载了数据集.页面加载的isPostback.

当用户执行页面上的任何操作并执行回发后,数据集中的数据将丢失,其中不包含任何数据,这就是为什么您没有在下拉列表中获得任何数据的原因.
I think first you need to understand how web applications works, ASP.NET does not maintain state, you have loaded the dataset from database in the not isPostback of the page load.

When user perfoms any operation on the page and perform the postback then the data present in your dataset is lost, it contains nothing, that’s why you are not getting any data in the dropdown list.

成员6967450写道:
Member 6967450 wrote:

protected void Page_Load(object sender, EventArgs e)    
    {        
            try
               {        
                 if (!IsPostBack)        
                    {            
                     populatedata();            //BindData();        
                     }   
              }    catch (Exception ex)      {    }                                                    }






您可以执行以下任一选项.

1-通过将数据存储到视图状态或会话中来坚持将数据呈现在数据集中(如果您的用户群很大或数据很大,则不是一个好方法)

2-要绑定下拉列表,请再次从数据库中加载数据





You can do any one of the following options.

1- Persist the data present the the dataset by storing it into viewstate or session (Not a good approach if your userbase is big or the data is huge)

2- To bind the dropdown list, load the data again from the database


这篇关于Datagrid编辑按钮无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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