OnCheckedChanged event not being triggered when checbox unchecked - c#

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.

Related

Bug with asp.net controlid name

Is there any naming convention or restrictions for a button to be named as "submit"? I have a dropdown list with autopostback= true and a button. It should auto generate textbox with the data from the database
<asp:DropDownList ID="d1" runat="server" DataSourceID="SqlDataSource1" DataTextField="scode" DataValueField="scode" OnSelectedIndexChanged="d1_SelectedIndexChanged" AppendDataBoundItems="true" AutoPostBack="true">
<asp:ListItem Selected="True">0</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [scode] FROM [warehouse]"></asp:SqlDataSource>
<asp:TextBox ID="t1" runat="server"></asp:TextBox>
<asp:Button ID="submit" runat="server" Text="Button" />
codebehind:
protected void d1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("hello");
t1.Text = "abcd";
}
my problem is when i set the controlid of my button as "submit" the postbacks and event handling from the dropdownlist are not triggered but pending. The postbacks and event handling are only triggered when i click on the "submit" button. Pretty sure the problem lies within controlid of the button as I took few hours of trial and error to find a workaround. By renaming the controlid of the button as "submit1" and everything worked perfectly. In short, the textbox are autogenerated with my data whenever I select an item when the controlid of the button is named as submit1". Meanwhile if the controlid of the button is "submit", the data are only generated when i click on the button. This is a small issue as i managed to find the workaround but i would like to know why is this happening.
Using the following minimal code I was able to reproduce your issue on a new web application project with no master page.
<asp:DropDownList runat="server" OnSelectedIndexChanged="d1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="ResultLabel" runat="server" />
<asp:Button ID="submit" runat="server" Text="Button" />
Code behind
protected void d1_SelectedIndexChanged(object sender, EventArgs e)
{
ResultLabel.Text = "Postback occurred";
}
If you watch the JavaScript console, you'll see that it results in an error when you change the dropdownlist selection.
Uncaught TypeError: theForm.submit is not a function
This error comes from the bit of code that Web Forms inserts into your page to make postbacks work properly.
That error prevents the postback from completing. The solution is to not have any controls with an ID of submit. You might consider this a bug, but changing the framework to get around this issue would make the framework more complicated than it needs to be.

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.

Databind fires after ItemUpdated event

I'm using an asp.net 4 web site project (C#) in VS2008 and I have a FormView with ItemUpdated event:
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
</EditItemTemplate>
</asp:FormView>
protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
FormView1.DataBind(); // adding this line even doesn't help
TextBox box = FormView1.FindControl("TextBox1") as TextBox;
box.Enabled = false;
}
But I can't figure out, why an extra "FormView1.DataBind()" or Render(?) happens AFTER the ItemUpdated event. The result is that my code in the ItemUpdated event gets like "overwritten" and the TextBox1 doesn't get disabled.
When I set a breakpoint to the last line "box.Enabled = false;" then I see that after the ItemUpdated event it jumps to the aspx page again and steps through the TextBoxes.
Disabling this TextBox1 from another GridView1_SelectedIndexChanged works fine.
Is there any way to see the "current lifecycle progress" in debugging?
EDIT:
To clarify the reasoning...
I have a GridView1 where selecting the item populates the abovementioned FormView1. The point is that I need to disable some of the TextBoxes in the FormView1 based on, for example, user access levels.
Selecting the item from GridView1 disables the TextBox1 just fine, but when I click the Update button on the FormView1 then all TextBoxes are enabled, even if I see in the debugger code running through the GridView1_SelectedIndexChanged() function. And after I re-select the gridview item, the correct TextBoxes are disabled again.
Using even this code:
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" DefaultMode="Edit" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("col2") %>' />
<asp:Button ID="Btn1" runat="server" CommandName="Update" Text="Update" />
</EditItemTemplate>
</asp:FormView>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (UserAccess() == false) {
TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
box2.Enabled = false;
}
}
protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
GridView1_SelectedIndexChanged(sender, e);
}
Maybe I should disable my Textboxes via another event?
This doesn't make sense, please explain more about why you are trying to disable the TextBox, have you just left the ItemTemplate off in your question? or is it actually missing? if it's missing why?
The TextBox is in the FormView's EditItemTemplate, so will only be visible when the FormView is in edit mode. After clicking update or cancel the TextBox will no longer be rendered, and the ItemTemplate is rendered instead. So there should be no need to set the TextBox to be disabled.
EDIT
Ok since you edited your question. You need to use the OnDataBound event of the FormView, which occurs at the end of binding, and disable your TextBoxes at that point.
aspx
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1"
OnDataBound="FormView1_DataBound">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
</EditItemTemplate>
</asp:FormView>
aspx.cs
protected void FormView1_DataBound(object sender, EventARgs e)
{
if (UserAccess() == false) {
TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
box2.Enabled = false;
}
}
Rather then using gridview selected Index change you can use DataBound event on formview, so your logic would fire everytime the formview is rebind.

How do I redirect with a Drop Down List, and not a Button?

I am looking into using only a ddl to run my query, not a ddl and a Button_Click function. I am yet to find what to do. How do I do this?
In your as(p/c)x:
<asp:DropDownList runat="server"
id="ddl"
OnSelectedIndexChanged="SelectionChanged"
AutoPostBack="true">
<asp:ListItem Text="Page 1" Value="/page1.aspx" />
<asp:ListItem Text="Page 2" Value="/page2.aspx" />
</asp:DropDownList>
The "AutoPostBack" property tells ASP.NET to wire up a client-side (javascript) command that submits the form as soon as the drop down list changes, instead of waiting for a button click.
And in your codebehind, the event handler we referenced in the "OnSelectedIndexChanged" property will get fired:
protected void SelectionChanged(object sender, EventArgs e)
{
Response.Redirect(((DropDownList)sender).SelectedValue);
}
Set the AutoPostBack property to true, then hook into the OnSelectedIndexChanged event
<asp:DropDownList
id="dropDownList1"
runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="dropDownList1_SelectedIndexChanged" />
Server Side
void dropDownList1_SelectedIndexChanged
(Object sender, EventArgs e) {
//run your query
}
Ensure your Drop down list has it's "AutoPostback" property set to true. This will cause the page to post back when the user selects an item from within the drop-down list. You can respond to this in your code-behind in whichever event you desire, Page_Load, or the DDL's own OnSelectedIndexChanged

Categories