0

I am currently working on an old WebForms application in VB.NET. I am supposed to add a dropdown to an existing page that has been working with all controls so far. This dropdown appears in the designer and also at runtime, but when I change the selected item, the entire page gets cleared (blank white page); there is no error message. Since the page was working before, I assume the problem must be with the new dropdown.
In the CodeBehind, I also set a breakpoint at Debug.WriteLine, but no event is being triggered.
So far, I did not writy any other code that interacts with this dropdown.
If I use AutoPostBack="false", the page does not reload, but then the SelectedIndexChanged event does not fire either.

Where is the error?

    <asp:DropDownList ID="ddlSomething" runat="server" 
        BackColor="#fff4e3" Font-Names="Verdana" Font-Size="12px"
        AutoPostBack="true">
        <asp:ListItem Text="unbekannt" Value="0"></asp:ListItem>
        <asp:ListItem Text="enthalten" Value="1"></asp:ListItem>
        <asp:ListItem Text="nicht enthalten" Value="2"></asp:ListItem>
    </asp:DropDownList>

Protected WithEvents ddlSomething As Global.System.Web.UI.WebControls.DropDownList

Protected Sub ddlSomething_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSomething.SelectedIndexChanged
        Debug.WriteLine(ddlSomething.SelectedValue)
        Debug.WriteLine(ddlSomething.SelectedItem)
End Sub

Edit
I also tried an AJAX approach.

      <asp:ScriptManager runat="server" ID="ScriptManager1" />
        <asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="True">
            <ContentTemplate>
                <asp:Label ID="lblsomething" runat="server" Text="some text:"
                    Font-Names="Verdana" Font-Size="11px"></asp:Label>
                <asp:DropDownList ID="ddlSomething" runat="server"
                    BackColor="#fff4e3" Font-Names="Verdana" Font-Size="12px"
                    AutoPostBack="True" OnSelectedIndexChanged="ddlSomething_SelectedIndexChanged">
                    <asp:ListItem Text="unbekannt" Value="0"></asp:ListItem>
                    <asp:ListItem Text="enthalten" Value="1"></asp:ListItem>
                    <asp:ListItem Text="nicht enthalten" Value="2"></asp:ListItem>
                </asp:DropDownList>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="ddlSomething" EventName="SelectedIndexChanged" />
            </Triggers>
        </asp:UpdatePanel>

and

Protected Sub ddlSomething_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSomething.SelectedIndexChanged
        Debug.WriteLine(ddlSomething.SelectedValue)
        Debug.WriteLine(ddlSomething.SelectedItem)
End Sub
2
  • if you don't want it to postback you would need to do an ajax request instead Commented Aug 27 at 9:58
  • I edited my question, I also tried it with AJAX. @Pete Commented Aug 27 at 10:17

2 Answers 2

1

Is the Page_Load function called, when you change the value?

Maybe you have to check in the Page_Load function if it is a postback or not and not reset the other values of the page.

in c#, you can do that with

if (!Page.IsPostBack)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @hasi05, when I change the item in the dropdown, Page_Load is not called again, but the page is cleared.
I didn't use the UpdatePanel for that test.
Page load ALWAYS ALWAYS runs first on ANY post-back, and THEN the button click or changed index code stub runs. So, YES page load event runs each and EVERY time on page post-back. In fact, while it's common to "setup" and "load up" controls in the page load event? You MUST use the If not IsPostBack then " setup code goes here" code stub. You can't build and make a correct working page if you setup code in page load event is not wrapped inside of the If Not IsPostBack code stub. For any button, any dropdown, or any other control event? Page load fires every time before event code.
0

I solved the problem. The page is actually being called from another page. And its Page_Load was executed there every time.

In that method, there is a large If construct checking various controls, and I had to add my control to that. Inside the If block, there is a function that re-displays all controls after the page reloads, including the new setting from my dropdown.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.