将动态增量单选按钮选择的值保存到数据库中
本文关键字:保存 数据库 选择 动态 单选按钮 | 更新日期: 2023-09-27 18:26:37
我正试图将单选按钮选择的值保存到数据库中,就好像它们不是动态递增的一样,那么保存起来也没什么大不了的,但在我的情况下,比率按钮是在html
表中动态生成的。
下面我添加了一个html
代码,其中它只启动了3组单选按钮,就像我根据数据库提取行的生成了n个单选按钮一样
我想要的是,我只想保存在提交时选择的单选按钮的值。到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();
}
}
}
有两种方法可以使用控制
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 ());
同时检查msdn文档
这是我对你的情况的解决方案
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
}