简体   繁体   English

验证复选框。从静态方法检查

[英]Verify Checkbox.Checked from static method

I'm developing a web page for clients to request exemptions from certain requirements. 我正在为客户开发一个网页,以请求豁免某些要求。 There is a list of 14 CheckBoxes, each with two subsequent Checkboxes. 有一个14个复选框的列表,每个复选框都有两个后续的复选框。 The code to check which of them are checked will be placed in the submit button. 检查其中哪个被选中的代码将放置在提交按钮中。 This method is in the code behind. 该方法在后面的代码中。 Here's the code for that (so far) 这是到目前为止的代码

[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.");
        }
    }
}

I get the error "An object reference iw required for the non-static field, method, or property..." 我收到错误消息“非静态字段,方法或属性需要对象引用iw ...”

In the aspx page I have the button and the javascript/ajax that calls the button in the codebehind. 在aspx页面中,我有按钮和在代码背后的javascript / ajax调用该按钮。 Shown here: 如图所示:

<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>

据我所知,除非并且除非您将“ Page”对象的引用显式传递给您编写的Web方法,否则您不能在静态方法中引用页面的控件。

Where you have the following code: 您在其中具有以下代码:

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

Why don't you have this validation code in a Class in your project.. is there a reason why you are trying to access the PageControls using this code 为什么您的项目中的类中没有此验证代码?。为什么有原因尝试使用此代码访问PageControls?

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

Change your for loop and start i = 0 and change <= 14 to < 13 C# is 0 based Place the whole method signature .. you are probably trying to call a non static method inside of your application either add the word static to the method or remove but I would rather see how you have the method created.. also where are you creating the instance of the WebMethod Class object..? 更改您的for循环并开始i = 0并将<= 14更改为<13 C#基于0放置整个方法签名..您可能正在尝试在应用程序内部调用非静态方法,或者在该方法中添加单词static。或删除,但我更希望看到如何创建方法。.以及在何处创建WebMethod类对象的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM