Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looks like you got your answer but here is something that might help although your question is a little bit vague on what your intent is. </p> <hr> <p>Updated to include UpdatePanel and jQuery solutions</p> <p>This is the jQuery solution using templates although you could do this in a few different ways. You don't need to use templates but I might it the easiest way of doing this. By sending back only JSON the overhead is much smaller than the UpdatePanel solution. </p> <pre><code>&lt;script src="https://github.com/jquery/jquery-tmpl/raw/master/jquery.tmpl.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script id="productTemplate" type="text/html"&gt; &lt;tr&gt; &lt;td&gt;${Name}&lt;/td&gt;&lt;td&gt;${Price}&lt;/td&gt; &lt;/tr&gt; &lt;/script&gt; &lt;script type="text/javascript"&gt; $(function () { $("#searching").ajaxStart(function () { $(this).show(); }); $("#searching").ajaxStop(function () { $(this).hide(); }); $("#btnSearch").click(function (evt) { // JSON.stringify requires json2.js for all browser support //https://github.com/douglascrockford/JSON-js //post to WebMethod var p = { product: $("#product").val() }; $.ajax({ type: "POST", url: "Default.aspx/Search", data: JSON.stringify(p), contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { //unwrap response if using .NET 3.5+ var results = data.d; //jQuery templates make it much easier to render repeat data $("#productTemplate").tmpl(results).appendTo($("#products").empty()); $("#results").show('slow'); } }); evt.preventDefault(); }); &lt;/script&gt; </code></pre> <p>html</p> <pre><code>&lt;div id="searching" style="display:none;"&gt; Searching Please Wait.... &lt;img src="Products/ajax-loader.gif" alt="Searching" /&gt; &lt;/div&gt; &lt;input type="text" id="product" /&gt; &lt;input type="submit" id="btnSearch" value="Search" /&gt; &lt;div id="results" style="display:none;"&gt; &lt;table cellspacing="0" border="1" style="border-collapse:collapse;"&gt; &lt;tbody&gt;&lt;tr&gt;&lt;th scope="col"&gt;Product&lt;/th&gt;&lt;th scope="col"&gt;Price&lt;/th&gt;&lt;/tr&gt;&lt;/tbody&gt; &lt;tbody id="products"&gt;&lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; </code></pre> <p>Code behind - you will need to include using System.Web.Services;</p> <pre><code>//If you're going to be using this in multiple locations then I'd put this into a seperate WebService.asmx [WebMethod] public static List&lt;Product&gt; Search(string product) { //Simulate database call System.Threading.Thread.Sleep(2000); //Do your database code here //Using strongly typed model makes generating the JSON much easier var products = new List&lt;Product&gt;(); for (int i = 0; i &lt; 10; i++) { products.Add(new Product() { Name = String.Format("Product {0} {1}", product, i), Price = 10 * i }); } return products; } public class Product { public string Name { get; set; } public int Price { get; set; } } </code></pre> <p>Update Panel Solution is probably more familiar to normal ASP.NET but it has the disadvantage of just hiding the Postback cycle. You are still sending back the full ViewState and HTMl on each request. </p> <pre><code>&lt;asp:ScriptManager ID="ScriptManager" runat="server"&gt;&lt;/asp:ScriptManager&gt; &lt;!-- Grab a nice loading gif http://www.ajaxload.info/ --&gt; &lt;asp:UpdateProgress ID="MainUpdateProgress" runat="server" AssociatedUpdatePanelID="MainUpdatePanel"&gt; &lt;ProgressTemplate&gt; Searching Please Wait.... &lt;img src="ajax-loader.gif" alt="Searching"/&gt; &lt;/ProgressTemplate&gt; &lt;/asp:UpdateProgress&gt; &lt;asp:UpdatePanel ID="MainUpdatePanel" runat="server"&gt; &lt;ContentTemplate&gt; &lt;asp:TextBox id="tbProduct" runat="server" /&gt; &lt;asp:Button ID="btnSearch" runat="server" Text="Search Products" onclick="btnSearch_Click" /&gt; &lt;asp:GridView ID="grdvProducts" runat="server"&gt;&lt;/asp:GridView&gt; &lt;br /&gt; &lt;!-- if you dont want to use a gridview then a repeater will do --&gt; &lt;asp:Repeater ID="rProducts" runat="server"&gt; &lt;HeaderTemplate&gt; &lt;table cellspacing="0" border="1" style="border-collapse:collapse;"&gt; &lt;tbody&gt; &lt;tr&gt;&lt;th scope="col"&gt;Product&lt;/th&gt;&lt;th scope="col"&gt;Price&lt;/th&gt;&lt;/tr&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt;&lt;td&gt;&lt;%# Eval("Product") %&gt;&lt;/td&gt;&lt;td&gt;&lt;%# Eval("Price") %&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/FooterTemplate&gt; &lt;/asp:Repeater&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; </code></pre> <p>Code behind</p> <pre><code> protected void btnSearch_Click(object sender, EventArgs e) { //Do Validation string product = tbProduct.Text; //Do database calls here Thread.Sleep(2000); //Simulate long search time var dt = new DataTable(); dt.Columns.Add("Product"); dt.Columns.Add("Price"); for (int i = 0; i &lt; 10; i++) { dt.Rows.Add(String.Format("Product {0} {1}", product, i), i * 10); } grdvProducts.DataSource = dt; grdvProducts.DataBind(); rProducts.DataSource = dt; rProducts.DataBind(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload