3

I have code in one of my pages which is as given below:

<asp:button id="btnTest" runat="server" onclick="btnTest_click" />
<div class="TomasForm">
        <ul>
            <li>
                <asp:Label ID="LineL" runat="server" AssociatedControlID="Lines" Text="<%$ FieldLabelResources:Units, Line, false %>" />
                <asp:DropDownList ID="Lines" runat="server" AutoPostBack="true" AppendDataBoundItems="true"
                    DataTextField="Name" DataValueField="Id" OnSelectedIndexChanged="Lines_SelectedIndexChanged">
                    <asp:ListItem Text="<%$ Resources:Common, SelectOne %>" Value="-1" />
                </asp:DropDownList >
            </li>
        </ul>
    </div>

My issue is when I change the Lines dropdownlist value, the page(page_load and Lines_SelectedIndexChanged events) is getting called twice. The page is behaving normal when I click btnTest button.

Could anyone let me know why this is happening??

Thanks, Mahesh

3
  • Could you please provide the code .. Commented May 19, 2011 at 10:42
  • You might want to paste the code from your SelectedIndexChanged event handler. Commented May 19, 2011 at 10:51
  • If you set the selected value in your page load eventhandler(or other life cycle event) then you will cause the changed event to fire again. Commented May 19, 2011 at 13:08

2 Answers 2

1

You need to change the AutoPostBack to False in your DropDownList.

Sign up to request clarification or add additional context in comments.

1 Comment

but if the OP wants the page to postback when the dropdownlist is changed (which is fine), it shouldn't be firing the page events twice...
0

You should set the AutoPostback to false for dropdown list and in code,make sure you write the condition like this in Page_Load if (!isPostBack()) { // Write your code, it will work only once }

2 Comments

But, I want to postback when the dropdown value is changed.
This is the postback code I am using: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Lines.DataSource = TomasContext.Current.GetTubeLines(); Lines.DataBind(); } }

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.