0

复选框数组请参考随附的屏幕截图。我有一个复选框数组和一个用于在 ASP.Net 页面中返回的按钮。我编写了如下函数来确定在按钮单击事件上选中了哪些复选框: 以下代码是从 ASP.Net 调用的业务组件的一部分。请让我知道如何将 actionArray 返回到在 ASP.Net 页面中调用函数。

    public void checkBoxValidation(Control parent, string strKey)
    {
        XmlDocument getCyleXML = new XmlDocument();
        string strChkID="", strActionXPath = "",strAction="";
        ArrayList actionArray = new ArrayList();

        // Loop through all the controls on the page  
        foreach (Control c in parent.Controls)
        {
            // Check and see if it's a checkbox. 
            if ((c.GetType() == typeof(CheckBox)))
            {
                // Since its a checkbox, see if this is checked.     
                if (((CheckBox)(c)).Checked == true)
                {
                    // Find the ID of the checkbox
                    strChkID = ((CheckBox)(c)).ID.ToString();
                    getCyleXML = CycleXML(strKey);
                    strActionXPath = "/Actions/Action[checkbox='" + strChkID + "']/*[self::Name]";
                    strAction = getCyleXML.SelectSingleNode(strActionXPath).ToString();
                    actionArray.Add(strAction);
                }
            }
            // Now we need to call itself (recursion) because all items (Panel, GroupBox, etc) is a container so we need to check 
            // all containers for any checkboxes.
            if (c.HasControls())
            {
                checkBoxValidation(c, strKey);
            }
        }
    }
4

1 回答 1

0

代码应该是这样的:

public ArrayList checkBoxValidation(Control parent, string strKey, ArrayList actionArray)
{
    XmlDocument getCyleXML = new XmlDocument();
    string strChkID="", strActionXPath = "",strAction="";
    if(actionArray == null) { actionArray = new ArrayList(); }

    // Loop through all the controls on the page  
    foreach (Control c in parent.Controls)
    {
        // Check and see if it's a checkbox. 
        if ((c.GetType() == typeof(CheckBox)))
        {
            // Since its a checkbox, see if this is checked.     
            if (((CheckBox)(c)).Checked == true)
            {
                // Find the ID of the checkbox
                strChkID = ((CheckBox)(c)).ID.ToString();
                getCyleXML = CycleXML(strKey);
                strActionXPath = "/Actions/Action[checkbox='" + strChkID + "']/*self::Name]";
                strAction = getCyleXML.SelectSingleNode(strActionXPath).ToString();
                actionArray.Add(strAction);
            }
        }
        // Now we need to call itself (recursion) because all items (Panel, GroupBox, etc) is a container so we need to check 
        // all containers for any checkboxes.
        if (c.HasControls())
        {
            checkBoxValidation(c, strKey, actionArray);
        }
    }

    return actionArray;
}
于 2012-11-04T16:34:57.487 回答