asp.net gridview没有保存状态

时间:2014-06-29 11:08:37

标签: c# jquery asp.net ajax

我在asp.net页面中有一个asp.net gridview,它继承自母版页。 网格视图位于更新面板内。我需要让用户向网格添加行,所以我在标题中添加了一个按钮。这是网格代码         

                                                                    <asp:UpdatePanel runat="server" UpdateMode="Conditional"  ID="updtSPG">

                                                                        <ContentTemplate>

                                                                            <asp:GridView ID="grdSPGData" runat="server" HorizontalAlign="Center" OnRowDataBound="grdSPGData_RowDataBound"

                                                                                AutoGenerateColumns="false" AllowPaging="false" EnableViewState="true" ShowHeaderWhenEmpty="true" ShowFooter="true"

                                                                                DataKeyNames="SPGID" CssClass="table table-hover table-striped table-bordered centerGridHead" OnRowCommand="grdSPGData_RowCommand">

                                                                                <Columns>

                                                                                    <asp:TemplateField HeaderText="" Visible="false" ShowHeader="false">

                                                                                        <ItemTemplate>

                                                                                            <asp:Label ID="lblSPGID" runat="server" Visible="false" Text='<%# Bind("SPGID") %>'></asp:Label>

                                                                                        </ItemTemplate>

                                                                                    </asp:TemplateField>

                                                                                    <asp:TemplateField HeaderText="Specialty Group" HeaderStyle-Width="40%">

                                                                                        <ItemTemplate>

                                                                                            <asp:DropDownList ID="ddlSPGSpecialities" runat="server" class="form-control "/>



                                                                                        </ItemTemplate>



                                                                                       <%-- <FooterTemplate>

                                                                                            <asp:DropDownList ID="ddlSPGSpecialities" runat="server" class="form-control "/>

                                                                                        </FooterTemplate>--%>

                                                                                    </asp:TemplateField>

                                                                                    <asp:TemplateField HeaderText="TPA 1_2" HeaderStyle-Width="15%">

                                                                                        <ItemTemplate>

                                                                                            <%--<asp:TextBox ID="txtSPGTPA1_2" CssClass="text-center" Text='<%# Bind("TPA1_2") %>' Width="100%" EnableViewState="true" runat="server" AutoPostBack="true" OnTextChanged="txtSPGTPA1_2_TextChanged">  </asp:TextBox>--%>

                                                                                            <asp:TextBox ID="txtSPGTPA1_2" CssClass="text-center"   Width="100%" EnableViewState="true" runat="server"  >  </asp:TextBox>

                                                                                        </ItemTemplate>



                                                                                       <%-- <FooterTemplate>

                                                                                            <asp:TextBox ID="txtSPGTPA1_2" CssClass="text-center"   Width="100%" EnableViewState="true" runat="server"  >  </asp:TextBox>

                                                                                        </FooterTemplate>--%>

                                                                                    </asp:TemplateField>

                                                                                    <asp:TemplateField HeaderText="TPA 3_5" HeaderStyle-Width="15%">

                                                                                        <ItemTemplate>

                                                                                            <%--<asp:TextBox ID="txtSPGTPA3_5" CssClass="text-center" Text='<%# Bind("TPA3_5") %>' Width="100%" EnableViewState="true" runat="server" AutoPostBack="true" OnTextChanged="txtSPGTPA3_5_TextChanged">  </asp:TextBox>--%>

                                                                                            <asp:TextBox ID="txtSPGTPA3_5" CssClass="text-center"  Width="100%" EnableViewState="true" runat="server" >  </asp:TextBox>

                                                                                        </ItemTemplate>

                                                                                       <%-- <FooterTemplate>

                                                                                            <asp:TextBox ID="txtSPGTPA3_5" CssClass="text-center"  Width="100%" EnableViewState="true" runat="server" >  </asp:TextBox>

                                                                                        </FooterTemplate>--%>

                                                                                    </asp:TemplateField>

                                                                                    <asp:TemplateField HeaderText="Total Doctors" ItemStyle-Width="30%">

                                                                                        <ItemTemplate>

                                                                                            <%--<asp:TextBox ID="txtSPGTotalDocs" CssClass="text-center" Text='<%# Bind("TotalDoctors") %>' Width="100%" EnableViewState="true" runat="server" AutoPostBack="true" OnTextChanged="txtSPGTotalDocs_TextChanged">  </asp:TextBox>--%>

                                                                                            <asp:TextBox ID="txtSPGTotalDocs" CssClass="text-center"   Width="100%" EnableViewState="true" runat="server"  >  </asp:TextBox>

                                                                                        </ItemTemplate>

                                                                                       <%-- <FooterTemplate>

                                                                                            <asp:TextBox ID="txtSPGTotalDocs" CssClass="text-center"   Width="100%" EnableViewState="true" runat="server"  >  </asp:TextBox>

                                                                                        </FooterTemplate>--%>

                                                                                    </asp:TemplateField>

                                                                                    <asp:TemplateField ItemStyle-Width="30%">

                                                                                        <HeaderTemplate>

                                                                                            <asp:LinkButton id="btnAddToSPGGrid" CommandName="AddNewRow" runat="server" CausesValidation="false"  Text="<i aria-hidden='true' class='icon-plus'></i>" CssClass="btn btn-info btn-xs "/>

                                                                                        </HeaderTemplate>

                                                                                        <ItemTemplate>

                                                                                    <asp:LinkButton id="btnRemove" runat="server" CausesValidation="false"  Text="<i aria-hidden='true' class='icon-remove'></i>" CssClass="btn btn-primary btn-xs "/>

                                                                                        </ItemTemplate>

                                                                                    </asp:TemplateField>

                                                                                </Columns>

                                                                            </asp:GridView>

                                                                        </ContentTemplate>

                                                                        <Triggers>

                                                                            <%--<asp:AsyncPostBackTrigger  ControlID="ContentPlaceHolder2_grdSPGData_btnAddToSPGGrid" EventName="click" />--%>

                                                                            <%--<asp:AsyncPostBackTrigger  ControlID="txtSPGTPA3_5" EventName="TextChanged" />

                                                                            <asp:AsyncPostBackTrigger  ControlID="txtSPGTotalDocs" EventName="TextChanged" />--%>

                                                                        </Triggers>

                                                                    </asp:UpdatePanel>

                                                                </div>

