Page_load() is firing on checkbox selection - c#

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.

Related

OnCheckedChanged event not being triggered when checbox unchecked

I am trying to implement a checkbox logic. When the checkbox is checked the textbox will be enabled and vice versa.
When the checkbox checked, the event is triggered. However when the checkbox is unchecked, the event isn't triggered.
Here are the codes for checkbox;
<asp:CheckBox ID="CheckBox" runat="server" Checked="false" OnCheckedChanged="CheckedChanged" CssClass="LabelText" EnableViewState="False" AutoPostBack="True" />
Here is the event block;
protected void CheckedChanged(object sender, EventArgs e)
{
txtIP.Enabled = CheckBox.Checked;
}
In the debug mode I can see that , in the case of checked, method of CheckedChanged is being calling and textbox enabled but in the case of unchecked nothing happens, just loading the page.
I couldn't see my mistakes, I hope you'll help me guys.
Thnx.
Try removing the Checked="false" and set EnableViewState="True":
<asp:CheckBox ID="CheckBox" runat="server" OnCheckedChanged="CheckedChanged"
CssClass="LabelText" EnableViewState="True" AutoPostBack="True" />
I've just tested this and it works correctly (without a master page). Bear in mind that you could also have EnableViewState set at the master page level.

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"

Calling a function in Aspx.cs with change in dropdownbox

I have a page where in a change in the value from the drop down box will pass the corresponding text in the drop down box to get the values from the database.
<asp:DropDownList ID="dropid" runat="server" OnChange="Getvaluesfromaspx"></asp:DropDownList>
I want the function named "Getvaluesfromaspx" in the aspx.cs to be called from the aspx file.
Please help.
Use "OnSelectedIndexChanged" event instead of "OnChange" event.
Also set AutoPostBack property value to true.
<asp:DropDownList ID="dropid" AutoPostBack="true" runat="server" OnSelectedIndexChanged="Getvaluesfromaspx"></asp:DropDownList>
And in code behind
protected void Getvaluesfromaspx(object sender, EventArgs e)
{
//Do whatever want to do here.
}

how to call DropDownList's onselectedindexchanged event from CreateUserWizard->WizardStep

I am using CreateUserWizard and added additional WizardStep for user additional details.
I am using one dropdownlist. value has been filled from DB in dropdownlist.
Now, when I will change the value in dropdownlist. I want to chekc/uncheck the one checkbox based on it's value in DB for selected dropdownlist value.
For that i want to call onselectedindexchanged event of dropdownlist.
How to fire onselectedindexchanged event?
Please help me to solve this problem?
Dont add onselectedindexchanged manually
All event for element are listed in event tab on right hand side
Your code should look like
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
In cs file
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Important Note
Remove the ViewStateMode="Disabled" in the CreateUserWizard control
Try this.
drag a dropdownlist control on your aspx page and add selectedindexchanged event, test it properly, now cut the control and paste it inside WizardStep.
I hope it works.

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.

Categories