防止在PostBack上丢失GridView的TextBox的值

时间:2018-07-20 11:23:33

标签: c# asp.net gridview

我一直在寻找解决方案一个月。我已经浏览了所有相关的帖子,这些帖子看起来很相似,但不符合我的要求。因此,我正在发布此查询,听起来与其他朋友可能发布的旧帖子类似,但是不符合我的问题。

我正在使用绑定字段获取数据的Grid View(名称:GridView3)中绑定数据,我在此Grid View中具有一个复选框,并且在选中时已更改,我正在创建数据表并传递值新的Grid View(名称: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();
        }

1 个答案:

答案 0 :(得分:0)

我在这里找到了答案,我粘贴了一个链接,以防万一有人想参考:

https://www.aspforums.net/Threads/172604/Prevent-losing-the-value-of-the-TextBox-of-the-GridView-on-PostBack/Answered#Replies

相关问题