我编写了一个代码来检索用户单击的动态链接按钮的 id。但是,它显示了所有链接按钮的 ID。
请检查我的代码。
if (reader.HasRows)
{
while (reader.Read())
{
JobDesignation = reader.GetString(0);
JobDescription = reader.GetString(1);
NoOfVacancies = Convert.ToString(reader.GetInt32(2));
DatePosted = Convert.ToString(reader.GetDateTime(3)).Replace("00:00:00", "");
jobId = reader.GetString(4);
int tblRows = 1;
int tblCols = 1;
Table tbl = new Table();
PlaceHolder1.Controls.Add(tbl);
for (int i = 0; i < tblRows; i++)
{
readerrowcount = readerrowcount + 1;
TableRow tr = new TableRow();
tr.CssClass = "rowStyle1";
for (int j = 0; j < tblCols; j++)
{
TableCell tc = new TableCell();
tc.CssClass = "cellStyle1";
System.Web.UI.WebControls.Label txtBox = new System.Web.UI.WebControls.Label();
txtBox.Text = "Job ID:" + jobId + "<br />" + "Job Designation:" + JobDesignation + "<br />" + "Job Description:" + JobDescription + "<br />" + "Vacancies:" + NoOfVacancies + "<br />" + "Ad Posted On:" + DatePosted + "<br />"+"";
tc.Controls.Add(txtBox);
tr.Cells.Add(tc);
System.Web.UI.WebControls.LinkButton lbView = new System.Web.UI.WebControls.LinkButton();
lbView.Text = "<br />" + "Apply for this Job";
lbView.OnClientClick = "return RedirectTo('" + id + "');";
lbView.ID = "linkButton" + readerrowcount;
ID = readerrowcount;
tc.Controls.Add(lbView);
tr.Cells.Add(tc);
}
tbl.Rows.Add(tr);
string query = "INSERT INTO EmployeeJobTagging VALUES('" +ID + "','" + id + "','"+jobId+"','"+JobDesignation+"')";
SqlCommand sqlCmd = new SqlCommand(query, con);
sqlCmd.ExecuteNonQuery();
}
ViewState["dynamictable"] = true;
} reader.NextResult();
}
这里我显示了两行,如果用户点击第一行
“申请这份工作”
我希望将 ID 为“1”传递到数据库,以便只有 1 行插入到 EmployeeJobTagging 表中。但在这里,这两行分别以 ID= 1 和 2 插入。
如下请见。
请给我一些帮助。