Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在web窗体中使用c#事件没有ASP控件_C#_Webforms - Fatal编程技术网

在web窗体中使用c#事件没有ASP控件

在web窗体中使用c#事件没有ASP控件,c#,webforms,C#,Webforms,如果我的html语法如下所示,我将如何引发事件: <select runat="server" id="selection" onclick="inputCheck" /> 我在代码隐藏中尝试了这一点: protected void inputCheck(object sender, EventArgs e) { //doesnt matter because it raised an on click. } 受保护的无

如果我的html语法如下所示,我将如何引发事件:

<select runat="server" id="selection" onclick="inputCheck"  />

我在代码隐藏中尝试了这一点: protected void inputCheck(object sender, EventArgs e) { //doesnt matter because it raised an on click. } 受保护的无效输入检查(对象发送方,事件参数e) { //没关系,因为它引发了点击。 }


异常:Microsoft JScript运行时错误:“inputCheck”未定义,第一个代码段中的
onclick
仅在javascript中运行。要提升事件服务器端,第一步是为此事件构建javascript方法。在此基础上,您有几个选项:

  • 您可以构建一个ajax请求来调用
  • 您可以将事件发回原始页面。由于您没有使用asp控件,因此必须在页面加载中包含代码,以检查事件的发布数据并自行引发它

下面是一个使用ajax方法将jQuery发回服务器的示例,非常干净且易于使用。如果你有问题,请告诉我

--HTML页面

<select id="selection" name="selection" />
//代码隐藏页

[System.Web.Services.WebMethod]
public static bool WebMethodName(string input)
{
    try
    {
        //do something here with the input

        return (true);
    }
    catch(Exception ex)
    {
        throw ex;
    }

}
--这将不需要任何回发就将代码发布到服务器上,这是我喜欢的。为了进行测试,在WebMethodName函数内设置一个断点并查看传入的输入。嗯

[System.Web.Services.WebMethod]
public static bool WebMethodName(string input)
{
    try
    {
        //do something here with the input

        return (true);
    }
    catch(Exception ex)
    {
        throw ex;
    }

}