aspx dropdownList does not maintain selected value - asp.net

Inherited code with a dropdownlist which is used to populate some GridViews. When a selection is made in the dropdownlist, the selection does not keep and it jumps back up to the first record. A requently mentioned fix is to add the !Page.IsPostBack in the Page_Load event, however in doing so does not resolve the issue.
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"
DataSourceID="SqlDataSource2" DataTextField="FullAddress"
DataValueField="UniqueRecordID" Width="445px" Height="20px"
style="margin-left: 0px" TabIndex="8" AppendDataBoundItems="true"
OnSelectedIndexChanged="resetGridsAndLabel">
</asp:DropDownList>
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList2.DataSource = sqlDataSource2;
DropDownList2.DataBind();
}

Are you setting the data source for the DDL anywhere else on the page?

Related

Drop down selected index changed event not firing event the autopostback = true

Dropdown selectedindexchanged event not firing event the autopostback=true and also i tried causes validation to false/true but not luck here is the code
<asp:DropDownList ID="DropDownList15" runat="server" EnableViewState="true"
CausesValidation="false" AutoPostBack="True" DataSourceID="ds_Country"
DataTextField="Country" style="margin-left:70px;"
DataValueField="CountryId" AppendDataBoundItems="True"
onselectedindexchanged="ddlCountry_SelectedIndexChanged1"> </asp:DropDownList>
code behind is simple like
protected void ddlCountry_SelectedIndexChanged1(object sender, EventArgs e)
{
Session["CountryId"] = ddlCountry.SelectedValue.ToString();
}

Dropdownlist SelectedIndexChanged not firing on aspx page

I want to auto click the search button whenever user select a value from drop down list.
code snippet:
Added the event handler in InitializeComponent() :
this.ddltrim.SelectedIndexChanged += new System.EventHandler(this.ddltrim_SelectedIndexChanged);
code:
private void ddltrim_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(ddlStores.Items.Count ==1)
btnSearch_Click("Search", null);
}
In designer:
<asp:dropdownlist id=ddltrim width="100%" Runat="server" AutoPostBack="True" EnableViewState="True">
<asp:ListItem Value="Select Submodel" Selected="True">
Select SubModel
</asp:ListItem></asp:dropdownlist>
But selectIndexChanged in not firing when i select any value from ddl. Have to manually click the button search.
Try
<asp:dropdownlist id=ddltrim width="100%" Runat="server" AutoPostBack="True" EnableViewState="True" onselectedindexchanged="ddltrim_SelectedIndexChanged">
<asp:ListItem Value="Select Submodel" Selected="True">Select SubModel</asp:ListItem>
Try to set your add event code in Page_PreInit method
protected void Page_PreInit(object sender, EventArgs e)
{
this.ddltrim.SelectedIndexChanged += new System.EventHandler(this.ddltrim_SelectedIndexChanged);
}

ASP.Net AJAX UpdatePanel not working with Repeater and RadioButtons

I have a repeater which includes a radio button in each item, and the whole thing sites inside an update panel. When I select a radio button the whole page reloads. Why is it not just updating the update panel. I've reduced this to a pretty simple example to avoid clutter. Code here...
ASPX...
<asp:ScriptManager ID="SM1" runat="server" />
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater runat="server" ID="history">
<ItemTemplate>
<asp:RadioButton runat="server" ID="radioButton" AutoPostBack="true" GroupName="HistoryGroup" OnCheckedChanged="RadioButton_CheckChanged" /><br />
</ItemTemplate>
</asp:Repeater>
<p><asp:Literal runat="server" ID="output" /></p>
</ContentTemplate>
</asp:UpdatePanel>
Code...
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<int> list = new List<int>();
for (int i = 0; i < 20; i++)
list.Add(i);
history.DataSource = list.ToArray();
history.DataBind();
}
}
protected void RadioButton_CheckChanged(Object sender, EventArgs e)
{
output.Text = DateTime.Now.ToString();
}
}
Setting ClientIDMode=Auto on the RadioButton should fix it (it's an infamous .NET bug, http://connect.microsoft.com/VisualStudio/feedback/details/584991/clientidmode-static-in-updatepanel-fails-to-do-async-postback)
please add up1.Update() after output.Text = DateTime.Now.ToString(). Your RadioButton is not the trigger for updatepanel
Turns out the solution was to remove the GroupName from the RadioButton. When I remove this tag it fires asynchronously and just updates the panel. I don't actually need this tag anyway (due to the known bug where GroupName doesn't work on RadioButtons in Repeaters) as I handle the grouping within my click event (i.e. uncheck any other RadioButtons of the same name in other repeater items).

asp.net + listbox +change of value

I have a listbox in asp.net
<asp:ListBox ID="ListBox1" class="listItem" runat="server"
onselectedindexchanged="ListBox1_SelectedIndexChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:ListBox>
I want to trigger a function when somebody changes it values...
code which i tried is
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Label6.Text = Label6.Text + "hello";
}
What's wrong here..
By setting the autopostback as true, my problem is solved but the page is reloaded everytime i change its value which i dnt want as large amount of data get reloaded on every autopostback(bcoz of images on the page).Even i put the following code in page load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
but it didnt work out...
The problem here is that you expect that the SelectedIndexChanged event is triggered immediately. But by default the ListControl's AutoPostBack property is set to false.
true if a postback to the server automatically occurs whenever the
user changes the selection of the list; otherwise, false. The default
is false.
So set it to true:
<asp:ListBox ID="ListBox1" AutoPostBack="true" class="listItem" runat="server"
onselectedindexchanged="ListBox1_SelectedIndexChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:ListBox>
AutoPostBack="true" is missing add it on ListBox1

RadioButtonList selection IndexChanged

My Scenario,
When i select the option in list i need to perform some action like Textbox should be disabled. But i don find my breakpoint pointing to that action to be performed.Pls help
I guess you mean the SelectedIndexChanged event does not fire that is so because you'vent set the AutoPostBack to true.
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
</asp:RadioButtonList>
Code
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox4.Enabled = false;
}

Resources