How to trigger an event handler before the page is reloaded? - c#

I have a control and Event Handler:
<asp:DropDownList ID="SortDirection1" runat="server"
OnSelectedIndexChanged="SortDirection_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Selected="True" Value="0"> ASC </asp:ListItem>
<asp:ListItem Value="1"> DESC </asp:ListItem>
</asp:DropDownList>
and
protected void SortDirection_SelectedIndexChanged(object sender, EventArgs e)
{
var DDL = (sender as DropDownList);
Session["SortDirection"] = DDL.SelectedIndex;
}
If I change selection in DropDownList, page will be reloaded and only after it this handler will be triggered. So, there is a problem. I try to use Session["SortDirection"] before it will be rewrited. The question is How to rewrite Session["SortDirection"] before page reloading?

You are using an autopostback on the DDL and then setting a session value in the Selected Index Changed event. The correct place to use that value is in the Load Complete event. That event gets fired after the Selected Index Change event.
It is very beneficial to learn the complete ASP.NET page lifecycle. There is a nice diagram in the "Additional Page Life Cycle Considerations" section that shows the events and methods for the page and its controls. The Selected Index Change event happens in Event Handling section.
The Load Complete happens after that. This gets raised when all of the process has been completed and just before the rendering starts. The purpose of the Load Complete is to give you a place for logic once all of the setup and processing is finished.

Related

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.Net DropDownList OnSelectedIndexChange not firing

I have the following DropDownList control:
<asp:label runat="server" text="Filter by state: "></asp:label>
<asp:dropdownlist runat="server" id="filterbystate"
OnSelectedIndexChanged="FilterByState">
<asp:ListItem value="all" selected="True">All</asp:ListItem>
<asp:ListItem value="ca" selected="False">California</asp:ListItem>
<asp:ListItem value="co" selected="False">Colorado</asp:ListItem>
<asp:ListItem value="id" selected="False">Idaho</asp:ListItem>
<asp:ListItem value="ut" selected="False">Utah</asp:ListItem>
</asp:dropdownlist>
Here is the method:
protected void FilterByState(object sender, EventArgs e)
{
var value = e;
}
The method will not fire for some reason. I select a different value and nothing happens. What I am trying to do is reload the page passing in the state value so I can filter the results by it.
What am I doing wrong?
Set AutoPostBack=True as an attribute of your DDL and it will automatically post back the selected index change event
Add this to dropdown list aspx it will cause a request to be send to the server and your event will be fired.
AutoPostBack="true"
You just need to set AutoPostBack = True
From ListControl.AutoPostBack property;
Gets or sets a value indicating whether a postback to the server
automatically occurs when the user changes the list selection.
AutoPostBack="true" and
write page load event
if (!IsPostBack)
{
DDL_Designation_Bind();
}
// Because autopostback properties fire load event then our dropdownlist rebind and always selected index 0 so Not Rebinding dropDownlist

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.

Changing dropdownlist selection causes full postback. (it's more interesting than it sounds)

Here's the scenario.
I have a search page with a TextBox that allows someone to type in a search term and press enter. (Which fires TextChanged). I have a DropDownList that specifies the kind of search that will be performed. It is defined in the markup as follows:
<asp:DropDownList ID="lstSearchType" runat="server" AutoPostBack="false">
<asp:ListItem Value="0">Last, First</asp:ListItem>
<asp:ListItem Value="1">Last</asp:ListItem>
<asp:ListItem Value="2">First</asp:ListItem>
<asp:ListItem Value="3">Liberty ID</asp:ListItem>
<asp:ListItem Value="4">E-mail</asp:ListItem>
<asp:ListItem Value="5">Telephone</asp:ListItem>
<asp:ListItem Value="6">Birthday (exact m/d/yyyy)</asp:ListItem>
<asp:ListItem Value="7">SSN (exact ###-##-####)</asp:ListItem>
<asp:ListItem Value="8">Address</asp:ListItem>
</asp:DropDownList>
As you can see, AutoPostBack is set to false, and there is no event hookup.
Pressing enter fires the OnTextChanged event for the TextBox, which performs a search and updates a GridView in an UpdatePanel. This UpdatePanel has its UpdateMode set to conditional and has one trigger: the TextChanged event of the search TextBox.
It's very simple.
And it works beautifully, almost.
Whenever I change the search type, the very next search does a full postback. All subsequent searches do partial postbacks (as desired) unless I change the search type again.
There is one exception to this rule: if I load the page and immediately change the search type, it doesn't do a full postback. So the first change of the DropDownList before any postback (full or partial) does not trigger a full postback.
Full Disclosure:
I'm doing a lot of JavaScript to change the appearance of the gridview during async requests. I don't detail it here because it seems unrelated. This problem only occurs when a DropDownList with no JavaScript wired up is changed.
Any ideas?
This is driving me crazy. Everything else is working.
Thanks in advance,
Clif
I figured it out. The problem was that the DropDownList was not in an UpdatePanel. It had no way to get the value without doing a full postback. The TextBox was immune to this because of the TextChanged event wiring.

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