我的usercontrol中的asp:dropdownlist不会回发onselectedindexchanged

时间:2015-02-12 10:43:50

标签: c# asp.net user-controls updatepanel postback

我无法在我的usercontrol中获取asp:下拉列表以回发更新天数。

usercontrol:

<%@ 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");
}

这也不会开火。

2 个答案:

答案 0 :(得分:1)

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

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

<强>的AutoPostBack

  

获取或设置一个值,该值指示是否回发到服务器   当用户更改列表选择MSDN时自动发生。

答案 1 :(得分:0)

 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>