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