Answered by:
TextBox is Empty onRoweditting event of gridview in c# asp.net ?

Question
-
User1601010121 posted
My edit template textboxes are empty when i click to edit button .
Here is RowEditingEventprotected void grdLocation_RowEditing(object sender, GridViewEditEventArgs e) { DataTable dt = ViewState["dtgetPackages"] as DataTable; grdLocation.DataSource = dt; grdLocation.EditIndex = e.NewEditIndex; grdLocation.DataBind(); //Bindgrid(); i used this method directly but same result as above this function is working fine }
if i remove Bindgrid(); function I need double click to edit button to go into edit mode .. in that case my text boxes are filled with right values and working fine . only problem is double click requires on edit button
here is Gridview
<asp:GridView ID="grdLocation" CssClass="table table-bordered table-condensed" OnRowDataBound="grdLocation_RowDataBound" DataKeyNames="PID" OnRowCancelingEdit="grdLocation_RowCancelingEdit" OnRowEditing="grdLocation_RowEditing" OnRowUpdating="grdLocation_RowUpdating" AutoGenerateColumns="false" runat="server"> <HeaderStyle BackColor="#378ABC" /> <Columns> <asp:TemplateField HeaderText="PackageName"> <ItemTemplate> <asp:Label runat="server" id="lblname" Text='<%# Eval("Name")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtName" Height="25px" runat="server"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Details"> <ItemTemplate> <asp:Label id="lblcode" runat="server" Text='<%# Eval("Details")%>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtDetails" Height="25px" runat="server"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="IsAptTransfer?"> <ItemTemplate> <asp:CheckBox ID="chkIsAptTransfer" AutoPostBack="true" OnCheckedChanged="chkIsAptTransfer_CheckedChanged" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:LinkButton ID="btnEdit" runat="server" CssClass="btn-sm " BackColor="#378ABC" ForeColor="White" Text="Edit" CommandName="Edit"> <i class="icon-edit icon-white"></i> Edit</asp:LinkButton> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="btn_Update" runat="server" Text="Update" CssClass="btn-sm btn-success " ToolTip="Update" CommandName="Update"> <i class="icon-upload icon-white"></i> Update</asp:LinkButton> <asp:LinkButton ID="btn_Cancel" runat="server" Text="Cancel" CssClass="btn-sm btn-danger" CommandName="Cancel"> <i class="icon-remove icon-white"></i> Cancel</asp:LinkButton> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Monday, June 20, 2016 6:06 AM
Answers
-
User1601010121 posted
Solution is
I solved this problem while ago but answering just for others..
<asp:TextBox ID="txtName" Height="25px" Text='<%# Eval("ColumnName")%>' runat="server"></asp:TextBox>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 21, 2016 11:30 AM
All replies
-
User1724605321 posted
Hi morfious90,
Please firstly read the GridView RowEditing demo below :
And mark sure you only bind the gridview in page_load once:
if(!Page.IsPostBack) { GridView.DataBind(); }
Best Regards,
Nan Yu
Monday, June 20, 2016 7:16 AM -
User1601010121 posted
Yes i did.
if (!IsPostBack) { Bindgrid(); } public void Bindgrid() { SmartCabsBO.Organization Org = new SmartCabsBO.Organization(); DataTable dtgetPackages = Org.getPackages(OID); if (dtgetPackages.Rows.Count > 0) { grdLocation.DataSource = dtgetPackages; grdLocation.DataBind(); } } protected void grdLocation_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { CheckBox ChkBox = ((CheckBox)e.Row.FindControl("chkIsAptTransfer")); string strCheck = ((DataRowView)e.Row.DataItem)["IsAptTransfer"].ToString(); if (strCheck == "1") { ChkBox.Checked = true; } else { ChkBox.Checked = false; } } }
here is all event required for Editing
Monday, June 20, 2016 9:51 AM -
User-1034726716 posted
Did you checked the value from ViewState? You can check it using dt.Rows.Count to ensure your datasource contains your expected data.
if i remove Bindgrid(); function I need double click to edit button to go into edit mode .. in that case my text boxes are filled with right values and working fine . only problem is double click requires on edit buttonDouble click issue arises when you are binding your grid on each and every postbacks at page_load event. Please take a look at this step=by-step example on how to implement CRUD in gridview using TemplateFields: http://geekswithblogs.net/dotNETvinz/archive/2009/06/10/adding-rows-in-gridview-with-edit-update-and-delete-functionality.aspx
Monday, June 20, 2016 2:59 PM -
User1601010121 posted
Friend I'm Not binding on every post back please check my code above its on if(!IsnoPostback)
Friday, June 24, 2016 9:40 AM -
User-1034726716 posted
Friend I'm Not binding on every post back please check my code above its on if(!IsnoPostback)
And you mean adding the following code below requires you to click twice before it will turn to edit mode?
protected void grdLocation_RowEditing(object sender, GridViewEditEventArgs e){ grdLocation.EditIndex = e.NewEditIndex; Bindgrid(); }
Friday, June 24, 2016 3:03 PM -
User1601010121 posted
Solution is
I solved this problem while ago but answering just for others..
<asp:TextBox ID="txtName" Height="25px" Text='<%# Eval("ColumnName")%>' runat="server"></asp:TextBox>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 21, 2016 11:30 AM