对于我正在开发的网站,我有两个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>
代码隐藏方法:
[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)如果方法是静态的,我如何检查复选框。
这样做的伎俩,谢谢! – 2011-12-22 20:42:05