【问题标题】:Save dynamic increment radio button selected value to data base将动态增量单选按钮选定的值保存到数据库
【发布时间】:2015-12-15 22:36:30
【问题描述】:

我正在尝试将单选按钮选择的值保存到数据库中,就好像它们没有被动态认定那样保存起来没什么大不了的,但是在我的情况下,比率按钮是在 html 中动态生成的表。

下面我添加了一个html 代码,其中它只有 3 组单选按钮,就像我有 n 个单选按钮生成取决于数据库获取行 s

我想要的是我只想保存提交时选择的单选按钮的值。到Mysql

<table id="attendence_div" width="100%" align="center" cellpadding="2" cellspacing="2" border="0">
<tr align="left" style="background-color:#004080;color:White;">
            <td> Student Name</td>                        
            <td>Present</td>            
            <td>Absent</td>       
                <td>Leave</td>                    
        </tr>
        <tr>
        <td>ANITHA S</td>
        <td><input type="radio" name="Present0" value="Present"></td>
        <td><input type="radio" name="Present0" value="Absent"></td>
        <td><input type="radio" name="Present0" value="Leave"></td>
        </tr>
        <tr>
        <td>ANITHA T C</td>
        <td><input type="radio" name="Present1" value="Present"></td>
        <td><input type="radio" name="Present1" value="Absent"></td>
        <td><input type="radio" name="Present1" value="Leave"></td>
        </tr>
        <tr>
        <td>BINDU K V</td>
        <td><input type="radio" name="Present2" value="Present"></td>
        <td><input type="radio" name="Present2" value="Absent"></td>
        <td><input type="radio" name="Present2" value="Leave"></td>
        </tr>
        </table>

C#代码

protected void Submit(object sender, EventArgs e)
{
   string present = Present0.Checked;
    string absent = Present1.Checked;
      string Leave = Present2.Checked;

    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        using (MySqlCommand cmd = new MySqlCommand("INSERT INTO attendence(absent, present, Leave ) VALUES(@absent, @present,@Leave)"))
        {
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@absent", absent);
            cmd.Parameters.AddWithValue("@present", present);
             cmd.Parameters.AddWithValue("@Leave", Leave);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

【问题讨论】:

  • 你试过asp radioButtonList控件吗
  • @FatihSert 我是新手,我真的不知道该怎么做:(如果你能帮助我,我会非常感谢你

标签: c# asp.net


【解决方案1】:

有两种方法可以使用控件

1 .您可以在 .aspx 中的单选按钮列表下添加为子项

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
   <asp:ListItem>ColdFusion</asp:ListItem>
   <asp:ListItem>Asp.Net</asp:ListItem>
   <asp:ListItem>PHP</asp:ListItem>
</asp:RadioButtonList>

2 。您可以在 C# 代码中添加其他方式

2 .1 在你的 .aspx 中添加控件定义

<asp:RadioButtonList ID="RadioButtonList1" runat="server"> </asp:RadioButtonList>

2.2可以在c#代码中动态添加

RadioButtonList1.Items.Add(new ListItem ());

Also check msdn document

这里是我针对您的情况的解决方案

ASPX

   <asp:Table runat="server" ID="table">
                <asp:TableHeaderRow BackColor="#004080" ForeColor="White">
                    <asp:TableHeaderCell runat="server" ID="header2" Text="Header2" Width="200px" />
                    <asp:TableHeaderCell runat="server" ID="header3" Text="Header3" Width="200px" />
                    <asp:TableHeaderCell runat="server" ID="Header4" Text="Header4" Width="200px" />
                </asp:TableHeaderRow>
            </asp:Table>
            <asp:Button runat="server" ID="submit" OnClick="submit_Click" Text="submit" />

C#

 private const string radioButtonGroupNamePrefix = "radioButtonGroupName";
        private const string radioButtonIDPrefix = "radioButton";

        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeRadioButtonList();
        }

        private void InitializeRadioButtonList()
        {
          // you can fetch data here and then use.
          // for example returns 5 rows from database then you must change "for" syntax 
            for (int i = 0; i < 4; i++)//rows count
            {
                var row = new TableRow();

                for (int j = 0; j < 3; j++)
                {
                    var cell = new TableCell();

                    var radioButton = new RadioButton();
                    radioButton.ID = string.Format("{0}_{1}_{2}", radioButtonIDPrefix, i,j);
                    radioButton.Text = radioButton.ID;
                    radioButton.GroupName = string.Format("{0}_{1}", radioButtonGroupNamePrefix, i);//same gruop name for same row
                    cell.Controls.Add(radioButton);

                    row.Cells.Add(cell);
                }
                table.Rows.Add(row);
            }
        }


        protected void submit_Click(object sender, EventArgs e)
        {
            foreach (var item in Request.Form.AllKeys)
            {
                if (item.Contains(radioButtonGroupNamePrefix))
                {
                    var radioButton = (FindControl(item) as RadioButton);
                }
            }
            //TODO : DB operations
        }

【讨论】:

  • 如果可能的话,你能告诉我一个例子,让我的代码为上述按钮点击事件插入数据到数据库
  • 抱歉@Shaik 现在不行,因为我正在写电话,写代码太难了。我会在 6 小时内尝试添加示例
  • 当然,但不要忘记等待你,感谢您的支持
  • 没问题,但要知道我今天又问了一个问题,他们也给了我同样的建议,他们让我使用中继器希望我可以分享我的屏幕
  • @Shaik 我为您的情况添加了解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 2018-04-15
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
相关资源
最近更新 更多