-2

当我在文本框中输入内容时,我想从 aspx 代码中调用 C# 函数。如何在文本框的按键事件中从 aspx 代码调用 C# 函数。

4

5 回答 5

2

进行按键按下事件

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    Function1();
}

功能

private void Function1()
{
}
于 2013-07-26T04:30:06.377 回答
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);
    }
   );
   });

您可以像这样在按键上调用您的网络方法。谢谢

于 2013-07-26T04:36:07.593 回答
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)
    {

    }
于 2013-07-26T04:32:40.813 回答
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;
    }

}
于 2013-07-26T04:47:27.950 回答
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# 网络方法 -

[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;
            }
        }

参考1

参考文献 2

参考文献 3

于 2013-07-26T04:32:11.453 回答