我正在 aspx 页面上构建一个表单。我想运行一个方法,如果用户勾选标记为“定期捐赠?”的复选框,该方法将添加更多字段和标签
我的表格
<telerik:LayoutRow CssClass="formContainer">
<Columns>
<telerik:LayoutColumn Span="12" SpanSm="12" SpanMd="12">
<asp:Label id="firstName" runat="server"></asp:Label>
<br />
<asp:TextBox runat="server" ID="UserFirstName"></asp:TextBox>
</telerik:LayoutColumn>
<telerik:LayoutColumn Span="12" SpanSm="12" SpanMd="12">
<asp:Label id="lastName" runat="server"></asp:Label>
<br />
<asp:TextBox runat="server" ID="UserLastName"></asp:TextBox>
</telerik:LayoutColumn>
<telerik:LayoutColumn Span="3" SpanSm="12" SpanMd="12">
<asp:Label id="address1" runat="server"></asp:Label>
<br />
<asp:TextBox runat="server" ID="userAddress1"></asp:TextBox>
</telerik:LayoutColumn>
<telerik:LayoutColumn Span="9" SpanSm="12" SpanMd="12">
<asp:Label id="address2" runat="server"></asp:Label>
<br />
<asp:TextBox runat="server" ID="userAddress2"></asp:TextBox>
</telerik:LayoutColumn>
<telerik:LayoutColumn Span="3" SpanMd="12" SpanSm="12">
<asp:Label ID="city" runat="server"></asp:Label>
<br />
<asp:TextBox runat="server" ID="userCity"></asp:TextBox>
</telerik:LayoutColumn>
<telerik:LayoutColumn Span="9" SpanMd="12" SpanSm="12">
<asp:Label ID="zip" runat="server"></asp:Label>
<br />
<asp:TextBox ID="userZip" runat="server"></asp:TextBox>
</telerik:LayoutColumn>
<telerik:LayoutColumn>
<asp:Label ID="returningDonor" runat="server"></asp:Label>
<br />
<asp:CheckBox ID="userReturningDonor" runat="server" />
</telerik:LayoutColumn>
</Columns>
</telerik:LayoutRow>
我的代码在后面
public partial class donationForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
firstName.Text = "First Name";
lastName.Text = "Last Name";
address1.Text = "Address 1";
address2.Text = "Address 2";
city.Text = "City";
zip.Text = "Zip Code";
returningDonor.Text = "Recurring Donation?";
userReturningDonor.Checked = showRecuring();
}
static void showRecuring()
{
/*RUN CODE*/
}
}
我得到的错误是
Cannot implicity convert type 'void' to 'bool'
最佳答案
根据您想要实现的目标,这里有一些可以尝试的事情:
单击复选框时导致回发
如果您确实希望它在检查时返回并运行代码,我会这样做:
像这样更新您的复选框:
<asp:CheckBox ID="userReturningDonor" runat="server" OnCheckedChanged="userReturningDonor_CheckedChanged" AutoPostBack="true" />
将其添加到后面的代码中:
protected void userReturningDonor_CheckedChanged(object sender, EventArgs e)
{
if (userReturningDonor.Checked) {
MsgBox("Checked");
} else {
MsgBox("Not checked");
}
}
只需消除错误
如果您只是想消除错误,但代码仍然按预期运行,那么您可以这样做:
将static void showRecuring()
更改为static bool showRecuring()
donationForm
期望 showRecuring
在这一行中返回一个 boolean
:
userReturningDonor.Checked = showRecuring();
但是,showRecuring
是一个void
。
这将消除您的错误,但如果您希望 showRecuring
根据是否 userReturningDonor.Checked
运行代码,那么您可以执行以下操作:
只需在 Page_Load
事件中运行函数
将 userReturningDonor.Checked = showRecuring();
替换为 showRecuring(userReturningDonor.Checked);
并像这样定义showRecuring
:
static void showRecuring(bool returningDonorChecked){
if(returningDonorChecked) {
//yes
} else {
//no
}
}
关于c# - 选中复选框后如何运行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31862030/