Doubt with PostBack and SelectedIndexChanged event

0

First of all, I apologize because I can not present code. The problem is as follows in an asp.net webforms application, I have 2 listboxes and each one has the SelectedIndexChanged event, (the code within those events is simply moving one item from one list to the other and vice versa) which are handled when there is PostBack and when some element is selected. I have a simple button with the click event but without code, let's say it simply serves to make PostBack. Now, the problem arises because I have another button which activates another event but if I have an item selected from any of the listbox, what is executed is first the SelectedIndexChanged event and then the event that the second button calls and actually I just want the second button event to be executed, I mean? How could I avoid this?

As you can see in this example image, if I click on the Save button, having selected an item from the listbox on the left, what the application does is call the SelectedIndexChanged event of that listbox and then after that call the Save event, and this works like that because of the PostBack issue. Is there any good way to implement that to avoid that?

    
asked by Popplar 05.05.2017 в 03:28
source

4 answers

0

You could make use of the UpdatePanel control of the Ajax extensions (which is already pre-installed in Visual Studio), and are used to postback only in the portion of the page that they enclose, accommodate your ListBox and UpdatePanel something like this:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
                    <asp:ListItem Value="0">Item 1</asp:ListItem>
                    <asp:ListItem Value="1">Item 2</asp:ListItem>
                </asp:ListBox>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="ListBox2" EventName="SelectedIndexChanged" />
            </Triggers>
        </asp:UpdatePanel>
        <br />
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox2_SelectedIndexChanged">
                    <asp:ListItem Value="0">Item 3</asp:ListItem>
                    <asp:ListItem Value="1">Item 4</asp:ListItem>
                </asp:ListBox>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="ListBox1" EventName="SelectedIndexChanged" />
            </Triggers>
        </asp:UpdatePanel>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Now I explain:

  • The ScriptManager is required to use the UpdatePanel.
  • Note that these have the property UpdateMode="Conditional" that serves to determine which events will trigger the postback within them.
  • Put the property AutoPostBack="True" in the ListBox since by default it is false and is necessary to send data to the server and get the answer.
  • The tags "Triggers" and "asp: AsyncPostBackTrigger" were set to determine which event will trigger the UpdatePanel update. (the controls with autopostback inside your UpdatePanel will trigger the event by itself without needing to identify it in the Triggers tag) And because to update the ViewState it is necessary to do postback, so both ListBox depend on one and the other to change their view. Adapt it to your ASP code and tell me if it worked. Greetings.
answered by 11.05.2017 / 01:08
source
1

The problem is that the code is not being put in the correct event handler.

As you explain, you are putting code in the SelectedIndexChanged event of the ListBox that you want to run when you click on a certain button if there is an element selected in the ListBox.

If you want the code to run when you click on a certain button, the logic is that the code is in the click event handler of the button.

If, on the other hand, you would like to have a code that will be executed each time the user selects a different option in the ListBox, what you should do is put the AutoPostBack property of the ListBox to true and use the SelectedIndexChanged event of it.

I understand that in your case we are in the first scenario. Therefore, you should remove the handler of the SelectedIndexChanged event and include the code in the handler of the Click event of the button, making sure that there is a selected element in the ListBox.

    
answered by 08.05.2017 в 08:12
0

We are in this scenario:

  • 2 Listbox with its own SelectedIndexChanged and the active PostBack event
  • 1 button that does something else with Active PostBack

Pressing the button executes the SelectedIndexChanged method and then the button itself.

Check:

  • That the two ListBox have the PostBack active

  • That the OnLoad event does not call any of the SelectedIndexChanged events

  • That the OnClick event of the button does not call one of the SelectedIndexChanged methods of the listbox by mistake

answered by 08.05.2017 в 11:50
0
  

Popplar: Now, the problem arises because I have another button which   activates another event but if I have an item selected from some   of the listbox, what is executed is first the event of   SelectedIndexChanged and then the event that the second button calls and   actually I just want the second button event to run,   explain? How could I avoid this?

Notice if the Page_Load event is calling the SelectedIndexChanged event or any method that is moving the item by selecting the other list.

After the post-back, first the Page_Load is executed and then the button event (eg Boton_Click).

I hope it serves you!

    
answered by 14.05.2017 в 03:29