我有一个列表框,里面装满了 sql-server 中表中的数据。我试图找到一种方法来单击列表框中的项目,然后找到与所选项目关联的 ID。我一直在网上寻找方法来做到这一点,并在堆栈溢出中尝试了以下问题中的方法,但它们似乎都不适合我将 列表框中所选项目的值作为字符串这是我用代码填充我的列表框的方法protected void Page_Load(object sender, EventArgs e) { String Sql = @" select * from project"; SqlConnection conn = new SqlConnection(Properties.Resources.cString); SqlDataAdapter DA = new SqlDataAdapter(Sql, Properties.Resources.cString); DataSet DS = new DataSet(); DA.Fill(DS, "Project"); DataTable DT = DS.Tables["Project"]; lbProjects.DataValueField = "ProjectID"; lbProjects.DataTextField = "ProjectName"; lbProjects.DataSource = DT; lbProjects.DataBind();}列表框正在填充正确的数据。What I am trying to do is when an item is selected and a button is pressed then the selected value will be retrieved. 他是我尝试过的,但不起作用protected void btnSave_Click(object sender, EventArgs e){ String text = lbProjects.SelectedValue.ToString(); }当然,当我调试 selectedvalue 是“”对于连接到 Sql 的列表框,是否有特定的方法来执行此操作?提前致谢!更新:我也使用了 SelectedItem 并且不起作用SQL 表代码 create TABLE PROJECT(ProjectID int Not Null,ProjectName varchar (35) Not Null,ProjectDesc varchar (100) Not Null,ProjectStartDate VarChar (11)Not Null,ProjectFinishedDate Varchar (11) Not Null,SupportTeamID varchar(10) NOT NULL,leadContact int NOT NULL,leadDeveloper int NOT NULL,Constraint pk_Project Primary Key (ProjectID),Constraint fk_supportTeamID Foreign Key (supportTeamID) references supportTeam(supportTeamID))
2 回答
蓝山帝景
TA贡献1843条经验 获得超7个赞
它与我的列表框代码无关,而是与回帖有关。我将此添加到页面加载中,现在可以使用了
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack)
{
String Sql = @" select * from project";
SqlConnection conn = new SqlConnection(Properties.Resources.cString);
SqlDataAdapter DA = new SqlDataAdapter(Sql, Properties.Resources.cString);
DataSet DS = new DataSet();
DA.Fill(DS, "Project");
DataTable DT = DS.Tables["Project"];
lbProjects.DataValueField = "ProjectID";
lbProjects.DataTextField = "ProjectName";
lbProjects.DataSource = DT;
lbProjects.DataBind();
}
}
- 2 回答
- 0 关注
- 152 浏览
添加回答
举报
0/150
提交
取消