c#获取复选框所选的id
时间: 2023-10-07 19:13:22 浏览: 57
首先,在你的 HTML 页面中,你需要为每个复选框元素指定一个唯一的 ID 属性。
然后,在你的 C# 代码中,你可以使用以下代码来获取所选复选框的 ID 值:
```csharp
List<string> selectedIds = new List<string>();
foreach (var checkbox in Request.Form.AllKeys)
{
if (checkbox.StartsWith("checkbox") && Request.Form[checkbox] == "on")
{
selectedIds.Add(checkbox.Replace("checkbox", ""));
}
}
```
在上面的代码中,我们首先创建一个名为 `selectedIds` 的字符串列表,用于存储所选复选框的 ID 值。然后,我们循环遍历 `Request.Form.AllKeys` 集合中的所有键(即表单数据中的所有元素),并检查它们是否以字符串 `"checkbox"` 开头。如果是,我们将检查该元素是否被选中(即,其值是否为 `"on"`)。如果是,则将该复选框的 ID 值添加到 `selectedIds` 列表中。
最后,你可以使用 `selectedIds` 列表中的值来执行其他操作,如更新数据库记录或发送电子邮件等。
相关问题
wincc7的常用c语言,[转载]23.Wincc V7.3 复选框使用C语言控制趋势显示和隐藏
答:WinCC 7 的常用 C 语言包括:
1. WinCC API 函数:用于与 WinCC 进行交互,例如读取变量值、写入变量值、启动/停止趋势等。
2. C# 语言:WinCC 7 支持使用 C# 语言进行编程,通过编写 WinCC 控件的事件处理程序,实现与 WinCC 的交互。
3. VBScript:WinCC 7 还支持使用 VBScript 进行编程,可以通过编写 VBScript 脚本实现与 WinCC 的交互。
关于您提到的 WinCC 7.3 中使用 C 语言控制复选框显示和隐藏的方法,可以参考以下代码:
```
// 获取复选框对象
ITouchCheckButtonPtr pCheckButton = ITouchCheckButtonPtr(pTrend->FindChild(ID_CHECKBOX));
// 控制复选框显示和隐藏
if (pCheckButton != NULL) {
if (bShow) {
pCheckButton->Show();
} else {
pCheckButton->Hide();
}
}
```
其中,pTrend 是趋势控件对象,ID_CHECKBOX 是复选框控件的 ID。通过获取复选框对象,然后通过 Show() 和 Hide() 方法控制其显示和隐藏。
而变化 3.通过按钮使用sql语句使datagridview复选框选中行的第三列等于前两列之和 4.点击datagridview第11行使第八列的值等于前两列的和,并且第10列的时间等于今天的年月日,使用update语句把数据更新到数据库
实现上述功能,需要编写 C# 代码来操作 DataGridView 和数据库。
首先,在窗体上添加一个 DataGridView 控件,并设置其 DataSource 属性为数据源。
然后,在窗体加载事件中,通过 SQL 查询语句从数据库中获取数据,并将其绑定到 DataGridView 控件上:
```csharp
private void Form1_Load(object sender, EventArgs e)
{
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=TestDB;Integrated Security=True";
string query = "SELECT * FROM MyTable";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
dataGridView1.DataSource = dataTable;
}
```
接下来,为 DataGridView 控件的 CellContentClick 事件添加处理程序,以实现复选框选中行的第三列等于前两列之和的功能:
```csharp
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0) // 判断是否点击了复选框列
{
int row = e.RowIndex;
int sum = Convert.ToInt32(dataGridView1.Rows[row].Cells[1].Value) + Convert.ToInt32(dataGridView1.Rows[row].Cells[2].Value);
dataGridView1.Rows[row].Cells[3].Value = sum;
}
}
```
最后,为 DataGridView 控件的 CellDoubleClick 事件添加处理程序,以实现点击行使第八列的值等于前两列的和,并且第十列的时间等于今天的年月日,并且使用 update 语句将数据更新到数据库的功能:
```csharp
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int row = e.RowIndex;
int sum = Convert.ToInt32(dataGridView1.Rows[row].Cells[1].Value) + Convert.ToInt32(dataGridView1.Rows[row].Cells[2].Value);
dataGridView1.Rows[row].Cells[7].Value = sum;
dataGridView1.Rows[row].Cells[9].Value = DateTime.Today.ToString("yyyy-MM-dd");
int id = Convert.ToInt32(dataGridView1.Rows[row].Cells[0].Value);
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=TestDB;Integrated Security=True";
string query = "UPDATE MyTable SET Column2=@Column2, Column8=@Column8, Column10=@Column10 WHERE Id=@Id";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Column2", dataGridView1.Rows[row].Cells[1].Value);
command.Parameters.AddWithValue("@Column8", dataGridView1.Rows[row].Cells[7].Value);
command.Parameters.AddWithValue("@Column10", dataGridView1.Rows[row].Cells[9].Value);
command.Parameters.AddWithValue("@Id", id);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
```
以上代码中,需要将 connectionString 替换为自己的数据库连接字符串,以及将 MyTable、Column2、Column8、Column10 和 Id 替换为自己的表名和列名。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)