在我的保存功能,我如何设置我的Dropdownlist显示最近插入的PK?如何将dropdownlist设置为刚插入的pk值?
try{
//saving stuff where pk is inserted
con.close();
DropDownListTug.Items.Clear();
DropDownListTug.DataBind();
DropDownListTug.SelectedValue = "SCOPE_IDENTITY();"; //???
}
在我的保存功能,我如何设置我的Dropdownlist显示最近插入的PK?如何将dropdownlist设置为刚插入的pk值?
try{
//saving stuff where pk is inserted
con.close();
DropDownListTug.Items.Clear();
DropDownListTug.DataBind();
DropDownListTug.SelectedValue = "SCOPE_IDENTITY();"; //???
}
您可以使用
SELECT IDENT_CURRENT('Tablename')
注意
在这种情况下,我认为使用SCOPE_IDENTITY()
更适合作为界面污物建议。
UPDATE
要更新您的下拉列表,你需要填充您的下拉列表中page_load
事件,我告诉你,在我的意见,看看最近发生的变化,并设置默认值。 当你点击按钮发布回看,你的页面重新加载,你的代码再次绑定下拉列表(必须在page_load事件中),所以要为你的dropdownlist
设置一个默认值,你需要在Page_load
事件中设置它的SelecedValue
。
只需添加SELECT SCOPE_IDENTITY()
线插入查询,如下所示。
// Sample sql insert statement
INSERT INTO Test(TestId,Des)
VALUES(@testId, @des);
SELECT SCOPE_IDENTITY()
然后应该ExecuteScalar()
执行插入查询作为follows.It将返回插入的PK值。
Int32 newId = (Int32) myCommand.ExecuteScalar();
DropDownListTug.SelectedValue = newId.ToString() ;
我想你错过了c#标签 – liquidsnake786
取决于_“//保存插入pk的东西 ”_的实现。 – CodeCaster
@ New2This你必须在'Page_Load'事件中赋值'SelectedValue'。但是提供你的代码或者至少要解释场景 –