简体   繁体   中英

AutoPostback for DropDownList in asp.net

When I set AutoPostback=True for DropDownList, then only the SelectedIndexChanged event is fired otherwise not...

I want to fire the event even when AutoPostback=false....

Is there any solution for this...

 <asp:DropDownList ID="SlotDuration_DDL" runat="server" Style="color: #727272 !important; font-size: 24px; font-weight: 100;" CssClass="span2" OnSelectedIndexChanged="SlotDuration_DDL_SelectedIndexChanged">

      <asp:ListItem>10</asp:ListItem>
      <asp:ListItem>15</asp:ListItem>
      <asp:ListItem>20</asp:ListItem>
      <asp:ListItem>25</asp:ListItem>
      <asp:ListItem>30</asp:ListItem>
      <asp:ListItem>35</asp:ListItem>
      <asp:ListItem>40</asp:ListItem>
      <asp:ListItem>45</asp:ListItem>
      <asp:ListItem>50</asp:ListItem>
      <asp:ListItem>55</asp:ListItem>
      <asp:ListItem>60</asp:ListItem>
  </asp:DropDownList>

code behind

protected void SlotDuration_DDL_SelectedIndexChanged(object sender, EventArgs e)
{
   DateTime dt = DateTime.Parse(StartDate_TB.Text);
        int n = Int32.Parse(EventDuration_DDL.SelectedItem.ToString());
        for (int i = 0; i < n; i++)
        {
            Label NewLabel = new Label();
            NewLabel.ID = "Label" + i;
            var eventDate = dt.AddDays(i); //Calendar1.SelectedDate.Date.AddDays(i);
            NewLabel.Text = eventDate.ToLongDateString();

            CheckBox newcheck = new CheckBox();
            newcheck.ID = "CheckBox" + i;

            this.Labeldiv.Controls.Add(new LiteralControl("<span class='h1size'>"));
            this.Labeldiv.Controls.Add(NewLabel);
            this.Labeldiv.Controls.Add(new LiteralControl("</span>"));
            this.Labeldiv.Controls.Add(new LiteralControl("<div class='make-switch pull-right' data-on='info'>"));
            this.Labeldiv.Controls.Add(newcheck);
            this.Labeldiv.Controls.Add(new LiteralControl("</div>"));
            this.Labeldiv.Controls.Add(new LiteralControl("<br/>"));

        }
 }

SelectedIndexChanged event is fired even if you set AutoPostback to false. But the page does not postback immediately.

So If the user change the selection of the DDL and then (possibly after doing other things in the page) clicks in a button submiting the page ( postback ) The handler for this event will be called.

Thats possible, but not using events at code behind. You will need to try using JS/JQuery.

Check here .

I think you want to set autopostback to false because the whole page is refreshing, to overcome this issue you can use update panel . keep drop down in updatepanel and set autopostback to true. only the portion inside the updatepanel will go to server and will call selectedindexchanged event and your page will not refresh. below link will give you some idea about update panel http://geekswithblogs.net/ranganh/archive/2007/05/16/112525.aspx

I want to select state after selecting state from the drop down it will bind the city drop down data.But Now if I select State then it will not show selected State name. What is the Problem Please any one help me.Following is the code

<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlState_SelectedIndexChanged"></asp:DropDownList>

protected void Page_Load(object sender, EventArgs e) {

        if (!Page.IsPostBack)
        {
            BindStateList();
        }

}

protected void ddlState_SelectedIndexChanged(object sender, EventArgs e) { try { SqlDataAdapter adp = new SqlDataAdapter("select cityname from tblCityWithState With(NOLOCK) where statename='" + ddlState.SelectedValue.ToString() + "' order by cityname asc", con); DataSet ds = new DataSet(); adp.Fill(ds); ddlCity.DataSource = ds; ddlCity.DataTextField = "cityname"; ddlCity.DataValueField = "cityname"; ddlCity.DataBind(); ddlCity.Items.Insert(0, new ListItem("--Select--", "0")); ds.Dispose(); adp.Dispose();

    }
    catch (Exception ex)
    {
        ex.Message.ToString();
    }
}

are you sure control is under From tag, also try to test it with hard coded values

 <form ID="form1" runat="server">
  ....dropdown
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM