Get Records by Generic Handler Using JQuery in Asp.net

Get Records by Generic Handler Using JQuery in Asp.net

HandlerUse.aspx——————————————————

<div>
<div>
Search User
</div>
<div>
User ID<asp:TextBox ID=”txtID” runat=”server” MaxLength=”1″></asp:TextBox><asp:Button
ID=”btnSearch” runat=”server” Text=”Search” />
</div>
<span id=”msgresult”>Loading.</span>
<div>
<table>
<tr>
<td>
Id
</td>
<td>
Name
</td>
<td>
Price
</td>
</tr>
<tr>
<td id=”IdRes”>
</td>
<td id=”NameRes”>
</td>
<td id=”PriceRes”>
</td>
</tr>
</table>
</div>
</div>

Add below Section to Head

<script src=”jquery-1.7.2.min.js” type=”text/javascript”></script>
<script type=”text/javascript”>

$(document).ready(function () {
$(“#msgresult”).hide();

//Button Click Event
$(“input[id$=btnSearch]”).click(function () {
var id = $(“input[id$=txtID]”).val();
var myTimer = setInterval(Inprocess, 1000);
//Giving value from handler using post method
$.post(“Handler.ashx”, { id: id }, function (result) {
var res = result;
var splitRes = res.split(‘||’);
$(“#msgresult”).show();
$(“.Result”).attr(‘style’, ‘display:block’);
$(“#IdRes”).html(splitRes[0]);
$(“#NameRes”).html(splitRes[1]);
$(“#PriceRes”).html(splitRes[2]);
$(“#msgresult”).html(“Records.”);
$(“#msgresult”).css(“color”, “green”);
clearInterval(myTimer);

});
return false;
});

//Function for Loader like Loading……
function Inprocess() {
$(“#msgresult”).show();
$(“#msgresult”).html($(“#msgresult”).html() + “.”);
}
});

</script>
<style type=”text/css”>
.Result
{
display:none;
}
</style>

Use Generic Handler Class File

Handler.ashx————————————————————–

using System;
using System.Web;
using System.Data;
using System.Text;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = “text/plain”;
int id = Convert.ToInt32(context.Request[“id”]);
string result = GetRecords(id);
context.Response.Write(result);
}
public string GetRecords(int id)
{
DataTable dt = GetProductDataTable();
DataRow[] UserDetail = dt.Select(“ID=” + id);
StringBuilder sb = new StringBuilder();
foreach (DataRow row in UserDetail)
{
sb.Append(Convert.ToString(row[“ID”]) + “||”);
sb.Append(Convert.ToString(row[“NAME”]) + “||”);
sb.Append(Convert.ToString(row[“PRICE”]));
}
return sb.ToString();
}

public DataTable GetProductDataTable()
{
// Here we create a DataTable with three columns.
DataTable dtproduct = new DataTable();
dtproduct.Columns.Add(“ID”, typeof(int));
dtproduct.Columns.Add(“NAME”, typeof(string));
dtproduct.Columns.Add(“PRICE”, typeof(int));

// Here we add five DataRows.
dtproduct.Rows.Add(1, “Product1”, 100);
dtproduct.Rows.Add(2, “Product2”, 110);
dtproduct.Rows.Add(3, “Product3”, 120);
dtproduct.Rows.Add(4, “Product4”, 130);
dtproduct.Rows.Add(5, “Product5”, 140);

return dtproduct;
}
public bool IsReusable {
get {
return false;
}
}

}

Download Code
Handler Use

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply