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

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.

Related

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.

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 execute a query when dropdownlist selected index changes

I have a DropDownList which is populated using a sqldatasource, i.e., from database and their is a grid view which is populated with another sqldatasource connected to using the value of dropdownlist.
But it does not execute the query dynamically. I want that whenever the value of dropdownlist changes, the grid view should update.
Code please..
ASPX code
<asp:DropDownList id="ddlCountry" AutoPostBack="True" runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" ></asp:DropDownList>
And CS code
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
FillYourGridviewHere();
}
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="ssn" DataValueField="ssn"
AutoPostBack=true>
</asp:DropDownList>
This Worked. Thank You.!!
You can just write the dynamic code for populating the grid into a function and bind that function to the dropdownlist onChange event.
Say "mydropdown" is the id of your dropdownlist and "dochanges" is a function that executes your dynamic codes. So you just need to bind the dochanges function to the change event of your dropdownlist.
$('#mydropown').bind('change',function(){
dochanges(); //call the dynamic function where you update your grid
});

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.

How to set DropDownList's selected value inside a Repeater?

I need to create a group of DropDownLists to show and allow change of a property group for an item.
I have the following code on my ASP page.
<asp:Repeater runat="server" ID="repeaterProperties">
<ItemTemplate>
<p><asp:DropDownList runat="server" ID="ddProperty" OnInit="ddProperty_OnInit" /><p>
</ItemTemplate>
</asp:Repeater>
The ddProperty_OnInit populates the DropDownList with all the possible values with a database query.
How can I set the selected value of each created DropDownList according to the Repeater's source data?
Let's say, for example, that we have the possible property values of A, B and C.
If the database output for the Repeater contains two of those values, A and B, the Repeater outputs two DropDownLists, both with all 3 values availabla and the first one with A as selected value and the second one with B as selected value.
Edit:
It seems that adding OnItemDataBound="repeater_ItemDataBound" to the Repeater and selecting the appropriate value in there is not the way to go in my case. This is because I also need to save the possibly changed values to a database.
The ItemDataBound event of the Repeater is fired before the OnClick event on a Button and changes the selected values to their old values before the new selections can be saved.
Any suggestion on how to work around this?
Current code:
<asp:Repeater runat="server" ID="repeaterJako" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<asp:DropDownList id="ddJako" runat="server" OnInit="ddJako_OnInit">
</asp:DropDownList><br />
</ItemTemplate>
</asp:Repeater>
<asp:Button runat="server" id="updateButton" Text="Save" OnClick="update_OnClick" />
In the code-behind, ddJako_OnInit populates the drop down list with all the possible choises, while the repeater_ItemDataBound uses the method suggested by Bryan Parker to select the proper value.
Maybe I'm misunderstanding something about your question... but it seems like this is exactly what OnItemDataBound is for. :)
Use FindControl to get a reference to your DropDownList in the event handler. Also check to make sure the item is not the header/footer. The example from MSDN does both these things:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.onitemdatabound.aspx
In regard the problem I specified in my edit, the time of the DataBind plays an important role. I used to do the databinding in the Page_Init event, which caused the repeater_ItemDataBound event to be fired before the button_OnClick event.
The solution was to move the databinding to the Page_PreRender event.
The population of the DropDownList with all the choises is still done in its OnInit event.

Categories