vb.net 数据绑定 Asp .Net 下拉列表 SelectedValue 到页面属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18917020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data Bind Asp .Net Drop Down List SelectedValue to Page Property
提问by Allan
I have a property in my Page
我的主页中有一个属性
Public Property Status as String
I set up the status list manually by calling
我通过调用手动设置状态列表
list.Items.Add(New ListItem("Open", "Open"))
and then I call
然后我打电话
list.DataBind()
On my page I want to set the selected value to the value in that property and I want the value in that property to contain the value of the list on each post back.
在我的页面上,我想将所选值设置为该属性中的值,并且我希望该属性中的值包含每次回发时列表的值。
I tried SelectedValue='<%# Bind("Status") %>'but I get the following error:
我试过了,SelectedValue='<%# Bind("Status") %>'但出现以下错误:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a data bound control.
Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。
Is there something I am missing? The end goal is to have the state of the DropDownList persisted to the Page property between post backs.
有什么我想念的吗?最终目标是在回发之间将 DropDownList 的状态持久化到 Page 属性。
Thank you.
谢谢你。
采纳答案by Sid M
You can get the selected value of dropdownlist by
您可以通过以下方式获取下拉列表的选定值
status=DropDownList1.SelectedItem.Value.ToString();
In order to get selected value after each postback,you need to add event in your dropdownlist onselectedindexchanged="DropDownList1_SelectedIndexChanged"and in that event you can get selected value as follows.
为了在每次回发后获得选定的值,您需要在下拉列表中添加事件 onselectedindexchanged="DropDownList1_SelectedIndexChanged",在该事件中您可以获得选定的值,如下所示。
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
status=DropDownList1.SelectedItem.Value.ToString();
}
you can persist the value of your status property by creating a view state as follows(in c#)
您可以通过如下创建视图状态来保留状态属性的值(在 c# 中)
public string status
{
get
{
if (ViewState["status"] != null)
return ViewState["status"].ToString();
else
return null;
}
set
{
ViewState["status"] = value;
}
}
回答by Irfan TahirKheli
The server control has viewstate that persist the value of the control between postbacks automatically as long as you don't set the viewstate property of a control to false.
只要您不将控件的 viewstate 属性设置为 false,服务器控件的 viewstate 就会自动在回发之间保留控件的值。
Second thing is you dont need to call it the DataBind() function in your case.
第二件事是你不需要在你的情况下调用它 DataBind() 函数。

