Click here to Skip to main content
13,054,329 members (125,403 online)
Rate this:
Please Sign up or sign in to vote.

I have a grid view, and I'm adding rows to that gridview in javascript.
Now the problem is when I click on submit button,
I need to loop through the gridview in code behind.
But while page load, the grid is getting rendered and getting empty, hence no rows in the loop.

the code bits are like below..

<asp:GridView ID="grdCase" runat="server" AutoGenerateColumns="False">
                                                <asp:BoundField DataField="Providername" HeaderText="Providername" HeaderStyle-Width="200px" />
                                                <asp:BoundField DataField="DOA" HeaderText="DOA" HeaderStyle-Width="50px" />
                                                <asp:BoundField DataField="DOD" HeaderText="DOD" HeaderStyle-Width="50px" />
                                                <asp:BoundField DataField="ReceiptNo" HeaderText="ReceiptNo" HeaderStyle-Width="50px"
                                                    DataFormatString="{0:c}" />
                                                <asp:BoundField DataField="ReceiptDate" HeaderText="ReceiptDate" HeaderStyle-Width="50px" />
                                                <asp:BoundField DataField="ReceiptAmount" HeaderText="ReceiptAmount" HeaderStyle-Width="50px" />
                                                <asp:BoundField DataField="ApprovedAmount" HeaderText="ApprovedAmount" HeaderStyle-Width="50px"
                                                    DataFormatString="{0:c}" />
                                                <asp:BoundField DataField="WageType" HeaderText="WageType" HeaderStyle-Width="50px" />
                                                <asp:BoundField DataField="VisitType" HeaderText="VisitType" HeaderStyle-Width="50px" />
                                                <asp:BoundField DataField="ClaimType" HeaderText="Claim Type" HeaderStyle-Width="50px" />


 function AddNewRecord() {
            var grd = document.getElementById("<%= grdCase.ClientID %>");
            var tr = document.createElement("tr");
            var cell1 = document.createElement("td");
            var cell2 = document.createElement("td");
            var cell3 = document.createElement("td");
            var cell4 = document.createElement("td");
            var cell5 = document.createElement("td");
            var cell6 = document.createElement("td");
            var cell7 = document.createElement("td");
            var cell8 = document.createElement("td");
            var cell9 = document.createElement("td");
            var cell10 = document.createElement("td");
            var cell11 = document.createElement("td");


            if (document.getElementById("<%= divProviderTextbox.ClientID %>").style.display == 'block') {
                var txtProviderName = document.getElementById("<%= txtProviderName.ClientID %>").value;
            else if (document.getElementById("<%= divProviderDropDown.ClientID %>").style.display == 'block') {
                var txtProviderName = document.getElementById("<%= ddlProvider.ClientID %>").value;
            var txtTreatmentDate = document.getElementById("<%= txtTeatmentDt.ClientID %>").value;
            var txtDOA = document.getElementById("<%= txtDOA.ClientID %>").value;
            var txtDOD = document.getElementById("<%= txtDOD.ClientID %>").value;
            var txtReceiptNo = document.getElementById("<%= txtReceiptNo.ClientID %>").value;
            var txtReceiptDt = document.getElementById("<%= txtReceiptDt.ClientID %>").value;
            var txtReceiptAmt = document.getElementById("<%= txtReceiptAmt.ClientID %>").value;
            var txtApprovedAmt = document.getElementById("<%= txtApprovedAmt.ClientID %>").value;
            var SelectedWage = document.getElementById("<%= ddlWageType.ClientID %>").value;
            var VisitType = document.getElementById("<%= lblVisitType.ClientID %>").innerHTML;
            var ClaimType = document.getElementById("<%= lblClaimType.ClientID %>").innerHTML;
            var bt = document.createElement('button');
            cell1.innerHTML = txtProviderName;
            if (VisitType == 'InPatient') {
                cell2.innerHTML = txtDOA;
                cell3.innerHTML = txtDOD;
            else {
                cell3.innerHTML = txtTreatmentDate;
            cell4.innerHTML = txtReceiptNo;
            cell5.innerHTML = txtReceiptDt;
            cell6.innerHTML = txtReceiptAmt;
            cell7.innerHTML = txtApprovedAmt;
            cell8.innerHTML = SelectedWage;
            if (VisitType == 'InPatient') {
                cell9.innerHTML = 'I';
            else {
                cell9.innerHTML = 'O';
            cell10.innerHTML = ClaimType;
            cell11.innerHTML = "<input id='delete' type='Submit' name='Remove' value='Remove' onclick ='javascript:return remove(this);' />";

            var tbod = grd.rows[0].parentNode;
            //var newRow = grd.rows[grd.rows.length - 1].cloneNode(true);
            return false;

Can anybody help me in this aspect..

Thanks in anticipation.

Posted 28-Apr-13 18:42pm
Rohan Leuva 29-Apr-13 0:55am
You have not showed page_load code.Well,i suspect that you are filling gridview on pageload from database,then if you added data from above code then where have you stored it?so on every page load,regardless of data inserted,it is getting old values only.
cyanceenu 29-Apr-13 1:01am
Yes Rohan.. Grid is binding with empty record on page load.. but it is restricted when post back..

If Not (IsPostBack) Then
End If
Rohan Leuva 29-Apr-13 1:06am
Add your data to datatable and bind that datatable to gridview modifying your above code,keep (!IsPostBack) code as it is.
cyanceenu 29-Apr-13 2:18am
there exists the problem... there is only one empty row in grid while post back pageload
Rohan Leuva 29-Apr-13 3:23am
i am not getting you. i suggested you that first of all get user data into data table and from data table to gridview.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100