mike44
mike44

Reputation: 812

Code not reaching SelectedIndexChanged event

I've a user control contined in MyPage.aspx. The user control contains few drop-down lists; each with Autopostback = true but when I run the code & change the drop-down item, other events gets fired but not SelectedIndexChanged.

<asp:DropDownList ID="ddPages1" runat="server" EnableViewState="true" AutoPostBack="true"
    onselectedindexchanged="ddPages1_SelectedIndexChanged">
</asp:DropDownList>

Code behind of ascx:

protected void ddPages1_SelectedIndexChanged(object sender, EventArgs e)
{
    ...
}

ascx also has ReportViewer & I'm populating number of pages in the report into drop-down list.

protected override void Render(HtmlTextWriter writer)
{
    TotalPages = ReportViewer1.LocalReport.GetTotalPages();
    txtPageCount1.Text = Convert.ToString(TotalPages);
    if (TotalPages > 0)
    {
        for (int i = 1; i <= TotalPages; i++)
        {
            ListItem listItem = new ListItem();
            listItem.Value = i.ToString();
            listItem.Text = i.ToString(); 
            ddPages1.Items.Add(listItem);
        }  
    }
    base.Render(writer);
 }

Upvotes: 0

Views: 444

Answers (3)

Shiridish
Shiridish

Reputation: 4962

You have the set the Autopostback = true on your dropdownlist, but probably you dint put the Autopostback = true on your usercontrol

Upvotes: 0

nick_w
nick_w

Reputation: 14938

Try this:

Where you have ddPages1.Items.Add(listItem);, replace it with

if(!this.IsPostBack)
{
    ddPages1.Items.Add(listItem);
}

The problem you could be having here is that by populating the list every time, you will be losing the selection.

Upvotes: 1

BuddhiP
BuddhiP

Reputation: 6451

Please provide more information? How do you bind to the SelectedIndexChanged event? Are you binding it programmatically? or using the declarative form?

Have you enabled ViewState?

Upvotes: 0

Related Questions