0

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金额。在这个更新面板之外,我有一个额外的服务器控制表单字段,例如文本框和一个提交按钮。

我遇到的错误是选择“其他”时,按钮“onclick”事件无法启动完整的回发。

例子:

<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" />

编辑:发布代码隐藏可能会让每个人都更容易

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"));
        }        
    }
}
4

4 回答 4

1

这可能是一个验证者阻止你吗?您的按钮是否应该忽略任何验证器?如果是这样,您应该在按钮上设置 CausesValidation="false",以便即使面板中的验证器无效,它也会触发。

否则,按钮的单击事件将被面板中的验证器停止。

于 2009-02-25T02:32:13.003 回答
0

AJAX 会在回发、会话等方面引起一些奇怪的行为。

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

编辑:也试试这个:确保你的连线不在“if (!isPostBack)”块中。

于 2009-02-25T00:20:00.390 回答
0

将以下代码放在更新面板之外。

<%--dummy validator to make ajax validation possible--%>
        <asp:RequiredFieldValidator runat="server" CssClass="hidden" ControlToValidate="dummyTextBox" ValidationGroup="dummy"></asp:RequiredFieldValidator>
        <asp:TextBox runat="server" ID="dummyTextBox" CssClass="hidden"></asp:TextBox>

        <style>
    .hidden {
      display: none;
    }
    </style>

下面的链接对我有用....

http://jeffreypaarhuis.com/2011/08/08/validation-not-working-in-updatepanel/

于 2013-05-14T04:17:20.680 回答
-3

检查 Button 控件的 AutoPostBack 属性。将其设置为“真”。

于 2009-02-24T23:52:32.310 回答