Jamesla
Jamesla

Reputation: 1408

Control event not firing from within updatepanel

I have a listbox that is being updated via a timer and working as expected inside an UpdatePanel.

However I cannot get the selectedindexchanged event to fire. I presume this is something to do with the partial postback. Does anybody know what I can do to make this work?

When I move it out of the UpdatePanel it works fine. However obviously I cannot do partial postbacks.

<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
    <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="500"></asp:Timer>
    <asp:ListBox ID="ListBox_JobPositions" OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged"  runat="server" Height="750px" Width="300px" DataSourceID="sqlDataSource" DataTextField="Company" DataValueField="Pid"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>

UPDATE:

Have now tried the below change, the timer event is still working but the selectedindexchanged event is not. I am getting lost with this.

<asp:UpdatePanel ID="UpdatePanel" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
    <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="500"></asp:Timer>
    <asp:ListBox ID="ListBox_JobPositions" runat="server" Height="750px" Width="300px" DataSourceID="sqlDataSource" DataTextField="Company" DataValueField="Pid" OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged" AutoPostBack="True"></asp:ListBox>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="Timer1" />
</Triggers>

Here is the event that does not fire when the listbox is inside the UpdatePanel but does work when it is not.

protected void ListBox_JobPositions_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("test");
    }

Upvotes: 0

Views: 10866

Answers (2)

Jamesla
Jamesla

Reputation: 1408

Works now, had to manually specify Async and Full Postback triggers. Thanks for your help.

<asp:UpdatePanel ID="UpdatePanel" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
    <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="500"></asp:Timer>
    <asp:ListBox ID="ListBox_JobPositions" runat="server" Height="750px" Width="300px" DataSourceID="sqlDataSource" DataTextField="Company" DataValueField="Pid" OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged" AutoPostBack="True"></asp:ListBox>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="Timer1" />
    <asp:PostBackTrigger ControlID="ListBox_JobPositions" /> 
</Triggers>
</asp:UpdatePanel>

Upvotes: 0

nunespascal
nunespascal

Reputation: 17724

The reason you are not getting the event is that, your change event is not causing the PostBack. Your postback is caused by the timer.

The event asp.net receives is the timer event and not the ListBox event.

To resolve the issue, you should set AutoPostBack to true. This will cause the ListBox to do a PostBack as soon as the data changes and your event should fire.

<asp:ListBox ID="ListBox_JobPositions" AutoPostBack="True"  
     OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged"  
     runat="server" Height="750px" Width="300px" 
     DataSourceID="sqlDataSource" 
     DataTextField="Company" 
     DataValueField="Pid">
</asp:ListBox>

Since you have set the UpdateMode to Conditional, you should also set the ChildrenAsTriggers to true. This way way the List causes a PostBack, that too will be a partial update.

<asp:UpdatePanel ID="UpdatePanel" runat="server" 
                 UpdateMode="Conditional" 
                 ChildrenAsTriggers="True">

Upvotes: 4

Related Questions