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.
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"
And if you are using Maste Pages you'll have to enable on it too.
And in the dropdown web control AutoPostback="true"