Datagrid编辑按钮无法正常工作

编程入门 行业动态 更新时间:2024-10-26 06:29:41
本文介绍了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编辑按钮无法正常工作

本文发布于:2023-11-27 05:32:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   按钮   编辑   工作   Datagrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!