【发布时间】:2018-07-25 12:14:57
【问题描述】:
我一直在寻找解决方案已经一个月了。我浏览了所有看起来相似但不符合我要求的相关帖子。因此,我发布了这个查询,听起来与其他朋友可能发布的旧帖子相似,但它不符合我的问题。
我在网格视图(名称:GridView3)中绑定数据,我使用绑定字段来获取数据,我在此网格视图中有一个复选框,并且在检查更改时我正在创建一个数据表并传递值新网格视图中的此绑定字段(名称:GridView4)。我正在使用 GridView4 中的模板字段来绑定从 GridView3 传递的值,但是在 GridView4 中我添加了一个额外的模板字段,它是一个文本框,它不会从 Gridview3 中获取值,这里我根据我的要求手动输入值.问题是,每当我尝试从 GridView3 的 On Checked Changed 事件中在 GridView4 中添加新行时,我在文本框中手动输入的值都会丢失。那么,如何在回发时停止丢失这个特定文本框的价值?
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();
}
【问题讨论】:
-
您不应该将整个 DataTable 存储在页面的 ViewState 中(在客户端 HTML 中呈现)。无论如何这都是多余的,因为您已经将所有内容都存储在 GridView 中。
-
@TimSchmelter 好的,你能帮我解决这个问题吗?
-
@TimSchmelter 祝我好运:(
-
看起来这是不可能的。放弃我的想法;(