[英]How to get SelectedValue of DropdownList without postback or updatepanel
I have a DropDownList
which i am binding on page load. 我有一个
DropDownList
,我在页面加载时绑定。 i dont have any buttons or anything. 我没有任何按钮或任何东西。 as soon as user selects the value in dropdown i need to show that value in label.
用户在下拉列表中选择值后,我需要在标签中显示该值。 i am not sure why this is not working.
我不确定为什么这不起作用。 please help.
请帮忙。
public string SelectedStore { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindStoresList();
}
}
protected void BindStoresList()
{ storeDDList.AppendDataBoundItems = true;
storeDDList.Items.Add(new ListItem("Select store", "-1"));
TempCollection stores = TempDataSource.LoadForCriteria("ALL", "Code ASC");
storeDDList.DataSource = stores;
storeDDList.DataTextField = "DisplayName";
storeDDList.DataValueField = "Code";
storeDDList.DataBind();
}
protected void storeDDList_SelectedIndexChanged(object sender, EventArgs e)
{
SelectedStore = storeDDList.SelectedValue.ToString();
selectedItem.Text = SelectedStore;
}
I dont need any kind of jquery stuff as i am going to add gridview which binds depending on the value of dropdown.. 我不需要任何jQuery的东西,因为我要添加的gridview绑定到下拉列表的值。
****** EDITS *******
if i set AutoPostBack=True
then on page refresh my DropDownList doesn't bind at all as you can see in Page_Load Method, it will not call BindStoresList()
method. 如果我将
AutoPostBack=True
设置AutoPostBack=True
则在页面刷新时,我的DropDownList完全不会绑定,正如您在Page_Load方法中看到的那样,它将不会调用BindStoresList()
方法。
***** ANSWER *****
For people who might get stuck with this.. 对于那些可能会为此卡住的人。
i was setting the EnableViewState
to True
for the DropDownList
, so after page refreshes the SelectedValue
was getting lost. 我将
DropDownList
的EnableViewState
设置为True
,因此页面刷新后, SelectedValue
丢失了。 after removing the EnableviewState
and setting AutoPostBack
to Ture
working fine... 在删除
EnableviewState
并将AutoPostBack
设置为Ture
正常工作之后...
You can Use JavaScript
. 您可以使用
JavaScript
。 Set the OnChange
attribute for your DropDownList to call a JS function and Change your label text there: 为您的DropDownList设置
OnChange
属性以调用JS函数并在此处更改标签文本:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindStoresList();
storeDDList.Attributes["onChange"] = "ChangeLabelText();";
}
}
JS
function : JS
功能:
<script type="text/javascript">
function ChangeLabelText() {
var lbl = document.getElementById("<%=lbl.ClientID%>");
var ddl = document.getElementById("<%=ddl.ClientID%>");
lbl.innerHTML = ddl.options[ddl.selectedIndex].text;
}
</script>
您必须为下拉列表设置AutoPostBack=True
,它将自动将呼叫发送到服务器端,而无需额外的按钮。
You may do it using Javascript, handle the OnChange event of the DropDownList and set the text of the label you want 您可以使用Javascript进行处理,处理DropDownList的OnChange事件并设置所需标签的文本
<asp:DropDownList ID="ddl" runat="server" onchange="ddl_change(this.value)"/>
<script language="javascript" type="text/javascript">
function ddl_change(value)
{
var lbl = document.getElementById('<%= yourlabel.ClientID %>');
lbl.value = value;
}
</script>
Good luck. 祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.