简体   繁体   English

dropdown onselectedindexchanged事件未触发并且值保留在回发c#

[英]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. 我无法将onselectedindexchanged事件的下拉列表触发,当我进行选择时,它会重置onpostback的下拉列表的值,即使我在页面加载事件中有if (!ispostback)

this is a content page in a master page in asp in case that matters. 这是asp中母版页中的内容页面,如果重要的话。

<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) 1)如果尚未添加脚本管理器,则需要将脚本管理器添加到顶部页面(如果尚未向页面添加脚本管理器,则会出现运行时错误)

2) You need to change the Update Panel content as shown below 2)您需要更改更新面板内容,如下所示

<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. 似乎禁用了任何父控件或页面leven ViewState。 please check in debug mode that what value you getting for EventSVCProgList.EnableViewState and other parent controls' as well. 请检查调试模式,您获取EventSVCProgList.EnableViewState和其他父控件的值。

thanks for the help. 谢谢您的帮助。 its still not working so i'm just going to try to use javascript instead. 它还没有工作,所以我只是试着用javascript代替。 its got to be some fundamental flaw with the way the master page or content page is setup. 它与主页面或内容页面的设置方式有一些根本性的缺陷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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