简体   繁体   English

如何使用中继器中的单选按钮从 sql 中检索数据?

[英]How to use Radio Button in Repeater to retrieve data from sql?

I am using Radio Buttons in Repeater but in C# Function i can't get the ID of that Radio Button:我在中继器中使用单选按钮,但在 C# 函数中我无法获得该单选按钮的 ID:

在此处输入图片说明

This Asp code Radio buttons with group in Repeater:此 Asp 代码单选按钮与中继器中的组:

在此处输入图片说明

using (SqlDataReader dr = cmd.ExecuteReader())
{
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            RadioButton1.Text = dr.GetString(5);
            RadioButton2.Text = dr.GetString(6);
            RadioButton3.Text = dr.GetString(7);
        }
    }
}

As you know your RadioButtns are in Repeater, so you have to set these buttons text from Repeater using Foreach loop as:如您所知,您的 RadioButtns 在中继器中,因此您必须使用Foreach循环将这些按钮文本从中继器设置为:

foreach (RepeaterItem item in Repeater1.Items)
{
   // Check for data item or alternating item
   if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
   {
       // find your radiobutton from repeater's item
       RadioButton RadioButton1 = item.FindControl("RadioButton1") as RadioButton;

       RadioButton1.Text = dr.GetString(5);

       //.. some other code
   }
}

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

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