c# - 选中复选框后如何运行方法

标签 c# asp.net checkbox

我正在 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/

相关文章:

c# - 从代码隐藏创建 aspx 文本框

c# - 使用 C# 对 (My)SQL 结果进行排序的想法

c# - 如何检查多维数组是否具有值及其索引?

javascript - ASP.NET 下拉列表在服务器button_click 中为空,ddl 已在客户端填充

javascript - 如何从 asp.net 代码隐藏函数调用 javascript 函数

javascript - Jquery 触发复选框 : function tied to click event occurs before the checked attribute is set

android - Android 中错误的复选框选择?

c# - ASP.net 应用程序可以为单个请求运行多长时间

c# - 无法连接到 Controller 中的方法

页面加载时选中 Jquery 复选框