Gridview不存储记录并显示最后输入的记录 [英] Gridview not storing the records and displaying the last entered record

查看:42
本文介绍了Gridview不存储记录并显示最后输入的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中我首先需要一个按钮点击按钮,用户应该看到弹出的标签和文本框,点击添加按钮,数据应该添加到网格视图中,每个事情都已完成,但如果是我第一次输入一个人的详细信息,然后点击添加按钮,然后将它们添加到gridview,如果我输入另一个人的详细信息,那么第一个人的gridview中的详细信息将被删除,而在gridview中第二个人的详细信息是显示但是如果我在添加记录后再次运行应用程序,它应该同时显示两个记录,然后添加的记录不会显示在gridview中,即使我运行应用程序也不会显示添加的记录。



我尝试了什么:



in my application i first need to have a button on clicking the button user should see a pop up with labels and text boxes and on clicking the add button the data should be added to grid view every thing is done but if for the first time i enter the details of a person and i click on add button then they are added to gridview,again if i enter details of another person then the details that are in gridview for first person are disappered and in gridview the second person details are displayed but it should display both the records also if i again run the application after adding the records then the added records are not displayed in gridview how can i display the added records even i run the application any no of times.

What I have tried:

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .Background
        {
            background-color: Black;
            filter: alpha(opacity=90);
            opacity: 0.8;
        }
        .Popup
        {
            background-color: #FFFFFF;
            border-width: 3px;
            border-style: solid;
            border-color: black;
            padding-top: 10px;
            padding-left: 10px;
            width: 400px;
            height: 350px;
        }
        .lbl
        {
            font-size:16px;
            font-style:italic;
            font-weight:bold;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="Fill Form in Popup" />

            <div style="margin-left:10px;margin-top:10px">  
     <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" CellPadding="2"  

        ForeColor="#333333" GridLines="None">  
        <AlternatingRowStyle BackColor="White" />  
        <Columns>  
            <asp:BoundField HeaderStyle-Width="120px" HeaderText="FirstName" DataField="FirstName" />  
            <asp:BoundField HeaderStyle-Width="120px" HeaderText=" MiddleName" DataField="MiddleName" />  
            <asp:BoundField HeaderStyle-Width="120px" HeaderText=" LastName" DataField="LastName" />  
            <asp:BoundField HeaderStyle-Width="120px" HeaderText="Gender" DataField="Gender" />  
        </Columns>  
        <EditRowStyle BackColor="#2461BF" />  
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
        <RowStyle BackColor="#EFF3FB" />  
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />  
        <SortedAscendingCellStyle BackColor="#F5F7FB" />  
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />  
        <SortedDescendingCellStyle BackColor="#E9EBEF" />  
        <SortedDescendingHeaderStyle BackColor="#4870BE" />  
    </asp:GridView>  
    </div>  
<!-- ModalPopupExtender -->
<cc1:ModalPopupExtender ID="mp1" runat="server" PopupControlID="Panl1" TargetControlID="Button1"

    CancelControlID="Button2" BackgroundCssClass="Background">
</cc1:ModalPopupExtender>
<asp:Panel ID="Panl1" runat="server" CssClass="Popup" align="center" style = "display:none">
    <div id="irm1" style=" width: 300px; height: 250px;" runat="server" >
    <table>
    <tr>
    <td>
    <asp:Label ID="Label1" runat="server" CssClass="lbl" Text="First Name"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox1" runat="server" Font-Size="14px" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label2" runat="server" CssClass="lbl" Text="Middle Name"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox2" runat="server" Font-Size="14px" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label3" runat="server" CssClass="lbl" Text="Last Name"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox3" runat="server" Font-Size="14px" ></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="Label4" runat="server" CssClass="lbl" Text="Gender"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="TextBox4" runat="server" Font-Size="14px" ></asp:TextBox>
    </td>
    <td>
    <asp:Button ID="Button3" runat="server" Text="Add" OnClick="Button3_Click" />
    </td>
        </tr>
    </table>
        </div>
    <br />
    <asp:Button ID="Button2" runat="server" Text="Close" />
</asp:Panel>
<!-- ModalPopupExtender -->
    </form>
</body>





In my code behind-





In my code behind-

protected void Page_Load(object sender, EventArgs e)
       {
           dt = new DataTable();
           DataColumn dc1 = new DataColumn("FirstName");
           DataColumn dc2 = new DataColumn("MiddleName");
           DataColumn dc3 = new DataColumn("LastName");
           DataColumn dc4 = new DataColumn("Gender");
           dt.Columns.Add(dc1);
           dt.Columns.Add(dc2);
           dt.Columns.Add(dc3);
           dt.Columns.Add(dc4);
           DataRow dr1 = dt.NewRow();
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }
       DataTable dt;

       protected void Button3_Click(object sender, EventArgs e)
       {
           DataRow dr1 = dt.NewRow();
           dr1[0] = TextBox1.Text;
           dr1[1] = TextBox2.Text;
           dr1[2] = TextBox3.Text;
           dr1[3] = TextBox4.Text;
           dt.Rows.Add(dr1);
           GridView1.DataSource = dt;
           GridView1.DataBind();
           ClearFields();

       }
       protected void ClearFields()
       {
           TextBox1.Text = "";
           TextBox2.Text = "";
           TextBox3.Text = "";
           TextBox4.Text = "";
       }

推荐答案

The issue in your Code In Page load you are re initiate the DataTable... Other thing is you now maintaining the datatable values between the post Back. Use Below code.



The issue in your Code In Page load you are re initiate the DataTable... Other thing is you now maintaining the datatable values between the post Back. Use Below code.

protected void Page_Load(object sender, EventArgs e)
       {
           if (ViewState["DT"] != null)
           {
               dt = (DataTable)ViewState["DT"];
           }
           else
           {
               dt = new DataTable();
               DataColumn dc1 = new DataColumn("FirstName");
               DataColumn dc2 = new DataColumn("MiddleName");
               DataColumn dc3 = new DataColumn("LastName");
               DataColumn dc4 = new DataColumn("Gender");
               dt.Columns.Add(dc1);
               dt.Columns.Add(dc2);
               dt.Columns.Add(dc3);
               dt.Columns.Add(dc4);
               ViewState["DT"] = dt;
           }

       }
protected void Button3_Click(object sender, EventArgs e)
       {

           DataRow Dr = dt.NewRow();
           Dr[0] = TextBox1.Text;
           Dr[1] = TextBox2.Text;
           Dr[2] = TextBox3.Text;
           Dr[3] = TextBox4.Text;
           dt.Rows.Add(Dr);
           dt.AcceptChanges();
           ViewState["DT"] = dt;
           GridView1.DataSource = dt;
           GridView1.DataBind();
           ClearFields();
       }


这篇关于Gridview不存储记录并显示最后输入的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