ASP.NET C# Web application - CheckBoxes - c#

I have two checkboxes, what I want to do is, when I check one, the other should be disabled, I always do this in C# Windows application and it is my first try with ASP.NET is there a way to do that without using combocheckboxes?
here is my method which does not work:
protected void checkplan0_CheckedChanged(object sender, EventArgs e)
{
if (checkplan0.Checked == true)
{
checkplan1.Enabled = false;
}
if (checkplan0.Checked == false)
{
checkplan1.Enabled = true;
}
}

Like others have said you'll need to have autopostback="true", also it might be worth considering using radio buttons, as only one radio button in a group can be checked at a time.
<asp:RadioButton id="radioplan0" Checked="True" GroupName="RadioPlan" runat="server" Autopostback="true" />
<asp:RadioButton id="radioplan1" Checked="False" GroupName="RadioPlan" runat="server" Autopostback="true" />
Then nothing needs to be added to the code behind to toggle the other options off.

Your code seems to be correct, as you mentioned that you are from windows form background I assume this is what you are missing
<asp:CheckBox
ID="checkplan0"
runat="server"
AutoPostBack="true"
OnCheckedChanged="checkplan0_CheckedChanged" />
Set AutoPostBack = "true" setting this true will mean on check of checkbox a postback will be sent to server and the code that you have written on Check Change will execute.

Your code seems good. But as you are beginner. You might be missing simple thing in source page.
=> Check whether Your AutoPostBack set to true for your checkbox. If not add AutoPostBack ="true"

Related

If Else Statement not working ASP.NET

