在web窗体中使用c#事件没有ASP控件
如果我的html语法如下所示,我将如何引发事件:在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. } 受保护的无
<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控件,因此必须在页面加载中包含代码,以检查事件的发布数据并自行引发它
<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;
}
}