Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
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..

XML
<asp:GridView ID="grdCase" runat="server" AutoGenerateColumns="False">
                                            <Columns>
                                                <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" />
                                            </Columns>
                                        </asp:GridView>




JAVASCRIPT :

JavaScript
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);' />";

           tr.appendChild(cell1);
           tr.appendChild(cell2);
           tr.appendChild(cell3);
           tr.appendChild(cell4);
           tr.appendChild(cell5);
           tr.appendChild(cell6);
           tr.appendChild(cell7);
           tr.appendChild(cell8);
           tr.appendChild(cell9);
           tr.appendChild(cell10);
           tr.appendChild(cell11);


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

       }


Can anybody help me in this aspect..

Thanks in anticipation.

Sri
Posted
Comments
Thanks7872 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
CreateEmptyCaseGrid()
End If
Thanks7872 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
Thanks7872 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)



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