Index.aspx
<asp:RadioButton ID="rbt1" runat="server" Text="By Customer" GroupName="summary" OnCheckedChanged="enabled_CheckedChanged" Checked="True"/>
<asp:RadioButton ID="rbt4" runat="server" Text="With Target" OnCheckedChanged="enabled_CheckedChanged" GroupName="summary"/>
Index.aspx.cs
public void enabled_CheckedChanged(object sender, EventArgs e)
{
if (rbt4.Checked == true)
{
rbt1.Checked = true;
}
else
{
rbt1.Checked = false;
}
}
The purpose of the code is when rbt1 is checked, rbt4 will be checked. User can also check rbt4 after checking rbt1. However, if rbt4 is checked, rbt1 will be checked automatically. The code above was used but it doesn't seem to be working. Did I miss out something or is there error in my coding? Please advice. Thanks in advance.
Both radio buttons have the same GroupName. Only one of them can be checked at a time. Change GroupName property of radio buttons to fix the error. refer this link for clarity. Also add `AutoPostBack="true" property to radio button code.
You are using radio button and want to select multiple but radio button are mutually exclusive with same GroupName. You can not select more then one RadioButton with same group. You probably need to use the CheckBox for selecting multiple, or Giving different GroupName to exceptional cases may sort out the problem or you may need combination of checkboxes with radiobuttons or nested html-checkboxes.
Use the GroupName property to specify a grouping of radio buttons to
create a mutually exclusive set of controls. You can use the GroupName
property when only one selection is possible from a list of available
options. When this property is set, only one RadioButton in the
specified group can be selected at a time, MSDN.
If you want to make some relation in checkbox like if one is selected then some other should be selected and nothing more then that then you may use client side script to save unnecessary PostBacks and smooth user experience.
Refer this coding Note: AutoPostBack="true". Remove the Same group name for Both Radio Button.
<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged" AutoPostBack="true" />
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
RadioButton2.Checked = true;
}
Please add : AutoPostBack="true" in your html code
<asp:RadioButton ID="rbt1" runat="server" AutoPostBack="true" Text="By Customer"GroupName="summary" OnCheckedChanged="enabled_CheckedChanged" Checked="True" />
<asp:RadioButton ID="rbt4" runat="server" AutoPostBack="true" Text="With Target" OnCheckedChanged="enabled_CheckedChanged" GroupName="summary" />

Page_load() is firing on checkbox selection

Whenever i am trying to select the checkbox of treeview, the page is reloaded and the checkbox is again deselected.
How can i get rid from this problem.
Thanks in advance!
.cs page
protected void RadTreeView1_NodeCheck(object o, EventArgs e)
{
}
.aspx page
You might need to set AutoPostBack to false
<asp:CheckBox ID="CheckBox1"
runat="server"
AutoPostBack="false"
Text="Checkbox control" />
The easiest suggestion is just set autopostback= false but I assume you need that as you want to do something when selecting checkbox but don't want to have whole reload.
I think it might happen because you forgot to add
if(!Page.IsPostBack)
{
//your code
}
inside your page_load function.
set AutoPostBack="false" in asp:CheckBox tag.

Checkbox not working in asp.net and HTML mixed page

I'm grappling with an issue where the site is asp.net/C# but controls on the .aspx pages are HTML and I'm not sure how well it would go over if I would change everything into asp.net controls. Also the change is minor. I was tasked to add a check box, as in <input type="checkbox" name="disableFeatureManager" runat="server" id="disableFeatureManager" />Disable Feature Manager and in the .cs page I want to check if the box is checked and make decisions based on that, but the control's checked property is always false. The submit button is also a HTML control: <input type="submit" value="Start" name="submitButton" />
In the Page_Load ckecking if check like this returns false.
if (disableFeatureManager != null && disableFeatureManager.Checked)
nextURL.Append(FeatureManagerChoices.CreateQueryStringFromFormData(Request.Form));
You could keep your checkbox as an Html server control by doing the following:
<input type="checkbox" name="disableFeatureManager" runat="server" id="disableFeatureManager" />
Then you could change your button to a web control as follows:
<asp:Button ID="submitStart" runat="server" OnClick="btn1_Click" Text="Start" ClientIDMode="Static" />
The only difference with the above rendered HTML will be the name that is output but you will have an Id that is submitStart due to the ClientIdMode being static, if you need a friendly consistent Id for javascript manipulation.
To wire in the event add this code which will read the value from the checkbox:
protected void btn1_Click(object sender, EventArgs e)
{
var isChecked = disableFeatureManager.Checked;
}
As I mentioned before, I was trown into the deep end of MVC. Co-worker offered a simple answer(as least in this case):
bool isChecked = false;
if(Request["disableFeatureManager"] != null)
isChecked = (Request["disableFeatureManager"].ToLower() == "on");
if (!isChecked)
nextURL.Append(FeatureManagerChoices.CreateQueryStringFromFormData(Request.Form));
I think this can be further simplified in to one IF statement.

Custom Validator not firing

I realize there are lots of similar posts, however I have not found one that has worked for me unfortunately. Basically, I have an asp:customvalidator that I am trying to add to a validationgroup with other validators so that all error messages appear in the same alert. Here is the customvalidator
<asp:TextBox runat="server" ID="txtVideo1Url" Columns="20" Width="98%" />
<asp:CustomValidator runat="server" ID="valURL1" ControlToValidate="txtVideo1Url" OnServerValidate="txtVideo1Url_ServerValidate" Display="None" ValidationGroup="submission" />
and here is the event
protected void txtVideo1Url_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = false;
valURL1.Text = "FAIL!";
}
The event isn't firing at all and I have no idea why. Once I can get the event firing I can put some actual logic into it, lol
UPDATE: I've noticed that I am now able to get the event firing, however the validationsummary is set to display all errors in a messagebox and this error isn't getting added to the messagebox.
Remember to set this property on the CustomValidator...
ValidateEmptyText="True"
You need to set the CausesValidation property of the TextBox to true, like this:
<asp:TextBox runat="server" ID="txtVideo1Url" Columns="20" Width="98%" CausesValidation="true" />
You will have to add ValidationGroup="submission" to the ASP.NET control that will fire the postback.
CustomValidators don't fire if other Validators in your ASPx are not validating. You may need to force a Page.Validate("something"), with your specific validation group. I suggest look at OnTextChanged event to force a page validate.

FreeTextBox javascript error is preventing PostBack on my DropDownList

I have one page with 3 FreeTextBox controls on it. They are set up correctly, and I was using them normally until I needed to add a DropDownList control that would PostBack to the server, but I was surprised to see that the OnSelectedIndexChanged event would never trigger. If I was to do a Post with a button, or some other server-side control, then would the event be triggered. After much debugging I found the following Javascript error was being thrown every time I selected something different on my DropDownList control:
TypeError: FTB_API.MainContent_MainContent_FreeTextBox1 is undefined
The error seems pretty straight forward;
Firebug tells me this error comes from the following function:
function WebForm_OnSubmit()
{
FTB_API['MainContent_MainContent_FreeTextBox1'].StoreHtml();FTB_API['MainContent_MainContent_FreeTextBox2'].StoreHtml();FTB_API['MainContent_MainContent_FreeTextBox3'].StoreHtml();
return true;
}
I've tried several things without success. When I remove the FreeTextBox controls from my page, I have successful PostBacks. Any help would be appreciated.
Thanks.
EDIT 1: This is some of my markup
3 FreeTextBox set up like this:
<FTB:FreeTextBox ID="FreeTextBox3" JavaScriptLocation="ExternalFile" ButtonImagesLocation="ExternalFile" ToolbarImagesLocation="ExternalFile" runat="server" EnableHtmlMode="true" />
My DropDownList:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
Set the property AutoPostBack = true of your dropdown in markup page. This will make the post back when you change the dropdown element and OnSelectedIndexChanged get triggered.
Example :
<asp:DropDownList id="drpList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="event name" />
I have found the answer to the question in this thread: Hidden FreeTextBox bug on Firefox It seems for some reason that when the control is not visible or is hidden (I have tabs) it behaves this way. The answer is kind of a Hack, but it works. Thanks for the answers.

Categories