Dropdownlist SelectedIndexChanged not firing on aspx page - asp.net

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

Related

aspx dropdownList does not maintain selected value

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?

How to access selected value from the code behind in drop down list defined inside edititemtemplate using ASP.NET?

I have a DropDownlist inside an Edititemtemplate. I want to access the selected value from code behind.
My aspx
<EditItemTemplate>
<asp:DropDownList ID="ddlcountry" runat="server">
<asp:ListItem Text="Select Country" Value="0" disabled selected></asp:ListItem>
<asp:ListItem Text="india" Value="1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
My aspx.cs
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
//I want to access the dropdown value here//
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
DropDownList ddlCountries = (ListView.EditItem.FindControl("ddlcountry") as DropDownList);
if(ddlCountries!=null)
{
string value=ddlCountries.SelectedValue;
}
}

transfer listbox items from one asp.net page to another using session

Transfer listbox items from one asp.net page to another using session. I want to transfer the items of a list box from one asp.net page to another. The code somehow is throwing error. The items in the listbox are not being retrieved either. I want to do the same with a check box list. Hopefully the listbox issue will help[ me solve that too. Please advice.
First Page
<asp:ListBox ID="SelectedItems" runat="server" SelectionMode="Multiple"/>
<asp:Button ID="sbmtButton" runat="server" Text="Submit" Width="152px" OnClick="sbmtButton_Click" />
.cs for the First Page
protected void sbmtButton_Click(object sender, EventArgs e)
{Session["wrd"] = SelectedItems;Server.Transfer("~/aftrSubmit.aspx";);}
Second Page
.cs for the Second Page
protected void Page_Load(object sender, EventArgs e)
{if (!this.IsPostBack){Prescription_list = (ListBox)Session["wrd"];}}
First Page ASPX Code:
<asp:ListBox ID="SelectedItems" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="A"></asp:ListItem>
<asp:ListItem Text="B"></asp:ListItem>
<asp:ListItem Text="C"></asp:ListItem>
</asp:ListBox>
<asp:Button ID="sbmtButton" runat="server" Text="Submit" Width="152px" OnClick="sbmtButton_Click" />
First Page C# Code-Behind:
protected void sbmtButton_Click(object sender, EventArgs e)
{
ListItem[] x = new ListItem[SelectedItems.Items.Count];
SelectedItems.Items.CopyTo(x, 0);
Session["wrd"] = x;
Server.Transfer("~/aftrSubmit.aspx");
}
Second Page ASPX Code:
<asp:ListBox ID="Prescription_list" runat="server" SelectionMode="Multiple"/>
Second Page C# Code-Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ListItem[] x = (ListItem[])Session["wrd"];
Prescription_list.Clear();
Prescription_list.Items.AddRange(x);
}
}

Click Event for ImageButton Inside RadGrid

I have an asp.net ImageButton inside a RadGrid (in a column) that when clicked opens a popup window. I can also expand this same RadGrid to reveal a nested grid. I have a button inside here that I need to assign a click event to such that it opens the same popup. How do I fire off an ImageButton click that's housed inside a RadGrid?
Here is the aspx and the "imgEdit" is what I need to fire off:
<MasterTableView DataKeyNames="SuggestionID">
<EditFormSettings CaptionFormatString="Edit Suggestion: {0}" CaptionDataField="Title"
InsertCaption="Add Suggestion" EditFormType="WebUserControl" PopUpSettings-Width="655px"
UserControlName="~/CommonUserControls/SuggestionControl.ascx">
</EditFormSettings>
<Columns>
<telerik:GridTemplateColumn UniqueName="EditAction" HeaderStyle-Width="32px" ItemStyle-Wrap="false"
Resizable="false">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" CommandName="Edit" Resizable="false"
ImageUrl="/ESDNET/Images/Icons/pencil.png" ToolTip="Edit Suggestion" Visible='<%# Eval("CanEdit") %>' />
You can achieve the same in different ways:
1) Directly attach the button click event of imagebutton
protected void imgEdit_Click(object sender, ImageClickEventArgs e)
{
// your code
}
2) Using CommandName:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if(e.CommandName=="Edit")
{
}
}
3) If the commandname is Edit ,then it will autometically fire EditCommand
protected void RadGrid1_EditCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
}

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