colobusgem
colobusgem

Reputation: 473

asp:dropdownlist in my usercontrol wont postback onselectedindexchanged

I cannot get the asp:dropdown list in my usercontrol to postback to update the number of days.

The 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>

And the code behind

    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();
    }

It all sets up fine on the page load, but this callback bit doesn't fire, even the label text doesn't change.

I also tried to work it client side with adding these to the inital setup

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

the client code as:

<script type="text/javascript">

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

That doesn't fire either.

Upvotes: 1

Views: 1344

Answers (2)

Hardeep Randhawa
Hardeep Randhawa

Reputation: 47

 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>

Upvotes: 0

Adil
Adil

Reputation: 148130

You need to set AutoPostBack="true" to get the SelectedIndexChanged event, the default for DropDownList is 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.

Upvotes: 1

Related Questions