如何通过使用jQuery的AJAX调用Web窗体的功能? [英] How to call a function of webform through jquery with ajax?
问题描述
我要调用一个函数来改变通过与阿贾克斯jQuery的在WebForm1的名为firstdivision分裂的文本框中的值。
这是code我写的,但它不工作:
I want to call a function to change the values in text boxes of division named "firstdivision" in WebForm1 through jquery with ajax. This is the code i wrote but it's not working :
<asp:ScriptManager ID="scriptmanager" runat="server" EnableCdn="true" AjaxFrameworkMode="Disabled">
<Scripts>
<asp:ScriptReference Path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"/>
</Scripts>
</asp:ScriptManager>
<script type="text/javascript" lang="javascript">
var pageurl = '<%=ResolveUrl("~/WebForm1.aspx/Search()") %>';
var srch = $("#<%=list_Search.Text%>");
var name = $("#<%=txt_Name.Text%>");
var empID = $("#<%=txt_EmpID.Text%>");
var address = $("#<%=txt_Address.Text%>");
var email = $("#<%=txt_Email.Text%>");
var phone = $("#<%=txt_Phone.Text%>");
var salary = $("#<%=txt_Salary.Text%>");
var dob = $("#<%=txt_DOB.Text%>");
var natinality = $("#<%=txt_Nationality.Text%>");
$('document').ready
(
function ()
{
$("#btn_Search").click
(
function (e)
{
e.preventDefault();
$.ajax
(
{
type : 'POST',
URL: pageurl,
async: "true",
//data: srch,
//contentType: "application/json; charset=utf-8",
//dataType: "json",
success: function (x)
{
alert("Done Successfully with " + x.msg);
},
error: function (e) { alert("The call got failed due to " + e.msg); }
}
);
$("#firstDivision").html("").append(data);
}
);
}
)
//function dotask()
//{
// employee_db.WebForm1.Search();
// return true;
//}
</script>
这是code的职能正在呼叫:
And this is the code for the functions am calling :
[WebMethod]
public static void Search()
{
//string search,string name,int empID,string address,string email,double phone,double salary,DateTime dob,string nationality
string constr = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
SqlConnection myconnection = new SqlConnection(constr);
myconnection.Open();
WebForm1 test = new WebForm1();
test.conc(myconnection);
//conc(myconnection);
myconnection.Close();
}
protected void conc(SqlConnection myconnection)
{
string search = list_Search.Text;
SqlDataReader reader;
if(string.Compare(search,"Search By Name",true)==0)
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM Emp_Details WHERE Name='" + txt_Name.Text + "'", myconnection);
reader= myCommand.ExecuteReader();
}
else if (string.Compare(search, "Search By Employee ID", true) == 0)
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM Emp_Details WHERE Employee_ID='" + txt_EmpID + "'", myconnection);
reader= myCommand.ExecuteReader();
}
else if (string.Compare(search, "Search By Address", true) == 0)
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM Emp_Details WHERE Address='" + txt_Address + "'", myconnection);
reader= myCommand.ExecuteReader();
}
else if (string.Compare(search, "Search By Phone No", true) == 0)
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM Emp_Details WHERE Phone='" + txt_Phone + "'", myconnection);
reader= myCommand.ExecuteReader();
}
else if (string.Compare(search, "Search By Email", true) == 0)
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM Emp_Details WHERE Email='" + txt_Email + "'", myconnection);
reader= myCommand.ExecuteReader();
}
else if (string.Compare(search, "Search By Salary", true) == 0)
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM Emp_Details WHERE Salary='" + txt_Salary + "'", myconnection);
reader= myCommand.ExecuteReader();
}
else if (string.Compare(search, "Search By Date Of Birth", true) == 0)
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM Emp_Details WHERE Date_of_Birth='" + txt_DOB + "'", myconnection);
reader= myCommand.ExecuteReader();
}
else
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM Emp_Details WHERE Nationality='" + txt_Nationality + "'", myconnection);
reader= myCommand.ExecuteReader();
}
try
{
DataTable dt = new DataTable();
if (!reader.Read())
return;
txt_Name.Text = reader["Name"].ToString();
txt_EmpID.Text = reader["Employee_ID"].ToString();
txt_Address.Text = reader["Address"].ToString();
txt_Phone.Text = reader["Phone"].ToString();
txt_Salary.Text = reader["Salary"].ToString();
txt_Email.Text = reader["Email"].ToString();
txt_DOB.Text = reader["Date_Of_Birth"].ToString();
txt_Nationality.Text = reader["Nationality"].ToString();
}
catch (Exception ex)
{
throw ex;
}
}
现在我没有得到任何错误,但我想刷新它包含所有这些文本框现在显示新数据搜索通过搜索功能,通过数据库
Now am not getting any error but i want to refresh the division "first division" which is containing all those textboxes to now show the new data searched through the search function through the database
推荐答案
好吧,第一件事第一 - 语法是坏的(尤其是功能...... ......()...... {等),
这code应该工作(使用的jsfiddle http://jsfiddle.net/ajynnoff/ )我不得不硬code $(#firstDivision)文本(这应该是来自阿贾克斯 - 只是模拟这里);
,但我想你会得到的图片。
Ok, first thing first - syntax is bad (especially "function ...... ... .... () ..... { etc.),
this code should work (using JSfiddle http://jsfiddle.net/ajynnoff/ ) I had to hardcode $("#firstDivision").text("this should come from ajax - just simulating here");
but I think you'll get the picture.
$('document').ready(function () {
$("#btn_Search").click(function (e) {
e.preventDefault();
console.log("clicked");
$("#firstDivision").load("/echo/html/", function (response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#ajax_status").html(msg + xhr.status + " " + xhr.statusText);
} else if (status == "success") {
var msg = "Success: ";
$("#ajax_status").html(msg + xhr.status + " " + xhr.statusText);
$("#firstDivision").text("this should come from ajax - just simulating here");
}
});
});
});
我个人会用纯粹的AJAX http://api.jquery.com/jquery.ajax / 的为起点,终点误差等,而不是负担 - 以获得最佳的部分)
Personally I would use "pure" ajax http://api.jquery.com/jquery.ajax/ for start,end, error etc. instead of load - to get the best parts :)
这篇关于如何通过使用jQuery的AJAX调用Web窗体的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!