为什么DropDownList.SelectedIndexChanged事件不火? [英] Why DropDownList.SelectedIndexChanged event does not fire?
问题描述
我有一定到一个ObjectDataSource一个下拉。在它的数据绑定的事件,我加入的0指数 - 选择 - 值。我在页面上和它的客户端点击一个LinkButton,我是在下跌中选择不同的项目下(使用JavaScript)。假设有3个项目,如:不限,选项1,选项2和选项3,现在的链接按钮的客户点击我选择的选项3,如果我现在选择默认值:不限,它不火的SelectedIndexChanged事件。如果我选择任何其他值,那么它触发。为什么它不是为默认值工作?
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(的IsPostBack&安培;!&安培;!IsCallback)
{
this.FillDropDown( - 选择---);
}
其他
{
如果(this.drp.SelectedItem!= NULL)
this.FillDropDown(this.drp.SelectedItem.Text);
其他
this.FillDropDown( - 选择---);
}
}保护无效FillDropDown(字符串的viewName)
{
this.obJectDataSource.Select(); this.drp.Items.Clear();
this.drp.SelectedIndex = -1;
this.drp.DataBind(); 如果(this.drp.Items.Count大于0)
{
列表项项= this.drp.Items.FindByText(的viewName);
如果(项目== NULL)
{
项= this.drp.Items.FindByText( - 选择---);
}
如果(项目!= NULL)
{
INT的selectedIndex = this.drp.Items.IndexOf(项目);
this.drp.Items [的selectedIndex] .Selected = TRUE;
this.drp.SelectedIndex =的selectedIndex;
}
}
}保护无效drp_OnDataBound(对象发件人,EventArgs的发送)
{
如果(this.drp.Items.Count大于0)
{
this.drp.Items.Insert(0,新的ListItem( - 选择---, - 1));
}
}保护无效drp_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
如果(drp.SelectedValue!=-1)
{
会议[的SelectedItem] = this.drp.SelectedItem.Text; }
}
///里面做回调不回发按钮< DX:ASPxCallback ID =ASPxCallback1=服务器ClientInstanceName =调用callback1OnCallback =SaveFilter_Click>
< ClientSideEvents CallbackComplete =功能(S,E){Callback1Success(S,E);}/>
< / DX:ASPxCallback>< DX:ASPxButton ID =btn_Save=服务器的CausesValidation =FALSEHEIGHT =20像素文本=保存的AutoPostBack =假UseSubmitBehavior =false的>
< ClientSideEvents点击=功能(S,E){
VAR的isValid =验证(这一点,txt1.GetText());
如果(的isValid ==真){
Callback1.PerformCallback(保存);
}
其他{
e.processOnServer = FALSE;
}}>
< / ClientSideEvents>
< / DX:ASPxButton>保护无效SaveFilter_Click(对象发件人,CallbackEventArgs E)
{
如果(e.Parameter.ToString()==保存)
{
如果(!string.IsNullOrEmpty(txt_SaveSaveSearch.Text))
{
//数据保存到数据库中。
this.FillDropDown(txt.Text);
e.Result = ASPxCallback.GetRenderResult(this.drp);
}
}
}功能Callback1Success(S,E){
VAR CTRL =的document.getElementById('ctl00_ContentHolder_drp');
ctrl.outerHTML = e.result;
}
更新:
根据修订后的问题 -
-
你为什么不设置DropDownList的AppendDataBoundItems?该物业将允许DropDownList的追加项目现有的。
< ASP:DropDownList的ID =DropDownList1RUNAT =服务器的AutoPostBack ='真正'的EnableViewState ='真'AppendDataBoundItems ='真'> < ASP:列表项选择='真'文本=' - 选择---'值='1'>< / ASP:ListItem的>< / ASP:DropDownList的>
-
在Page_Load方法不会做你想要什么。它的else部分将被执行,即使其中一人是真正的..ex:如果回传是真的或回调是真正的它会进入else部分。但正如在步骤(1)建议,设置AppendDataBoundItems并删除code以添加 - 选择 -
最有可能的问题与ViewState中,设置的EnableViewState =真正的
<%@页标题=LANGUAGE =C#的MasterPageFile =〜/ Site.master母AutoEventWireup =真codeFILE =Test.aspx文件。 CS的EnableViewState =真正的%GT;
如果你使用的是Maste网页你必须启用它。
<%@主语言=C#AutoEventWireup =真codeFILE =Site.master.cs继承=网站的EnableViewState =真正的类名=网站%GT;
和在下拉Web控件的AutoPostBack =真正的
< ASP:DropDownList的ID =DropDownList1RUNAT =服务器的AutoPostBack ='真正的'
OnSelectedIndexChanged ='HandleOnDropDownListSelectedIndexChanged'>
< / ASP:DropDownList的>
I have a DropDown which is bounded to an ObjectDataSource. On its data bound event, I am adding "--select--" value on the 0 index. I have a LinkButton on the page and on its client click, i am selecting different item on drop down (using JavaScript). Suppose there are 3 items like --select--, option1, option2 and option3 and now on link button's client click i selected option3, now if I select the default value "--select--", it does not fire the SelectedIndexChanged event. If I select any other value then it fires. Why does it not work for the default value?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && !IsCallback)
{
this.FillDropDown("--Select--");
}
else
{
if (this.drp.SelectedItem != null)
this.FillDropDown(this.drp.SelectedItem.Text);
else
this.FillDropDown("--Select--");
}
}
protected void FillDropDown(string viewName)
{
this.obJectDataSource.Select();
this.drp.Items.Clear();
this.drp.SelectedIndex = -1;
this.drp.DataBind();
if (this.drp.Items.Count > 0)
{
ListItem item = this.drp.Items.FindByText(viewName);
if (item == null)
{
item = this.drp.Items.FindByText("--Select--");
}
if (item != null)
{
int selectedIndex = this.drp.Items.IndexOf(item);
this.drp.Items[selectedIndex].Selected = true;
this.drp.SelectedIndex = selectedIndex;
}
}
}
protected void drp_OnDataBound(object sender, EventArgs e)
{
if (this.drp.Items.Count > 0)
{
this.drp.Items.Insert(0, new ListItem("--Select--", "-1"));
}
}
protected void drp_SelectedIndexChanged(object sender, EventArgs e)
{
if (drp.SelectedValue != "-1")
{
Session["SelectedItem"] = this.drp.SelectedItem.Text;
}
}
/// The button which do callback not postback
<dx:ASPxCallback ID="ASPxCallback1" runat="server" ClientInstanceName="Callback1" OnCallback="SaveFilter_Click">
<ClientSideEvents CallbackComplete="function(s,e){Callback1Success(s,e);}" />
</dx:ASPxCallback>
<dx:ASPxButton ID="btn_Save" runat="server" CausesValidation="False" Height="20px" Text="Save" AutoPostBack="false" UseSubmitBehavior="false">
<ClientSideEvents Click="function(s, e) {
var isValid = Validate(this, txt1.GetText());
if(isValid==true) {
Callback1.PerformCallback('Save');
}
else {
e.processOnServer = false;
}}">
</ClientSideEvents>
</dx:ASPxButton>
protected void SaveFilter_Click(object sender, CallbackEventArgs e)
{
if (e.Parameter.ToString() == "Save")
{
if (!string.IsNullOrEmpty(txt_SaveSaveSearch.Text))
{
// saving data into data base.
this.FillDropDown(txt.Text);
e.Result = ASPxCallback.GetRenderResult(this.drp);
}
}
}
function Callback1Success(s,e) {
var ctrl = document.getElementById('ctl00_ContentHolder_drp');
ctrl.outerHTML = e.result;
}
Update:
Based on the revised question -
Why don't you set AppendDataBoundItems on the dropdownlist? The property would allow the dropdownlist to append items to the existing ones.
<asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true' EnableViewState='true' AppendDataBoundItems='true'> <asp:ListItem Selected='True' Text='--Select--' Value='1'></asp:ListItem></asp:DropDownList>
The Page_Load method doesn't do what you want to. The else part of it will be executed even if one of them is true ..ex: if "Postback is true" or "callback is true" it would go into the else part. But as suggested in the (1) step, set the AppendDataBoundItems and remove code to add "--select--".
Most likely issue be with ViewState, Set EnableViewState="true"
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" EnableViewState="true"%>
And if you are using Maste Pages you'll have to enable on it too.
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" EnableViewState="true" ClassName="Site" %>
And in the dropdown web control AutoPostback="true"
<asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true'
OnSelectedIndexChanged='HandleOnDropDownListSelectedIndexChanged'>
</asp:DropDownList>
这篇关于为什么DropDownList.SelectedIndexChanged事件不火?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!