我有一个绑定到 ObjectDataSource 的 DropDown。在其数据绑定事件中,我在 0 索引上添加“--select--”值。我在页面上有一个 LinkButton,在其客户端单击时,我在下拉列表中选择不同的项目(使用 JavaScript)。假设有 3 个项目,如 --select--、option1、option2 和 option3,现在在链接按钮的客户端上单击我选择的 option3,现在如果我选择默认值“--select--”,它不会触发 SelectedIndexChanged 事件。如果我选择任何其他值,它就会触发。为什么它对默认值不起作用?
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:
基于修改后的问题 -
-
为什么不在下拉列表上设置 AppendDataBoundItems ?该属性将允许下拉列表将项目附加到现有项目上。
<asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true' EnableViewState='true' AppendDataBoundItems='true'>
<asp:ListItem Selected='True' Text='--Select--' Value='1'></asp:ListItem></asp:DropDownList>
Page_Load 方法不执行您想要的操作。即使其中之一为 true,它的 else 部分也会被执行。例如:如果“Postback is true”或“callback is true”,它将进入 else 部分。但按照 (1) 步骤中的建议,设置 AppendDataBoundItems 并删除添加“--select--”的代码。
最可能的问题是 ViewState,设置 EnableViewState="true"
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" EnableViewState="true"%>
如果您使用 Maste Pages,您也必须启用它。
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" EnableViewState="true" ClassName="Site" %>
并在下拉菜单中控制 AutoPostback="true"
<asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true'
OnSelectedIndexChanged='HandleOnDropDownListSelectedIndexChanged'>
</asp:DropDownList>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)