Gonzalo
Gonzalo

Reputation: 992

Keep value after postback

I have an ASP.NET web site with something similar to the following code:

.aspx

<asp:UpdatePanel ID="UpdatePanel" runat="server">
    <ContentTemplate>
        <table id="RatesTable">
            <tr>
                <td>Hotel</td>
                <td>&nbsp;</td>
            </tr>
            <tr id="NewRateRow">
                <td>
                    <asp:DropDownList ID="TxtHotel" runat="server" OnSelectedIndexChanged="TxtHotel_SelectedIndexChanged" AutoPostBack="true">
                    </asp:DropDownList>
                </td>
                <td>
                    <a href="javascript:AddHotelPackRate()">Add</a>
                </td>
            </tr>
        </table>
    </ContentTemplate>
</asp:UpdatePanel>

AddHotelPackRate JS function, reads the value in the dropdown and "draws" a new row at the beginning of the table, containing this value. The problem is that after this, when the SelectIndexChanged function is executed again, the row that was added before, now disappears.

Is there a way to avoid losing this type of information after postback?

Upvotes: 2

Views: 3074

Answers (2)

Servy
Servy

Reputation: 203802

You'll either have to have the content added by JS inside of the updatepanel, or you'll need to add whatever data the JS code added in the postback so that it continues to exist when the data is re-drawn.

The easy way is probably just to have AddHotelPackRate cause an updatepanel update that adds the row via server side code along with any other database/etc. changes so that it will continue to persist.

Edit: another comment also brings up the fact that you could just not update the update panel when the selected item changes, thus avoiding the issue entirely (in a different way).

Upvotes: 3

Wahtever
Wahtever

Reputation: 3678

The problem is you didn't add a triggr to your update panel. Change your code to this:

<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
    <table id="RatesTable">
        <tr>
            <td>Hotel</td>
            <td>&nbsp;</td>
        </tr>
        <tr id="NewRateRow">
            <td>
                <asp:DropDownList ID="TxtHotel" runat="server" OnSelectedIndexChanged="TxtHotel_SelectedIndexChanged" AutoPostBack="true">
                </asp:DropDownList>
            </td>
            <td>
                <a href="javascript:AddHotelPackRate()">Add</a>
            </td>
        </tr>
    </table>
</ContentTemplate>
<Triggers>
   <asp:AsyncPostBackTrigger ControlID="TxtHotel" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>

Upvotes: -1

Related Questions