SelectedIndexChanged event of DropDownList is not fired - c#

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">

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

Different result when adding same attributes to asp control?

So I have this method that should run on TextChanged of a text box:
void CheckIn_TextChanged(object sender, EventArgs e)
{
checkIn.Text += "It Worked!";
}
In the aspx file I have this control:
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" AutoPostBack="true" TextChanged="CheckIn_TextChanged"></asp:textbox>
All the attributes work as they should except the TextChanged?
But if I remove this from the control and set it in the codebehind on page_load like so: checkIn.TextChanged = CheckIn_TextChanged; it does work?!
So my question is this why does it work when setting in codefile behind but not assigning the attribute to the control in the aspx file? Where am I going wrong?
Event name should be OnTextChanged. (Not TextChanged)
<asp:TextBox runat="server" ID="checkIn"
ClientIDMode="Static"
AutoPostBack="true"
OnTextChanged="CheckIn_TextChanged">
</asp:TextBox>

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

Getting a dropdownlist to check a checkbox in asp.net/C#

<input runat ="server" type ="checkbox" id="helprequest" />
<label for="helprequest">Help request</label>
<asp:DropDownList ID="options" runat="server" OnSelectedIndexChanged="checkHelpRequest">
<asp:ListItem Text="Windows"></asp:ListItem>
<asp:ListItem Text="Macintosh"></asp:ListItem>
<asp:ListItem Text="Linux"></asp:ListItem>
<asp:ListItem Text="Other"></asp:ListItem>
</asp:DropDownList>
In my codebehind, I have
protected void checkHelpRequest(object sender, EventArgs e)
{
helprequest.Checked = true;
}
But when I select something on the dropdownlist, the checkbox, does not get marked as checked, how do I get the checkbox to appear as checked when I change the index on a dropdownlist?
Your DropDownList does not have AutoPostBack='true' set. Without setting this, your dropdown will not post back when you change the selected index.
Just change it to:
<asp:DropDownList AutoPostBack="true" ID="options"
runat="server" OnSelectedIndexChanged="checkHelpRequest">
Without setting this, your checkHelpRequest method will still be called when your drop down changes index, but only after a postback is caused by some other control, like a button, or another DropDownList that does have AutoPostBack set.

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