If Else Statement not working ASP.NET - c#

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" />

Related

ASP.NET C# Web application - CheckBoxes

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"

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.

ASP:Dropdownlist OnSelectedIndexChanged Event Not Firing

I have a non-databound Dropdownlist inside of an UpdatePanel:
<asp:UpdatePanel runat="server" ID="FiltersUpdPnl">
<ContentTemplate>
<div class="filters">
<asp:Button runat="server" ID="ExportBtn" Text="Export Map to Image" />
<br />
Show:
<asp:DropDownList runat="server" ID="CapNumProjectsDDL" AutoPostBack="true" OnSelectedIndexChanged="ApplyFilters" OnPreRender="CapNumProjectsDDL_PreRender">
<%--<asp:ListItem Value="0" Text="" Selected="True"></asp:ListItem>--%>
<asp:ListItem Value="1" Text="Capacity"></asp:ListItem>
<asp:ListItem Value="2" Text="Number of Projects"></asp:ListItem>
</asp:DropDownList>
</div>
</ContentTemplate>
</asp:UpdatePanel>
The event handler isn't terribly important, it just does stuff:
protected void ApplyFilters(object sender, EventArgs e)
{
//Do Stuff relating to the selected Value
}
When the page loads, "Capacity" is selected by default, as it's the first ListItem. When I switch off to "Number of Projects", the event handler fires as expected, executing the code. But when I switch back to "Capacity", the handler does NOT fire. The Postback is occurring, but I want it to specifically hit the event handler on both listitems.
You can see that I have a commented-out "0-value" ListItem in there. When I uncomment that, both "Capacity" and "Number of Projects" will hit the event handler as expected.
The issue is that when the page loads, it's loading the data relevant to the "Capacity" dropdown, so I want the "Capacity" ListItem to be showing, but be able to fire the event handler when selected.
Am I missing something obvious here?
E: I tried adding a handler for the DDL_Prerender event, setting the SelectedIndex to like 200 or something clearly not in the list, hoping it would de-select "Capacity", but that didn't work either.
This is the Pre-render code:
protected void CapNumProjectsDDL_PreRender(object sender, EventArgs e)
{
CapNumProjectsDDL.SelectedIndex = 200;
}
This didn't change the way it worked.
It seems that you want that drop down element raise change even if you select in drop down box already selected item. I believe that this will not work, and the cause of the problem is not in server side asp.net dropdownlist control, but in how HTML select element is working to which DropDownList control is rendered.
The problem is that HTML select element doesn't fire change event if user select the same item as was before drop box was shown (because from the point of view of control state - it was not changed).
So, I believe that the behavior that you want can be implement, but you should not use HTML select control and instead of it implement custom solution.

AJAX Update Panel - Action button click on drop down selection

Evening all
I have the following scenarion. I have a range of drop down menus where a clietn can select. The code below is wrapped in an update panel but without this, the button click fires a method to retrieve the number of products. So for example, an item is selected from ddlCategory, the btnValidate is clicked and the label returns the number of products within that category.
I have the following code for an update panel - I'm just not sure how to implement if effectively.
<asp:UpdatePanel ID="UpdatePanel1" runat="Server">
<ContentTemplate>
<asp:Label ID="lblSearchResultsStatus" runat="server" Text="Number of results found: "></asp:Label>
<asp:Label ID="lblSearchResults1" runat="server" Text=""></asp:Label>
<br />
<br />
<asp:Button ID="btnValidate" runat="server" Text="Validate Search"
OnClick="btnValidate_Click" Width="120px" />
</ContentTemplate>
</asp:UpdatePanel>
How do I go about wiring the update panel so that when a drop down list item is selected, the buttong is effectively clicked?
Do I have to implement something on each of the ddlSelectedIndexChanged event or is there a property within the update panel that does?
Apologies for the noob question.
The point of the UpdatePanel is to update a portion of the page with an AsyncPostBack instead of reloading the whole page, but in order for the drop-down lists to trigger an AsyncPostBack automatically, they must be on an UpdatePanel. In order to update the labels, they must be on the same UpdatePanel with the labels.
A common pattern to implement what you want to accomplish:
Put the DDLs on the UpdatePanel, and set AutoPostBack="true" on each DDL, so they trigger AsyncPostBacks.
Add an event handler for SelectedIndexChanged on each DDL (can be the same event handler).
Move whatever you do in btnValidate_Click to another method.
Call the new method from btnValidate_Click and the SelectedIndexChanged event handler(s) so they all perform the same function.
You can call your btnValidate_Click event from codebehind at any point, i.e. Page_Load
protected void Page_Load(object sender, EventArgs e)
{
btnValidate_Click(btnValidate, new EventArgs());
}

About RadioButton CheckedChanged event

Hi there im using a single TextBox for querying differents types of chossings, for example when i select the first RadioButton (Last Name) I search by Client last name, when i choose second RadioButton (Doc. Code) I search by code and so on, please how can i manage or handle exceptions when for example if user choose "Search by Date" and send a string type?
Im using C# 3.5 - Asp.net
I would like to make it with regular expressions and add that in the RadioButton event, so when users changes radio, he could type just some characters in optionA, other more in Option B, and just dates in OptionC ... (regular expression)
Thanks in advances
if you are using the asp web control radiobuttonlist then you can make lots of changes when their is a postback. you can set the attribute to SelectIndexChanged,so whenever their is a change it cause a postback and then you can do whatever from their(verifications).
ex:
<asp:radioButtonList
id="radio1" runat="server"
autoPostBack="true"
cellSpacing="20"
repeatColumns="3"
repeatDirection="horizontal"
RepeatLayout="table"
textAlign="right"
OnSelectedIndexChanged="radio_SelectedIndexChanged">
<asp:ListItem text="10pt" value="itsMe"/>
<asp:ListItem text="14pt" value="itsYou"/>
<asp:ListItem text="16pt" value="Neither"/>
</asp:radioButtonList>
on the server you should have
protected void radio_SelectedIndexChanged(object sender, EventArgs e)
{
//do whatever you want by calling the name of the radio id
//example
if(radio1.SelectedItem.Value=="(whatever you want to test)"
}

Categories