1.编写function
function GetStudentByNumb() {
var number = $('#TextBox1').val(); //jquery 取文本框的值(学号)
if (number == null) {
alert("请输入学号");
return false;
}
$.ajax({
type: "POST",
url: "MainForm.aspx/GetStudentByNumb",
contentType: "application/json",
dataType: "json",
data: "{\"strNumb\":\"" + number + "\"}", //以json格式传参数
success: function (data) {
//查询函数返回的不是整形,返回的是结果字符串
if (data.d != "") {
$("#div_1").html(data.d);
} else {
$("#div_1").html("暂无数据");
}
},
error: function (msg) {
alert(msg);
}
});
return true;
}
2.控件按钮点击事件绑定
<input type="button" id="btnQuery" οnclick="GetStudentByNumb();" value="查询" />
3.编写webmethod操作数据库
[WebMethod]
public static String GetStudentByNumb(string strNumb)
{
OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
try
{
StringBuilder strSb = new StringBuilder();
strSb.Append("<table><tr><td>学号</td><td>姓名</td><td>性别</td><td>年龄</td><td>语文</td><td>数学</td><td>外语</td><td>物理</td><td>化学</td></tr>");
conn.Open();
OracleCommand cmd = conn.CreateCommand();
if(strNumb!="")
{
string sql = " select A.SN,A.NAME,A.GENDER,A.AGE,B.CHINESE,B.MATH,B.ENGLISH,B.PHYSICS,B.CHEMISTRY FROM T_CX_STUMANAGE_STUDENT A,T_CX_STUMANAGE_GRADE B WHERE A.SN=B.SNO and A.SN ='" + strNumb + "'";
cmd.CommandText = sql;
}
OracleDataReader dr = cmd.ExecuteReader();
strSb.Append("<tr>");
if (dr.Read())
{
strSb.Append("<td>" + dr["SN"].ToString() + "</td>");
strSb.Append("<td>" + dr["NAME"].ToString() + "</td>");
strSb.Append("<td>" + dr["GENDER"].ToString() + "</td>");
strSb.Append("<td>" + dr["AGE"].ToString() + "</td>");
strSb.Append("<td>" + dr["CHINESE"].ToString() + "</td>");
strSb.Append("<td>" + dr["MATH"].ToString() + "</td>");
strSb.Append("<td>" + dr["ENGLISH"].ToString() + "</td>");
strSb.Append("<td>" + dr["PHYSICS"].ToString() + "</td>");
strSb.Append("<td>" + dr["CHEMISTRY"].ToString() + "</td>");
strSb.Append("</tr>");
}
strSb.Append("</table>");
string strResult = strSb.ToString();
return strResult;
}
catch (Exception ex)
{
return "数据读取失败,错误是:" + ex.Message;
}
}