對於我正在開發的網站,我有兩個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