2011-12-22 103 views
0

我正在爲客戶端開發一個網頁,要求免除某些要求。有一個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'> 
      &nbsp; 
     </td> 
    </tr> 
    <tr> 
     <td align='left'> 
      <fieldset id="Fieldset"> 
       <button onclick="SendForm();"> 
        Send</button>    
       &nbsp; 
       <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> 
+2

整個頁面沒有用web方法發回。 – 2011-12-22 18:40:06

+0

對於初學者來說,讓你的int 1從0開始擺脫<=你是否是一個VB程序員轉換到C#如果記住C#for循環和數組訪問和類似的東西都是從零開始的。 – MethodMan 2011-12-22 18:42:37

+0

事實上,這會更好地放在後面的代碼並將數組作爲參數傳遞 – Chad 2011-12-22 18:52:54

回答

1

按我的知識,你不能指頁面的控制在靜態方法,直到,除非你明確地傳遞給你的Web方法「頁面」對象的引用書面。

0

如果你有下面的代碼:

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 */ 
} 

你爲什麼不具備在一類這個驗證碼在您的項目..有一個原因,你試圖訪問使用此代碼的PageControls

if (IsValidEmailAddress(email))   
{    
    for (int i = 1; i <= count; i++) 
    { 
    CheckBox chkBox = chkDesc1.Checked; 
    } 
} 

更改您的for循環和啓動I = 0,改變< = 14 < 13 C#是0基於 將整個方法簽名..你可能想打電話給你的APPLICAT內部的非靜態方法離子或者添加單詞靜態的方法或刪除,但我寧願看看你是如何創建的方法..還有你在哪裏創建的WebMethod類對象的實例..?