How to execute a query when dropdownlist selected index changes - c#

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
});

Related

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.
}

SelectedIndexChanged event of DropDownList is not fired

ASPX FILE contain DropDown as Follows:
< asp:DropDownList ID="drpDist" runat="server" CssClass="dropDownStyle" OnSelectedIndexChanged="drpDist_SelectedIndexChanged" TabIndex="6">
In ASPX.CS FILE
protected void drpDist_SelectedIndexChanged(object sender, EventArgs e)
{
}
Please Help me.I can't get why it is not working.
Use Property
AutoPostBack="True"
you need to set AutoPostBack="true"
<asp:DropDownList ID="drpDist" runat="server" AutoPostBack="true">
when you set that property as true, a postback to the server automatically occurs whenever the user changes the selection of the list
You need to set the AutoPostBack="True" property. This will make the page to postback automatically hence firing your event.
Set AutoPostBack="true"
< asp:DropDownList ID="drpDist" runat="server" AutoPostBack="true" CssClass="dropDownStyle" OnSelectedIndexChanged="drpDist_SelectedIndexChanged" TabIndex="6">

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

Adding Item to DataBound Drop Down List

Yes, I have read most of the topics here, but I can't find an answer that works.
I have Three drop-down lists. The first is databound to grab distinct experiment names. The user selects, page posts back, and the second drop-down menu displays distinct time points. This is where I need help. I need to add an item to THAT drop-down list whose ID, DataTextField, DataValueField are all TimePt.
Seems simple, but I can't get it to work.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
TimePt.DataSource = TimePTDD;
TimePt.DataValueField = "TimePt";
TimePt.DataTextField = "TimePt";
TimePt.DataBind();
TimePt.Items.Insert(0, new ListItem("--Select---", "0"));
TimePt.SelectedIndex = 0;
}
}
I'm missing sometthing.
Set AppendDataBoundItems="true" on your dropdown list and it should work.
Here's a similar question: How to add Item to SqlDataSource databound list
And another one about potential duplicates using this method and a workaround for it: Dropdownlist AppendDataboundItems (first item to be blank)
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
TimePt.DataValueField = "TimePt";
TimePt.DataTextField = "TimePt";
var times = TimePTDD.ToList();
times.Insert(0, new {TimePt="0",TimePt="--Select--"});
TimePt.DataSource = times;
TimePt.DataBind();
//TimePt.SelectedIndex = 0;
}
}
<asp:DropDownList ID="ExpAnalysisName" runat="server"
DataSourceID="DropDownEXP" DataTextField="ExpAnalysisName"
DataValueField="ExpAnalysisName" AppendDataBoundItems="true" AutoPostBack=true>
<asp:ListItem Selected="True" Value="0">Select an Experiment</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="DropDownEXP" runat="server"
ConnectionString="<%$ ConnectionStrings:SGMD3_DataBase %>" SelectCommand="SELECT DISTINCT ExpAnalysisName FROM VW_Data">
</asp:SqlDataSource>
<asp:DropDownList ID="TimePt" runat="server" AutoPostBack="True" DataSourceID="TimePTDD" DataTextField="TimePt"
DataValueField="TimePt">
</asp:DropDownList>
<asp:SqlDataSource ID="TimePTDD" runat="server"
ConnectionString="<%$ ConnectionStrings:SGMD3_DataBase %>" SelectCommand="SELECT DISTINCT TimePt
FROM VW_Data
WHERE ExpAnalysisName = #ExpAnalysisName">
<SelectParameters>
<asp:FormParameter FormField="ExpAnalysisName" Name="ExpAnalysisName" />
</SelectParameters>
</asp:SqlDataSource>
So you can have a reference.
I see that you're specifying the DataSource in two different ways - The DataSourceID in your markup as well as manually setting the DataSource in your codebehind. Try removing the DataSourceID from your markup and see if that helps.
It's been a little while since I've used ASP.NET too much, but I have a feeling your DropDownList is rebinding after the Page_Load which would replace your previous binding.
My bet is this is a page lifecycle issue. Per MSDN, each data bound control whose DataSourceID property is set calls its DataBind method.
I think the values are getting bound to the dropdown twice. First, when you manually bind and append the extra item in Page_Load and then the datasource is being bound inside the Page_PreRender event. Try bringing your Page_Load code into Page_PreRender. Hopefully the order helps.
I suggest using OnDataBound event of DropDownList control, and put your code behind there.
That you way you can combine DataSourceID with code behind.

Categories