当我在文本框中输入内容时,我想从aspx代码调用C#函数。如何在文本框的按键事件中从aspx代码调用C#函数。?
答案 0 :(得分:2)
进行按键事件
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Function1();
}
功能
private void Function1()
{
}
答案 1 :(得分:0)
尝试Jquery ajax -
var ListPostalCode = ["12345"];
var PostalCodeJsonText = JSON.stringify({ list: ListPostalCode });
$.ajax({
type: "POST",
url: "JobManagement.aspx/FindLocation",
data: PostalCodeJsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
},
failure: function (response) {
alert(response.d);
}
});
C#WebMethod -
[System.Web.Services.WebMethod()]
public static string FindLocation(List<string> list)
{
try{
string LocationInfo = "";
HttpWebRequest FindLocationreq = (HttpWebRequest)WebRequest.Create("http://ziptasticapi.com/" + list[0]);
FindLocationreq.Method = "GET";
using (WebResponse Statusresponse = FindLocationreq.GetResponse())
{
using (StreamReader rd = new StreamReader(Statusresponse.GetResponseStream()))
{
LocationInfo = rd.ReadToEnd();
}
}
return LocationInfo;
}
catch (Exception ex)
{
return ex.Message;
}
}
答案 2 :(得分:0)
试试这个
<asp:TextBox ID="TextBox1" clientidmode="Static" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>
JS:
function EnterEvent(e) {
if (e.keyCode == 13) {//if enter key is pressed condition
__doPostBack('<%=Button1.UniqueId%>', "");
}
}
C#:
protected void Button1_Click(object sender, EventArgs e)
{
}
答案 3 :(得分:0)
$("#target").keypress(function() {
var value=$("#target").val();
$.ajax({
type: "POST",
url: "../Webservices/yourwebservice.asmx/webmethodName",
data: "{value: " + value + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
alert(result.d);
}
);
});
你可以在关键的按键上调用你的web方法。谢谢
答案 4 :(得分:0)
这是一种方式:
ASPX:
<asp:TextBox ID="MyTextBox" ClientIDMode="Static" runat="server" />
JS:
$(function() {
$('#MyTextBox').keyup(function() {
var jsonObj = { c: $(this).val() };
$.ajax({
type: 'POST',
url: 'webservice.aspx/MyCSharpFunction',
data: JSON.stringify(jsonObj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data);
}
});
});
});
C#(本例中为webservice.aspx):
public partial class webservice : System.Web.UI.Page
{
[WebMethod]
public static string MyCSharpFunction(string c)
{
return "You typed " + c;
}
}