我正在爲客戶端開發一個網頁,要求免除某些要求。有一個14個複選框的列表,每個複選框都有兩個複選框。檢查選中哪個的代碼將被放置在提交按鈕中。這個方法在後面的代碼中。下面是該(到目前爲止)代碼驗證複選框。從靜態方法中檢查
[WebMethod]
public 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.");
}
}
}
我得到錯誤「的對象引用IW的非靜態字段,方法或屬性嘀......」
在aspx頁面我有代碼隱藏的按鈕和調用按鈕的javascript/ajax。如下圖所示:
<table width='750' align='center'>
<tr>
<td align='left'>
<label>Please Provide your email address:</label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align='left'>
</td>
</tr>
<tr>
<td align='left'>
<fieldset id="Fieldset">
<button onclick="SendForm();">
Send</button>
<button onclick="CancelForm();">
Cancel</button>
</fieldset>
</td>
</tr>
</table>
</form>
<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>
整個頁面沒有用web方法發回。 – 2011-12-22 18:40:06
對於初學者來說,讓你的int 1從0開始擺脫<=你是否是一個VB程序員轉換到C#如果記住C#for循環和數組訪問和類似的東西都是從零開始的。 – MethodMan 2011-12-22 18:42:37
事實上,這會更好地放在後面的代碼並將數組作爲參數傳遞 – Chad 2011-12-22 18:52:54