我正在开发一个网站,其中有两个html按钮,不是ASP按钮,因为我不希望它们进行回传。对于提交按钮,我会调用一个Javascript函数来实现PageMethods,以便从服务器端调用一个C#方法。以下是按钮和Javascript的代码:
<fieldset id="Fieldset">
<button onclick="SendForm();">Send</button>
<button onclick="CancelForm();">Cancel</button>
</fieldset>
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server" />
<script type="text/javascript">
function SendForm() {
var email = $get("txtEmail").value;
PageMethods.SendForm(email, OnSucceeded, OnFailed);
}
function OnSucceeded() {
$get("Fieldset").innerHTML = "<p>Thank you!</p>";
}
function OnFailed(error) {
alert(error.get_message());
}
</script>
这里展示的是codebehind方法:
[WebMethod]
public static void SendForm(string email)
{
if (string.IsNullOrEmpty(email))
{
throw new Exception("You must supply an email address.");
}
else
{
if (IsValidEmailAddress(email))
{
bool[] desc = new bool[14];
bool[] local = new bool[14];
bool[] other = new bool[14];
for (int i = 1; i <= 14; i++)
{
desc[i] = ((CheckBox)Page.FindControl("chkDesc" + i.ToString())).Checked;
local[i] = ((CheckBox)Page.FindControl("chkLocal" + i.ToString())).Checked;
other[i] = ((CheckBox)Page.FindControl("chkOther" + i.ToString())).Checked;
/* Do stuff here */
}
}
else
{
throw new Exception("You must supply a valid email address.");
}
}
}
除非将其声明为静态,否则无法工作。 将其声明为静态会阻止我在页面上检查复选框,因为它会生成“需要对象引用的非静态字段、方法或属性”错误。 因此,我的问题可以从以下两个方向解决。 A)有没有办法在不将该方法声明为静态的情况下使其工作? B)如果该方法是静态的,如何检查复选框。