C# 表单提交后如何显示dropdownlist中的选定值
我的表格中有一个下拉列表。提交表单后,我不会从UI中的下拉列表中获取所选值。我只得到第一个值。如何实现这一点。如何在表单提交后显示下拉选择的值。您需要使用ViewState或Sessions来保存变量并重新使用它,它看起来像这样:C# 表单提交后如何显示dropdownlist中的选定值,c#,C#,我的表格中有一个下拉列表。提交表单后,我不会从UI中的下拉列表中获取所选值。我只得到第一个值。如何实现这一点。如何在表单提交后显示下拉选择的值。您需要使用ViewState或Sessions来保存变量并重新使用它,它看起来像这样: private void Page_Load() { //If page is not being loaded for first time then set the value another //option would be
private void Page_Load()
{
//If page is not being loaded for first time then set the value another
//option would be to check if ViewState["dropdownvalue"] == null and if
//not then set the value
if (Page.IsPostBack)
{
DropDownList1.SelectedValue = ViewState["dropdownvalue"]
}
}
protected void Submit_Click(object sender, EventArgs e)
{
ViewState["dropdownvalue"] = DropDownList1.SelectedValue;
// Do everything else
}
感谢您的回复,但我想在表单提交后显示下拉选择值。您想在离开页面后保留选择值吗?在这种情况下,使用会话基本上是在页面之间传输数据的一种方式。(我在Global.asax中定义了我的会话,但我不完全确定它的必要性,只是我的一种实践)。让我知道这是否是你正在寻找的,我会在页面加载下拉选择值消失后更新我的答案。我想在下拉界面中显示选定的下拉列表值,如selected=selected。您需要在会话中保存下拉列表的值(现在我认为ViewState会更好),并且在重新加载页面时,将下拉列表值设置为会话值