所以当我按下这个按钮时,我可以添加一个新行。我创建了一个新行

  protected void createNewRow()

{

    //GridViewRow gvRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);

    dtSPG = new DataTable();



    //dtSPG = (DataTable)Session["dtSPG"];

    //dtSPG = (Session["dtSPG"] == null ? new DataTable() : (DataTable)Session["dtSPG"]);

    if (dtSPG.Columns.Count == 0)

    {

        dtSPG.Columns.Add("SPGID", typeof(string));

        dtSPG.Columns.Add("dos_id", typeof(string));

        dtSPG.Columns.Add("TPA1_2", typeof(string));

        dtSPG.Columns.Add("TPA3_5", typeof(string));

        dtSPG.Columns.Add("TotalDoctors", typeof(string));

    }

    DataRow dr;

    foreach (GridViewRow row in grdSPGData.Rows)

    {

        dr = dtSPG.NewRow();



        dr[0] = ((Label)row.Cells[0].FindControl("lblSPGID")).Text;

        dr[1] = ((DropDownList)row.Cells[1].FindControl("ddlSPGSpecialities")).SelectedValue;

        dr[2] = ((TextBox)row.Cells[1].FindControl("txtSPGTPA1_2")).Text;

        dr[3] = ((TextBox)row.Cells[2].FindControl("txtSPGTPA3_5")).Text;

        dr[4] = ((TextBox)row.Cells[3].FindControl("txtSPGTotalDocs")).Text;



        dtSPG.Rows.Add(dr);

    }

    dr = dtSPG.NewRow();

    dr[0] = Guid.NewGuid(); dr[1] = ""; dr[2] = ""; dr[3] = ""; dr[4] = "";

    dtSPG.Rows.Add(dr);

    //spgs.Add(new SpecialtyGroup("2", "00000001-0000-0000-0000-000000000000", 1, 3, 5));

    //spgs.Add(new SpecialtyGroup("2", "00000001-0000-0000-0000-000000000000", 1, 3, 5));



    grdSPGData.DataSource = dtSPG;

    grdSPGData.DataBind();

    Session["dtSPG"] = dtSPG;

}







    protected void grdSPGData_RowCommand(object sender, GridViewCommandEventArgs e)

{

    if (e.CommandName.Equals("AddNewRow"))

    {



        createNewRow();

    }

}

我的问题是,当我按下添加新行按钮时,所有网格内容都会消失,虽然我将它们保存在数据表中,然后重新绑定它。 任何帮助将不胜感激 谢谢

0 个答案:

没有答案