控制单选按钮

时间:2015-09-27 12:10:11

标签: c# asp.net

我有200个单选按钮,其ID为KP1,KP2,KP3 ...... KP200。我想运行一个for循环来检查它们是否被检查。

我在9号线发生了撞车事件:

RbId = CtrlId;

我想从字符串中提取单选按钮Ctrl,类似于使用javascript完成的方式,即。,

document.getElementById("<%=ID%>").

请告知。

代码:

int i;
RadioButton RbId = null;
string CtrlId = null;
char[] KPList = new char[200];

for (i = 1; i <= 200; i++)
{
    CtrlId = "KP"+i.ToString();
    RbId = CtrlId;
    if(RbId.Checked)
    {
        KPList[i] = (char)j;
        break;
    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用FindControl()方法:

Control ctrl = this.FindControl(CtrlId);

if (ctl is RadioButton)
{
    RadioButton rdBtn = ctrl as RadioButton;
    // now do whatever here
    if(rdBtn.Checked)
    {
    }
}

答案 1 :(得分:1)

试试这个:

protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);

        SolidBrush brushBackground = new SolidBrush(Color.White);
        try
        {
            pe.Graphics.Clip = new Region(new Rectangle(0, 0, this.Width, this.Height));
            pe.Graphics.FillRegion(brushBackground, pe.Graphics.Clip);

            if (Image == null) return;
            Graphics g = pe.Graphics;
            g.ScaleTransform(m_Scale, m_Scale);
            g.DrawImage(Image, (float)m_ImageOffset.X, (float)m_ImageOffset.Y);

        }
        finally
        {
            brushBackground.Dispose();
        }
    }

    private List<IShape> _Shapes = new List<IShape>();
    /// <summary>
    /// A list of all of the entities (shapes) to be drawn.
    /// </summary>
    public List<IShape> Shapes
    {
        get 
        {
            if (_Shapes == null) _Shapes = new List<IShape>();
            return _Shapes;
        }
        set { _Shapes = value; }
    }

    private Metafile Image { get; set; }

    /// <summary>
    /// Creates an in memory image file.
    /// </summary>
    private void BuildImage()
    {
        Graphics refGraph = this.CreateGraphics();
        IntPtr hdc = refGraph.GetHdc();
        try
        {
            Metafile image = new Metafile(hdc, EmfType.EmfOnly, "Shapes");
            using (Graphics g = Graphics.FromImage(image))
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                foreach (IShape entity in Shapes)
                {
                    entity.Draw(g);
                }
            }
            Image = image;
        }
        finally
        {
            refGraph.ReleaseHdc(hdc);
            refGraph.Dispose();
        }
        Invalidate();
    }

    private void PanelMapControl_Resize(object sender, EventArgs e)
    {
        if (m_InZoomFitMode)
        {
            ZoomFit();
        }
    }

    private void PanelMapControl_MouseDown(object sender, MouseEventArgs e)
    {
        this.Focus(); //Need to get focus in order to receive mouse wheel events. 

        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Cursor = Cursors.Hand;

            //Note the current mouse position in order to calculate the image offset
            //when the mouse is moved.
            m_MouseStartPosition.X = e.X;
            m_MouseStartPosition.Y = e.Y;
            m_OffsetAtStartOfMove = m_ImageOffset;
        }
    }

    private void PanelMapControl_MouseMove(object sender, MouseEventArgs e)
    {
        if(e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            m_ImageOffset.X = (int)(m_OffsetAtStartOfMove.X + (e.X - m_MouseStartPosition.X) / m_Scale);
            m_ImageOffset.Y = (int)(m_OffsetAtStartOfMove.Y + (e.Y - m_MouseStartPosition.Y) / m_Scale);
            Invalidate();
        }
    }

    private void PanelMapControl_MouseUp(object sender, MouseEventArgs e)
    {
        if(e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Cursor = Cursors.Cross;
        }
    }

这样会更好:

foreach (RadioButton rdbtn in myDiv.Controls.OfType<RadioButton>())//Assume the RadioButtons are inside a div tag called myDiv
{
    if(rdbtn.Checked)
    {
        ....
    }
}
相关问题