Click here to Skip to main content
15,896,481 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:

ASP.NET
<%@ 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-

C#
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 = "";
       }
Posted
Updated 10-Jul-16 21:04pm
v2
Comments
manishss 9-Jul-16 16:37pm    
whay you want this,you can do with another way
PIEBALDconsult 10-Jul-16 0:47am    
I don't do ASP, but have you tried dt.AcceptChanges() ?
https://msdn.microsoft.com/en-us/library/system.data.datatable.acceptchanges(v=vs.110).aspx
Member 12324523 10-Jul-16 2:16am    
can you show it in my code

1 solution

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.

C#
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();
       }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900