我正在尝试检查数据库中是否存在特定值,但是我想要的是
使用javascript
按钮调用C#函数的更好方法protected void Check_exam_id(object sender, EventArgs e)
{
string DDSelected_Class = DD_class.SelectedValue;// store it in some variable;
string DD__Method = DD_Method.SelectedValue;// store it in some variable;
if (DD__Method == "THEORY")
{
using (MySqlConnection myConnection = new MySqlConnection(constr))
{
string oString = "Select * from score_master WHERE Class=@DDSelected_Class ";
MySqlCommand oCmd = new MySqlCommand(oString, myConnection);
oCmd.Parameters.AddWithValue("@DDSelected_Class", DDSelected_Class);
myConnection.Open();
using (MySqlDataReader oReader = oCmd.ExecuteReader())
{
if (oReader == null || !oReader.HasRows)
{
ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('No Student Found')", true);
}
else
{
ScriptManager.RegisterStartupScript(this, typeof(Page), "alert2", "alert('Exist')", true);
myConnection.Close();
}
}
}
}
}
asp按钮
<asp:Button runat="server" id="Check_examid" AutoPostback = "false" onclick='Check_exam_id' style=" display:none; float: right; width: 22%; height: 41px; text-align: center; background: #EAEAEA; border: none; border-color: #EAEAEA; margin-top: 2%;" Text="Submit"></asp:Button>
Javascript
document.getElementById("BodyHolder_Check_examid").click()
答案 0 :(得分:2)
将其作为web方法,并使用ajax从jquery调用该方法。 请找到以下链接,它包含使用jquery ajax在asp.net中调用webmethods的示例。 http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
希望这有帮助!
答案 1 :(得分:2)
正如评论中所提到的,您可以使用 jquery ajax 轻松完成。为此,您必须定义类似这样的WebMethod: -
请加入using System.Web.Services;
。
[WebMethod]
public static bool Check_exam_id(string className, string MethodName)
{
bool examIdExist = false;
if (DD__Method == "THEORY")
{
\\Your logic here
}
\\Based on DB operation return value
examIdExist
}
最后从你的客户端这样打电话: -
$("#Check_examid").click(function (e) {
e.preventDefault(); //To avoid postback
//Get these values from dropdwon
var className = $("#DD_class").val();
var methodName = $("#DD_Method").val();
var data = { "className": className , "MethodName": methodName };
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/Check_exam_id",
data: JSON.stringify(data),
success: function (response) {
if(response.d) //If method returned true.
alert('Exist');
else
alert('No Student Found')
},
error: function (msg) { alert(msg.d); }
});
});
另外,包括jquery基本库文件。
答案 2 :(得分:0)
使用Jquery ajax方法或使用UpdatePanels
将方法Check_exam_id创建为公共静态并将其装饰为[WebMethod] 当你调用任何其他函数时,在按钮的click事件上调用javascript函数。
function DoWork(){
$.ajax({
url: 'YourPage.aspx/Check_exam_id',
data: {}, //blank since you are sending nothing
dataType:'JSON',
type: 'POST',
contentType:'application/json;charset=utf-8',
success:function(){
alert('Success');
//Do whatever you want with the HTML
},
error:function()
{
alert('Error');
}
});
}
答案 3 :(得分:-1)
$("#Check_examid").click(function (e) {
e.preventDefault(); //To avoid postback
//Get these values from dropdwon
var className = $("#DD_class").val();
var methodName = $("#DD_Method").val();
var data = { "className": className , "MethodName": methodName };
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/Check_exam_id",
data: JSON.stringify(data),
success: function (response) {
if(response!=null) //If method returned true.
alert('Exist');
$("DivId").html(response.d)
else
alert('No Student Found')
},
error: function (msg) { alert(msg.d); }
});
});