Reputation: 7263
I am building a form on an aspx page. I want to run a method that will add more fields and labels if the user ticks a checkbox labeled 'recurring donation?'
My Form
<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>
And my code behind
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*/
}
}
The error im getting is
Cannot implicity convert type 'void' to 'bool'
Upvotes: 2
Views: 1465
Reputation: 19486
There's a few things to try here depending on what you want to accomplish:
Cause a post back when checkbox is clicked
If you actually want it to post back and run code right when it's checked, I would do this:
Update your checkbox like so:
<asp:CheckBox ID="userReturningDonor" runat="server" OnCheckedChanged="userReturningDonor_CheckedChanged" AutoPostBack="true" />
Add this to the code behind:
protected void userReturningDonor_CheckedChanged(object sender, EventArgs e)
{
if (userReturningDonor.Checked) {
MsgBox("Checked");
} else {
MsgBox("Not checked");
}
}
Just get rid of the error
If you just want to get rid of the error but your code still runs as expected, then you could do this:
Change static void showRecuring()
to static bool showRecuring()
donationForm
is expecting showRecuring
to return a boolean
in this line:
userReturningDonor.Checked = showRecuring();
However, showRecuring
is a void
.
This will get rid of your error, but if you want showRecuring
to run code based on whether or not userReturningDonor.Checked
then you could do something like this:
Just run function in the Page_Load
event
Replace userReturningDonor.Checked = showRecuring();
with showRecuring(userReturningDonor.Checked);
and define showRecuring
like so:
static void showRecuring(bool returningDonorChecked){
if(returningDonorChecked) {
//yes
} else {
//no
}
}
Upvotes: 2
Reputation: 2016
Your static method showRecuring is not returning any values (like a bool) but you are using it in the call as it should.
userReturningDonor.Checked = showRecuring();
static void showRecuring()
should be
static bool showRecuring()
Upvotes: 1
Reputation: 1560
Checked
is a bool that gets or set the status of the checkbox (checked or unchecked). if you want to do something when the box is checked, you want to handle the checkbox's CheckedChanged
event. so you want something like
userReturningDonor.CheckedChanged += showRecuring(usinderReturningDonor, new EventArgs());
and
static void showRecuring(object sender, EventArgs e)
{
/*RUN CODE*/
}
EDIT: Just to be clear, the Checked
property of the checkbox is NOT how you handle when it is checked. It is how you tell if it is currently checked or how you set it to be checked or unchecked.
EDIT 2: As other have pointed out, you are getting an error because the Checked
property expects a true or false, where your method returns void. However, Changing your method to return a bool wont help you in your desire to run some code when a checkbox becomes checked
Upvotes: 1
Reputation: 25694
You are setting the return value of showRecurring()
to the checked
property of userReturningDonor
, which expects a bool.
Change the return type of showRecurring()
to a bool.
static bool showRecuring()
{
/*RUN CODE*/
}
Upvotes: 1