简体   繁体   中英

dropdown onselectedindexchanged event not firing and value kept on postback c#

I can't get the dropdown onselectedindexchanged event to fire, and when I make a selection it resets the value of the dropdown onpostback , even though I have the if (!ispostback) in the page load event.

this is a content page in a master page in asp in case that matters.

<asp:UpdatePanel runat="server">
  <ContentTemplate>                   
    <asp:DropDownList ID="EventSVCProgList" runat="server" 
      EnableViewState="true" 
      OnSelectedIndexChanged="EventSVCProgList_SelectedIndexChanged" 
      AutoPostBack="true"></asp:DropDownList>
  </ContentTemplate>
</asp:UpdatePanel>

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["CBTestDBConnectionString"].ConnectionString);

        SqlCommand eventsvcprogCMD = new SqlCommand("select*from SvcProg where eventatteligable=1", constr); // table name 
        SqlDataAdapter eventsvcadapt = new SqlDataAdapter(eventsvcprogCMD);
        DataSet eventsvcset = new DataSet();
        constr.Open();
        eventsvcadapt.Fill(eventsvcset);  // fill dataset
        EventSVCProgList.DataTextField = eventsvcset.Tables[0].Columns["SvcProgID"].ToString(); // text field name of table dispalyed in dropdown
        EventSVCProgList.DataValueField = eventsvcset.Tables[0].Columns["eventatteligable"].ToString();
        EventSVCProgList.DataSource = eventsvcset.Tables[0];      //assigning datasource to the dropdownlist
        EventSVCProgList.DataBind();  //binding dropdownlist

        constr.Close();
    }

}

protected void EventSVCProgList_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show("Eat Poop");
    var somevalue = EventSVCProgList.SelectedValue;
}

There are couple of things.

1) You need to add a Script Manager to the page at the top if not added already(it will give you a runtime error if you have not added a script Manager to the page)

2) You need to change the Update Panel content as shown below

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:DropDownList ID="EventSVCProgList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="EventSVCProgList_SelectedIndexChanged">

            </asp:DropDownList>

        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="EventSVCProgList" EventName="SelectedIndexChanged" />
        </Triggers>
    </asp:UpdatePanel>

There seems that any parent control or page leven ViewState is disabled. please check in debug mode that what value you getting for EventSVCProgList.EnableViewState and other parent controls' as well.

thanks for the help. its still not working so i'm just going to try to use javascript instead. its got to be some fundamental flaw with the way the master page or content page is setup.

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