如何使用组名查找已选中的单选按钮附加了我的代码? [英] How To Find Checked Radio Button Using Group name My code Attached?

查看:122
本文介绍了如何使用组名查找已选中的单选按钮附加了我的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用组名查找已检查的单选的值,我有返回该值的方法,但是该方法应与组名一起传递

i am trying to find the value of checked radio using group name i have method that return it but what should i pass in that method along with group name

方法在这里

private string getRadioValue(ControlCollection clts, string groupName)
{
    string ret = "";
    foreach (Control ctl in clts)
    {
        if (ctl.Controls.Count != 0)
        {
            if (ret == "")
                ret = getRadioValue(ctl.Controls, groupName);
        }

        if (ctl.ToString() == "System.Web.UI.WebControls.RadioButton")
        {
            RadioButton rb = (RadioButton)ctl;
            if (rb.GroupName == groupName && rb.Checked == true)
                ret = rb.Attributes["Value"];
        }
    }
    return ret;
}

我像

Oc.aProjectSubmited = getRadioValue(RadioButton,"Aps");

Aps是单选组,但我通过单选按钮却收到错误无效参数"?

where Aps is radio group but getting error "invalid argument" on radio button i pass ??

希望您的建议预先致谢

推荐答案

这是因为您传递了RadioButton.您的方法接受ControlCollection,而不是Control.

This is because you are passing RadioButton. Your method accepts ControlCollection, not a Control.

为什么不通过this.Controls来传递页面的整个ControlCollection?还是您可能要用来保留要检查的RadioButton的任何其他ControlCollection?

Why not pass this.Controls to pass the whole ControlCollection of the page? Or any other ControlCollection that you might be using to keep that RadioButton you want to check?

这是一个例子:

    protected void Page_Load(object sender, EventArgs e)
    {
        getRadioValue(this.Controls, "Hello");
    }

    private string getRadioValue(ControlCollection clts, string groupName)
    {
        string ret = "";
        foreach (Control ctl in clts)
        {
            if (ctl.Controls.Count != 0)
            {
                if (ret == "")
                    ret = getRadioValue(ctl.Controls, groupName);
            }

            if (ctl.ToString() == "System.Web.UI.WebControls.RadioButton")
            {
                RadioButton rb = (RadioButton)ctl;
                if (rb.GroupName == groupName && rb.Checked == true)
                    ret = rb.Attributes["Value"];
            }
        }
        return ret;
    }

这篇关于如何使用组名查找已选中的单选按钮附加了我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