asp:dropdownlist 在我的用户控件中不会回发 onselectedindexchanged

asp:dropdownlist in my usercontrol wont postback onselectedindexchanged

我无法让我的用户控件中的 asp:dropdown 列表回发以更新天数。

用户控件:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Profile.ascx.cs" Inherits="TestApp2.Views.Profile" %>


<asp:UpdatePanel ID="ProfileMainUpdatePanel" runat="server">
        <ContentTemplate>
            <div class="form-group profile-form">
                <asp:DropDownList runat="server" id="selYear" class="form-control profile-date right-align" OnSelectedIndexChanged="selYear_SelectedIndexChanged" />
                <asp:DropDownList runat="server" id="selMonth" class="form-control profile-date right-align" OnSelectedIndexChanged="selMonth_SelectedIndexChanged"  />
                <asp:DropDownList runat="server" id="selDay" class="form-control profile-date right-align"/>
                <asp:label runat="server" class="control-label right-align profile-form-label" for="selDay" Text="Date of Birth" ID="temp" />
            </div>
        </ContentTemplate>
</asp:UpdatePanel>

以及背后的代码

    public void fillDays()
    {
        selDay.Items.Clear();
        //getting numbner of days in selected month & year
        int noofdays = DateTime.DaysInMonth(Convert.ToInt32(selYear.SelectedValue), Convert.ToInt32(selMonth.SelectedValue));

        //Fill days
        for (int i = 1; i <= noofdays; i++)
        {
            selDay.Items.Add(i.ToString());
        }
        selDay.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected = true;// Set current date as selected

    }

    protected void selMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        temp.Text = "here";
        fillDays();
    }

    protected void selYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        temp.Text = "there";
        fillDays();
    }

在页面加载时一切都设置得很好,但是这个回调位没有触发,甚至标签文本也没有改变。

我还尝试通过将这些添加到初始设置来在客户端工作

selYear.Attributes.Add("onchange", "fillDays();");
selMonth.Attributes.Add("onchange", "fillDays();");

客户端代码为:

<script type="text/javascript">

function fillDays() {
    alert("yes");
}

那也不火。

您需要设置AutoPostBack="true"来获取SelectedIndexChanged事件,DropDownList默认为false

<asp:DropDownList runat="server" id="selYear" AutoPostBack="true" class="form-control profile-date right-align" OnSelectedIndexChanged="selYear_SelectedIndexChanged"  />

AutoPostBack

Gets or sets a value indicating whether a postback to the server automatically occurs when the user changes the list selection, MSDN.

 try this in Update Panel. Hope its working...


  <asp:UpdatePanel ID="ProfileMainUpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
    <ContentTemplate>
        <div class="form-group profile-form">
            <asp:DropDownList runat="server" id="selYear" class="form-control profile-date right-align" OnSelectedIndexChanged="selYear_SelectedIndexChanged" />
            <asp:DropDownList runat="server" id="selMonth" class="form-control profile-date right-align" OnSelectedIndexChanged="selMonth_SelectedIndexChanged"  />
            <asp:DropDownList runat="server" id="selDay" class="form-control profile-date right-align"/>
            <asp:label runat="server" class="control-label right-align profile-form-label" for="selDay" Text="Date of Birth" ID="temp" />
        </div>
    </ContentTemplate>