ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.How to save dynamically created GridView data inside user control (.ascx) to Database using jQuery in ASP.NetHi @nagaraju60,<br />Please try the following<br /><br /><a href = 'link(Threads/181179/Find-and-access-TextBox-inside-UserControl-from-Parent-Page-using-JavaScript/)'>Find and access TextBox inside UserControl from Parent Page using JavaScript</a><br /><br />It might help you.<br /><br />Cheers Andrea.https://www.aspforums.net:443/Threads/656083/How-to-save-dynamically-created-GridView-data-inside-user-control-ascx-to-Database-using-jQuery-in-ASPNet/https://www.aspforums.net:443/Threads/656083/How-to-save-dynamically-created-GridView-data-inside-user-control-ascx-to-Database-using-jQuery-in-ASPNet/Wed, 26 Jul 2017 03:33:34 GMTHow to save dynamically created GridView data inside user control (.ascx) to Database using jQuery in ASP.Net<p>Hi <a class="username" rel="nagaraju60"> nagaraju60</a>,</p> <p>Is all the code inside the user control?</p>https://www.aspforums.net:443/Threads/656083/How-to-save-dynamically-created-GridView-data-inside-user-control-ascx-to-Database-using-jQuery-in-ASPNet/https://www.aspforums.net:443/Threads/656083/How-to-save-dynamically-created-GridView-data-inside-user-control-ascx-to-Database-using-jQuery-in-ASPNet/Wed, 26 Jul 2017 03:34:25 GMTHow to save dynamically created GridView data inside user control (.ascx) to Database using jQuery in ASP.Net<blockquote><cite>dharmendr says:</cite> <p>Hi <a class="username" rel="nagaraju60"> nagaraju60</a>,</p> <p>Is all the code inside the user control?</p> </blockquote> <p>Dear @dharmendr,</p> <p>&nbsp; Yes, the entire code in user control page (.ascx) only</p>https://www.aspforums.net:443/Threads/656083/How-to-save-dynamically-created-GridView-data-inside-user-control-ascx-to-Database-using-jQuery-in-ASPNet/https://www.aspforums.net:443/Threads/656083/How-to-save-dynamically-created-GridView-data-inside-user-control-ascx-to-Database-using-jQuery-in-ASPNet/Wed, 26 Jul 2017 03:51:11 GMTHow to save dynamically created GridView data inside user control (.ascx) to Database using jQuery in ASP.Net<p>Hi <a class="username" rel="nagaraju60"> nagaraju60</a>,</p> <p>You can't call a method kept inside a usercontrol through jquery. Because .ascx controls doesn't represent real url that can be accessed from a client machine. They're purely server side meant to embed in other pages.</p> <p>So you need to write the ajax code and the web method in the page itself.</p> <p>Refer the below sample code</p> <p><span style="text-decoration: underline;">DynamicGrid.ascx</span></p> <pre class="brush: html">&lt;%@ Control Language=&#34;C#&#34; AutoEventWireup=&#34;true&#34; CodeFile=&#34;DynamicGrid.ascx.cs&#34; Inherits=&#34;DynamicGrid&#34; %&gt; &lt;div&gt; &lt;table border=&#34;0&#34; cellpadding=&#34;0&#34; cellspacing=&#34;0&#34;&gt; &lt;tr&gt; &lt;td&gt; Columns &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtColumns&#34; runat=&#34;server&#34; Text=&#34;2&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Rows &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtRows&#34; runat=&#34;server&#34; Text=&#34;2&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan=&#34;2&#34;&gt; &lt;asp:Button ID=&#34;btnSubmit&#34; OnClick=&#34;GenerateGridView&#34; Text=&#34;Submit&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvData&#34; runat=&#34;server&#34; /&gt; &lt;input type=&#34;hidden&#34; name=&#34;hfInsertQuery&#34; id=&#34;hfInsertQuery&#34; /&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Save&#34; runat=&#34;server&#34; ID=&#34;btnSave&#34; /&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvRecords&#34; runat=&#34;server&#34; EmptyDataText=&#34;No record present&#34; /&gt; &lt;/div&gt; </pre> <p><span style="text-decoration: underline;">DynamicGrid.ascx.cs</span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string constr = System.Configuration.ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr)) { using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(&#34;SELECT * FROM nagaraju60&#34;)) { using (System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); gvRecords.DataSource = dt; gvRecords.DataBind(); } } } } } } protected void GenerateGridView(object sender, EventArgs e) { gvData.Columns.Clear(); DataTable dt = new DataTable(); int cols = Convert.ToInt32(txtColumns.Text.Trim()); int rows = Convert.ToInt32(txtRows.Text.Trim()); for (int i = 0; i &lt; cols; i++) { TemplateField field = new TemplateField(); field.HeaderText = &#34;Column&#34; + (i + 1).ToString(); field.ItemTemplate = new GridViewTemplate(&#34;Column&#34; + (i + 1).ToString(), (i + 1).ToString()); gvData.Columns.Add(field); } for (int i = 0; i &lt; rows; i++) { dt.Rows.Add(); } gvData.DataSource = dt; gvData.DataBind(); } public class GridViewTemplate : ITemplate { private string columnNameBinding; public GridViewTemplate(string colname, string colNameBinding) { columnNameBinding = colNameBinding; } public void InstantiateIn(System.Web.UI.Control container) { TextBox tb = new TextBox(); tb.ID = &#34;txtDynamic&#34; + columnNameBinding; container.Controls.Add(tb); } } </pre> <p><span style="text-decoration: underline;">Default.aspx</span></p> <pre class="brush: html">&lt;%@ Page Language=&#34;C#&#34; AutoEventWireup=&#34;true&#34; CodeFile=&#34;Default.aspx.cs&#34; Inherits=&#34;_Default&#34; %&gt; &lt;%@ Register TagPrefix=&#34;uc&#34; Src=&#34;~/DynamicGrid.ascx&#34; TagName=&#34;Grid&#34; %&gt; &lt;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&gt; &lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#39;[id*=btnSave]&#39;).on(&#34;click&#34;, function () { var query = &#34;&#34;; var columnQuery = &#34;INSERT INTO nagaraju60(&#34;; var trs = $(&#39;[id*=gvData]&#39;).find(&#39;tr:has(:not(th))&#39;); var columnCount = $($(trs)[0]).find(&#39;td&#39;).length; for (var i = 1; i &lt;= columnCount; i++) { columnQuery += &#34;[&#34; + i + &#34;],&#34;; } columnQuery = columnQuery.substring(0, columnQuery.length - 1); columnQuery += &#34;) VALUES (&#34;; for (var i = 0; i &lt; $(trs).length; i++) { var tds = $(trs)[i]; var valueQuery = &#34;&#34;; $(tds).find(&#39;td&#39;).each(function () { valueQuery += &#34;&#39;&#34; + $(this).find(&#39;[id*=txtDynamic]&#39;).val() + &#34;&#39;,&#34;; }); valueQuery = valueQuery.substring(0, valueQuery.length - 1); query += columnQuery + valueQuery + &#34;) \n&#34;; } $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/InsertCustomers&#34;, data: &#39;{query: &#34;&#39; + query + &#39;&#34; }&#39;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (response) { window.location.href = window.location.href; }, error: function (response) { alert(response.d); } }); return false; }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div&gt; &lt;uc:Grid ID=&#34;ucGrid&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p><span style="text-decoration: underline;">Default.aspx.cs</span></p> <pre class="brush: csharp">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { [System.Web.Services.WebMethod] public static void InsertCustomers(string query) { string constr = System.Configuration.ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr)) { using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query)) { cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } }</pre> <p><span style="text-decoration: underline;">Screenshot</span></p> <p><span style="text-decoration: underline;"><img src="http://i.imgur.com/LGuC3Iy.gif" alt="" width="358" height="496" /></span></p>https://www.aspforums.net:443/Threads/656083/How-to-save-dynamically-created-GridView-data-inside-user-control-ascx-to-Database-using-jQuery-in-ASPNet/https://www.aspforums.net:443/Threads/656083/How-to-save-dynamically-created-GridView-data-inside-user-control-ascx-to-Database-using-jQuery-in-ASPNet/Wed, 26 Jul 2017 09:56:46 GMT