gpt4 book ai didi

c# - 尝试存储 ListView ID

转载 作者:太空宇宙 更新时间:2023-11-03 22:39:37 24 4
gpt4 key购买 nike

我试图通过复选框和其他值存储 ListView 表中记录的 ID 号,但在我点击提交按钮后出现此错误。它说有一个空值。 enter image description here

这是填充表:

void GetEmployees()
{
using (SqlConnection con = new SqlConnection(Helper.GetCon()))
{
con.Open();
string query = @"SELECT EmployeeID, FirstName, LastName, Position FROM
Employees ";
using (SqlCommand cmd = new SqlCommand(query, con))
{

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "EmployeeID");
lvEmployees.DataSource = ds;
lvEmployees.DataBind();
con.Close();
}
}
}

这里是提交点击代码:

protected void btnAddParticipants_Click(object sender, EventArgs e)
{
foreach (ListViewDataItem item in this.lvEmployees.Items)
{
string idValue = lvEmployees.DataKeys[item.DataItemIndex].Value.ToString();
if (item.ItemType == ListViewItemType.DataItem)
{
CheckBox cb = (CheckBox)item.FindControl("cbEmpPart");
if (cb.Checked)
{
using (SqlConnection con = new SqlConnection(Helper.GetCon()))
{
con.Open();
string query = @"INSERT INTO CourseParticipants VALUES (@TrainingModuleID, @EmployeeID, @Active, @DateAdded)";

using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@TrainingModuleID", txtCourseID.Text);
cmd.Parameters.AddWithValue("@EmployeeID", 1);
cmd.Parameters.AddWithValue("@Active", 1);
cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now);
cmd.ExecuteNonQuery();
//con.Close();

}
}
}
}
}
}

这是.aspx

<asp:ListView ID="lvEmployees" runat="server" OnPagePropertiesChanging="lvEmployees_PagePropertiesChanging" OnDataBound="lvEmployees_DataBound">
<ItemTemplate>
<tr>
<td><%# Eval("FirstName")%>,<%# Eval("LastName")%></td>
<td><%# Eval("Position")%></td>
<td>

<asp:CheckBox ID="cbEmployee" runat="server" />

</td>
</tr>
</ItemTemplate>
</asp:ListView>

最佳答案

首先将 DataKeyNames 添加到 aspx 页面上的 ListView。

<asp:ListView ID="lvEmployees" runat="server" DataKeyNames="EmployeeID">

关于c# - 尝试存储 ListView ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53008827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com