views:

3524

answers:

3

I have a donation form that contains an update panel that contains a dropdown for predetermined amounts plus the "Other" option". When "Other" is selected the dropdown has triggered the partial postback and renders the update panel with the additional "Other" textbox for the amount. Outside this update panel I have a additional server control form fields such as textboxes and a button for submission.

The bug I run into is when "Other" is selected the button "onclick" event fails to fire a full postback.

Example:

<asp:UpdatePanel ID="updatePanelAmount" runat="server">
    <ContentTemplate>
        <table style="width: 500px;">
        <tbody>
        <tr>
            <th style="width: 200px;"><asp:Label ID="lblAmount" runat="server" CssClass="required" Text="Donation Amount: " /></th>
            <td>
                <asp:DropDownList ID="selAmount" runat="server" />
                <asp:CustomValidator ID="valDonationAmount" runat="server" ControlToValidate="selAmount" ErrorMessage="Donation Amount" Display="None" />
            </td>
        </tr>
        </tbody>
        </table>        
        <asp:Panel ID="panelOther" runat="server" Visible="false">
            <table style="width: 500px;">
            <tbody>
            <tr>
                <th style="width: 200px;"><asp:Label ID="lblOther" runat="server" Text="Other Amount: " /></th>
                <td>
                    $<asp:TextBox ID="txtOther" runat="server" />
                    <asp:RequiredFieldValidator ID="valOther" runat="server" ControlToValidate="txtOther" Display="None" ErrorMessage="Other Amount" Enabled="false" />
                    <asp:RegularExpressionValidator ID="valOtherExpress" runat="server" ControlToValidate="txtOther" Display="None" ErrorMessage="Other Amount: Invalid" ValidationExpression="[1-9][0-9]+(\.[0-9]{2})?" Enabled="false" />
                </td>
            </tr>
            </tbody>
            </table>
        </asp:Panel>                
    </ContentTemplate>           
</asp:UpdatePanel>    
<ctl:CreditCardForm ID="ctlCreditCardForm" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Donate" />

EDIT: Posting the code-behind might make is easier for everyone

public partial class _Default : System.Web.UI.Page
{
    private ArrayList _donations;

    protected void Page_Init(object sender, EventArgs e)
    {
        valDonationAmount.ServerValidate += new ServerValidateEventHandler(valDonationAmount_ServerValidate);        
        selAmount.AutoPostBack = true;
        selAmount.SelectedIndexChanged += new EventHandler(selAmount_SelectedIndexChanged);
        updatePanelAmount.UpdateMode = UpdatePanelUpdateMode.Conditional;
        updatePanelAmount.ChildrenAsTriggers = true;
        btnSubmit.Click += new EventHandler(btnSubmit_Click);
    }

    void selAmount_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (selAmount.SelectedItem.Text == "Other")
        {
            panelOther.Visible = true;
            valOther.Enabled = true;
            valOtherExpress.Enabled = true;
        }
        else
        {
            panelOther.Visible = false;
            valOther.Enabled = false;
            valOtherExpress.Enabled = false;
        }
    }

    void valDonationAmount_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;
        if (args.Value == "0")
        {
            args.IsValid = false;
        }
    }

    void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();                 
        if (Page.IsValid)
        {

        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {    
        _donations = new ArrayList();
        double[] donations = new double[] { 20.00, 50.00, 100.00, 250.00, 500.00 };
        _donations.AddRange(donations);

        if (!IsPostBack)
        {
            foreach (Double d in _donations)
            {
                selAmount.Items.Add(new ListItem(String.Format("{0:c}", d), String.Format("{0:c}", d)));
            }
            selAmount.Items.Insert(0, new ListItem("Select Donation Amount","0"));
            selAmount.Items.Add(new ListItem("Other", "Other"));
        }        
    }
}
A: 

Check the AutoPostBack property of the Button control. Set it to "True".

HardCode
btnSubmit doesn't have a AutoPostBack property. At least not that I know of.
edames
Buttons are AutoPostback by default.
tsilb
A: 

AJAX causes some funky behavior with postbacks, session, etc.

<asp:Button ID="btnSubmit" runat="server" Text="Donate" onClick="btnSubmit_Click" />

Edit: Also try this: Make sure your wire-up isn't in an "if (!isPostBack)" block.

tsilb
Gave it try. Sorry no go.
edames
+1  A: 

Is this possibly a validator blocking you? Is your button supposed to ignore any validators? If so, you should set CausesValidation="false" on the button so that it will fire even if the validators in the panel are invalid.

Otherwise, the button's click event will be stopped by the validators in the panel.

Jay S
Yes! This would explain why I could do the following: $('#<%=btnSubmit.ClientID %>').click(function() { __doPostBack('<%=btnSubmit.UniqueID %>', ''); });It must of been a client-side script keeping the postback from happening.
edames
Thank you so much. I wanted to know why instead of using the workaround. I should of traced through the generated JS more. Might have found the answer.
edames