r k

r k

  • NA
  • 111
  • 16.9k

Populating Grid View rows from client side -without post back

Apr 25 2021 2:41 PM
I have a Grid view control & a bootstrap modal pop-up. When the user adds values by clicking add button bootstrap modal opens, the user fills values in the field & saves data with the save button from modal to the SQL database and this part works perfectly, I want the user can see the added row immediately in the grid view as soon as the user saves data though the Bootstrap save button without in postback or serverside binding. By this SqlDataSource1 i am unable to see any row in the grid view , but server-side data binding works fine but user has to refresh the page which I do not want
 
Please help
 
Below is my Modal :
  1. <button type="button" class="btn btn-primary btn-sm" data-target="#myModal">Add</button>  
  2. <div class="modal fade" tabindex="-1" role="dialog" id="myModal" >  
  3. <div class="modal-dialog modal-lg">  
  4. <div class="modal-content">  
  5. <div class="modal-header">  
  6. <button type="button" class="close" data-dismiss="modal">×</button>  
  7. <h4 class="modal-title">User add</h4>  
  8. </div>  
  9. <div class="modal-body">  
  10. <div class="form-group">  
  11. <label id="lblstartdate" for="StartDate" class="control-label">Start Date:</label>  
  12. <input id="StartDate" placeholder="Start Date" type="text" class="input-smalldp input-sm form-control clearable CMRStartDate" name="StartDate" />  
  13. </div>  
  14. <div class="form-group">  
  15. <label id="lblenddate" for="EndDate" class="control-label">End Date:</label>  
  16. <input id="EndDate" type="text" class="input-smalldp input-sm form-control clearable CMRENDDate" name="EndDate" />  
  17. </div>  
  18. div class="form-group">  
  19. <label id="lblDtype" for="ddlrole" class="control-label">Role:</label>  
  20. <select id="ddlrole" class=" form-control input-sm selectType country" name="ddlrole" multiple="multiple" size="7" >  
  21. </select>  
  22. div class="modal-footer">  
  23. <button type="button" class="btn btn-primary" name="save" onclick="SaveRecord();return false" >Save changes</button>  
  24. <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
  25. </div>  
  26. </div>  
My Save script :
  1. function SaveRecord() {  
  2. var startdate = document.getElementById('StartDate').value;  
  3. var enddate = document.getElementById('EndDate').value;  
  4. var role = countries.join(", ");  
  5. $.ajax({  
  6. type: "POST",  
  7. dataType: "json",  
  8. contentType: "application/json; charset=utf-8",  
  9. url: "MyPage.aspx/SaveDetails",  
  10. data: "{'StartDate':'" + startdate + "', 'EndDate':'" + enddate + "','Role':'" + role + "'}",  
  11. success: function (response) {  
  12. $("#gvDetails tbody").append("<tr><td>"+document.getElementById('StartDate').value+"</td><td>"+document.getElementById('EndDate').value+"</td><td>")  
  13. alert("Saved");  
  14. });  
  15. $('#myModal').modal('hide');  
  16. },  
  17. error: function () {  
  18. alert("fail")  
  19. });  
  20. },  
  21. complete: function () {  
  22. $('.modal-backdrop').remove();  
  23. }  
  24. });  
  25. }  
AND the Grid view :
  1. <asp:GridView id="gvDetails" runat="server" DataKeyNames="id" DataSourceID="SqlDataSource1">  
  2. <Columns>  
  3. <asp:BoundField DataField="FromDate" DataFormatString="{0:d}"  
  4. HeaderText="From Date" SortExpression="FromDate" />  
  5. <asp:BoundField DataField="ToDate" DataFormatString="{0:d}"  
  6. HeaderText="To Date" SortExpression="ToDate" />  
  7. <asp:BoundField DataField="Role" HeaderText="Role"  
  8. SortExpression="Role">  
  9. <ItemStyle HorizontalAlign="Center" />  
  10. </asp:BoundField>  
  11. </Columns>  
  12. </asp:GridView>  
  13. <asp:SqlDataSource ID="SqlDataSource1" runat="server"  
  14. ConnectionString="<%$ ConnectionStrings:MyDatabase%>" SelectCommand="SELECT ID, FromDate, ToDate, Role from [dbo].[NewUser] Where [IsActive]=1"  
  15. ORDER BY AddedDate"></asp:SqlDataSource>  

Answers (4)