This is a simple and practical jquery+ajax request data display in the GridView ASP tutorial. NET program Oh, let's take a look at how to get Ajax to return the values shown in the ASP.net tutorial data displayed on the GridView.
aspx file
protected void Page_Load (object sender, EventArgs e)
{
if (request["id"]!= null)
{
SqlConnection conn=null;
SqlCommand cmd = null;
SqlDataAdapter adapter = null;
Try
{
conn = new SqlConnection ();
conn.connectionstring = configurationmanager.connectionstrings["NorthwindConnectionString"].connectionstring;
Conn.Open ();
cmd = new SqlCommand ();
Cmd.connection = conn;
Cmd.commandtype = CommandType.Text;
String cmdstr = "SELECT * from Dbo.customers";
if (request["id"].tostring ()!=string.empty)
{
Cmdstr + = "where customerid= '" + request["id"].tostring () + "'";
}
Cmd.commandtext = Cmdstr;
adapter = new SqlDataAdapter (cmd);
DataSet ds = new DataSet ();
Adapter.fill (DS);
This.gvdata.datasource = ds;
This.gvdata.databind ();
}
Catch
{
Response.Write ("Error happend!");
Response.Flush ();
Response.End ();
}
Finally
{
if (adapter!= null)
{
Adapter.dispose ();
}
if (cmd!= null)
{
Cmd.dispose ();
}
if ((conn!= null) && (conn.state = = ConnectionState.Open))
{
Conn.close ();
}
}
}
}
jquery Code
<script src= "Js/jquery-1.4.2.js" type= "text/Web Effects" ></script>
<script type= "Text/javascript" >
function Showprogressdiv () {
var id = $ ("Input#idtxt"). Val ();
$.ajax ({
Type: "Get",
URL: "Getgridviewbyconditionform.aspx",
Data: "id=" + ID,
Beforesend:function () {
$ ("Div#progressdiv"). CSS Tutorial ("Display", "block");
},
Success:function (msg) {
$ ("Div#showsearchresult"). HTML (msg);
},
Complete:function () {
$ ("Div#progressdiv"). CSS ("display", "none");
}
});
}
</script>
<form id= "Form1" runat= "Server" >
<div>
<input type= "text" id= "Idtxt" name= "Idtxt"/>
<input type= "button" id= "loadbtn" value= "Loaddatagridview" onclick= "Showprogressdiv ()"/>
</div>
<div id= "Progressdiv" style= "Display:none" >
loading ...
</div>
<div id= "Showsearchresult" >
</div>
</form>