【问题标题】:how to run method after check box is checked选中复选框后如何运行方法
【发布时间】:2015-08-07 04:47:08
【问题描述】:

我正在一个 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*/
    }
}

我得到的错误是

不能将类型“void”隐式转换为“bool”

【问题讨论】:

  • Checked 是布尔属性,showRecuring() 必须返回布尔值
  • 除我之外的所有答案似乎都忽略了当复选框被选中时您想要运行一些代码的事实。您遇到问题的原因似乎是 Checked 属性和 CheckedChanged 事件之间的混淆。
  • @onTheInternet 您希望页面在选中或取消选中复选框时立即回发并运行代码吗?

标签: c# asp.net checkbox


【解决方案1】:

根据您想要完成的任务,可以在这里尝试一些事情:

点击复选框时回帖

如果您真的希望它在检查后立即回发并运行代码,我会这样做:

像这样更新您的复选框:

<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();

但是,showRecuringvoid

这将消除您的错误,但如果您希望showRecuring 根据是否userReturningDonor.Checked 运行代码,那么您可以执行以下操作:

只需在Page_Load 事件中运行函数

userReturningDonor.Checked = showRecuring(); 替换为showRecuring(userReturningDonor.Checked);

并像这样定义showRecuring

static void showRecuring(bool returningDonorChecked){
    if(returningDonorChecked) {
        //yes
    } else {
        //no
    }
}

【讨论】:

  • @psoshmo 感谢您指出发帖者的意图。我们大多数人最初只是注意到导致错误的原因。
【解决方案2】:

您将showRecurring() 的返回值设置为userReturningDonorchecked 属性,它需要一个布尔值。

showRecurring()的返回类型改为bool。

static bool showRecuring()
{
    /*RUN CODE*/
}

【讨论】:

  • 这个答案没有解决当复选框被选中并执行一些代码时OP想要处理的事实。他似乎对 check 属性的作用有误解。将他的方法更改为返回 bool 不会帮助他实现实际目标,只会让错误消失。
【解决方案3】:
Checked

是一个布尔值,用于获取或设置复选框的状态(选中或未选中)。如果您想在选中该框时执行某些操作,则需要处理该复选框的CheckedChanged 事件。所以你想要类似的东西

 userReturningDonor.CheckedChanged += showRecuring(usinderReturningDonor, new EventArgs());

 static void showRecuring(object sender, EventArgs e)
{
    /*RUN CODE*/
}

编辑:为了清楚起见,复选框的Checked 属性不是您在选中它时的处理方式。这是您如何判断它当前是否被选中或如何将其设置为选中或取消选中的方式。

编辑 2:正如其他人指出的那样,您收到一个错误,因为 Checked 属性需要一个 true 或 false,您的方法返回 void。但是,将方法更改为返回 bool 不会帮助您在选中复选框时运行某些代码

【讨论】:

    【解决方案4】:

    您的静态方法 showRecuring 未返回任何值(如布尔值),但您在调用中按应有的方式使用它。

    userReturningDonor.Checked = showRecuring();
    
    static void showRecuring()
    

    应该是

    static bool showRecuring()
    

    【讨论】:

    • 对不起,我错误地编辑了你的帖子而不是我的。
    • @TonyL。没问题。修好了。
    猜你喜欢
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多