【问题标题】:Verify Checkbox.Checked from static method从静态方法验证 Checkbox.Checked
【发布时间】:2011-12-22 18:35:37
【问题描述】:

我正在开发一个网页,供客户请求豁免某些要求。有 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.");
        }
    }
}

我收到错误“非静态字段、方法或属性所需的对象引用...”

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

【问题讨论】:

  • 整个页面没有通过网络方式回传。
  • 对于初学者来说,让你的 int 1 从 0 开始摆脱
  • 确实,这会更好地放在后面的代码中并将数组作为参数传递
  • 为什么提交按钮使用AJAX方式?除非你的意思不是“提交”?
  • 编辑帖子以更准确地描述问题

标签: c# exception static


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你有以下代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      相关资源
      最近更新 更多