TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Dharam Rai
NA
383
22.5k
Prevent losing the value of the TextBox of the GridView
Jul 20 2018 11:48 PM
I had been looking for the solution been a month. I have surfed all the related post which seems similar but doesn’t match my requirement. Therefore I am posting this query which will sound similar to the old post that other friends might have posted, however it doesn’t meet my problem.
I am binding a data in Grid View (Name : GridView3) where I am using Bound Field to get the data, I have a checkbox in this Grid View and On Checked Changed I am creating a Data Table and passing the value of this Bound Field in new Grid View (Name: GridView4). I am using Template Field in GridView4 to bind the value passed from GridView3, but in GridView4 I have added one extra Template Field which is a Text Box which doesn’t get value from Gridview3, here I am typing the value manually as per my requirement. The problem is, the value which I am typing manually in Text Box get lost whenever I try to add new row in GridView4 from On Checked Changed event of GridView3. So, how do I stop losing the value of this particular Text Box on Post Back?
Here is my Grid View:
GridView3:
<asp:GridView ID="GridView3" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" CssClass="tableAdmin" BorderStyle="Solid" BorderWidth="1px" CellPadding="0" ForeColor="Black" GridLines="Vertical" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" Enabled="true" onclick="checkAll(this);" AutoPostBack="true" OnCheckedChanged="CheckBox_CheckChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick="Check_Click(this)" AutoPostBack="true" OnCheckedChanged="CheckBox_CheckChanged" />
</ItemTemplate>
<HeaderStyle Width="10%" />
<ItemStyle Width="10%" />
</asp:TemplateField>
<asp:BoundField DataField="AId" HeaderText="AId" Visible="true" HtmlEncode="false">
<HeaderStyle Width="20%" />
<ItemStyle Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="AssetsName" HeaderText="Product" HtmlEncode="false">
<HeaderStyle Width="20%" />
<ItemStyle Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="SubAssetName" HeaderText="Product_Type" HtmlEncode="false">
<ItemStyle Width="20%" />
<HeaderStyle Width="20%" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
GridView4
<asp:GridView ID="GridView4" runat="server" Font-Names="Arial" Font-Size="11pt" BackColor="White" BorderColor="#999999" CssClass="tableAdmin" BorderStyle="Solid" BorderWidth="1px" AllowPaging="True" EmptyDataText="No Product Selected" AutoGenerateColumns="False"
CellPadding="3" ForeColor="Black" GridLines="Vertical">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField HeaderText="AId" Visible="false">
<ItemTemplate>
<asp:Label ID="lblAId" runat="server" Text='<%# Bind("AId") %>'></asp:Label>
<asp:TextBox ID="txtGAId" runat="server" Text='<%# Bind("AId") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
<ControlStyle Width="10%" />
<HeaderStyle Width="10%" />
<ItemStyle Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:Label ID="lblAssetsName" runat="server" Text='<%# Bind("AssetsName") %>'></asp:Label>
<asp:TextBox ID="txtGAssetsName" runat="server" Text='<%# Bind("AssetsName") %>'
Visible="false"></asp:TextBox>
</ItemTemplate>
<ControlStyle Width="100%" />
<HeaderStyle Width="15%" />
<ItemStyle Width="20%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub_Product">
<ItemTemplate>
<asp:Label ID="lblSubAssetName" runat="server" Text='<%# Bind("SubAssetName") %>'></asp:Label>
<asp:TextBox ID="txtGSubAssetName" runat="server" Text='<%# Bind("SubAssetName") %>'
Visible="false"></asp:TextBox>
</ItemTemplate>
<ControlStyle Width="100%" />
<HeaderStyle Width="10%" />
<ItemStyle Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" Visible="true" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtPrice" runat="server" ReadOnly="true" CssClass="tableAdminTextBox"Visible="true">
</asp:TextBox>
</ItemTemplate>
<ControlStyle Width="100%" />
<HeaderStyle Width="8%" />
<ItemStyle Width="8%" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" BorderWidth="1px" BorderColor="Black" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
Below is the code behind:
protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
pnl_InProduct.Visible = true;
GetData();
SetData();
BindSecondaryGrid();
}
private DataTable CreateDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("AId");
dt.Columns.Add("AssetsName");
dt.Columns.Add("SubAssetName");
dt.AcceptChanges();
return dt;
}
private DataTable AddRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("AId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length <= 0)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["AId"] = gvRow.Cells[1].Text;
dt.Rows[dt.Rows.Count - 1]["AssetsName"] = gvRow.Cells[2].Text;
dt.Rows[dt.Rows.Count - 1]["SubAssetName"] = gvRow.Cells[3].Text;
dt.AcceptChanges();
}
return dt;
}
private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("AId = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}
private void GetData()
{
DataTable dt;
if (ViewState["SelectedRecords"] != null)
dt = (DataTable)ViewState["SelectedRecords"];
else
dt = CreateDataTable();
CheckBox chkAll = (CheckBox)GridView3.HeaderRow.Cells[0].FindControl("chkAll");
for (int i = 0; i < GridView3.Rows.Count; i++)
{
if (chkAll.Checked)
{
dt = AddRow(GridView3.Rows[i], dt);
}
else
{
CheckBox chk = (CheckBox)GridView3.Rows[i].Cells[0].FindControl("chk");
if (chk.Checked)
{
dt = AddRow(GridView3.Rows[i], dt);
}
else
{
dt = RemoveRow(GridView3.Rows[i], dt);
}
}
}
ViewState["SelectedRecords"] = dt;
}
private void SetData()
{
CheckBox chkAll = (CheckBox)GridView3.HeaderRow.Cells[0].FindControl("chkAll");
chkAll.Checked = true;
if (ViewState["SelectedRecords"] != null)
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
for (int i = 0; i < GridView3.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView3.Rows[i].Cells[0].FindControl("chk");
if (chk != null)
{
DataRow[] dr = dt.Select("AId = '" + GridView3.Rows[i].Cells[1].Text + "'");
chk.Checked = dr.Length > 0;
if (!chk.Checked)
{
chkAll.Checked = false;
}
}
}
}
}
private void BindSecondaryGrid()
{
DataTable dt = (DataTable)ViewState["SelectedRecords"];
GridView4.DataSource = dt;
GridView4.DataBind();
}
Reply
Answers (
6
)
Get parent & child relationship using Recursive in C#
Creating UpVote And DownVote button