SelectedIndexChanged - c#

Problem:
SelectedIndexchanged does not fire. I tried investigating with a breakpoint but it does not even get to the event.
I made the event by double clicking on the combobox. But it did not help.
Please advice.
Here is the code:
protected void nav_dd_parent_edit_SelectedIndexChanged(object sender, EventArgs e)
{
}
<td width="55%" class="style1" height="20px">
<asp:DropDownList ID="nav_dd_parent_edit" runat="server"
DataSourceID="sp_GetNavParents_Edit" DataTextField="Name"
DataValueField="NavItemId" Height="24px" ReadOnly="FALSE" Width="375px"
onselectedindexchanged="nav_dd_parent_edit_SelectedIndexChanged">
</asp:DropDownList>
</td>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Page.MaintainScrollPositionOnPostBack = true;
//SiteMaster.g_solution = "Couche-Tard - QV";
//SiteMaster.g_solution_id = 27;
nav_dd_parent.DataBind();
if (SiteMaster.g_solution != null && SiteMaster.g_solution != "")
{
nav_literal.Text = "Solution: " + SiteMaster.g_solution;
nav_hidden_SoltnId.Value = SiteMaster.g_solution_id.ToString();
}
else
{
nav_literal.Text = "Please select a solution first from the 'Solution Template' Tab.";
panel_top.Visible = false;
}
}

You're not seeing your breakpoint get hit because the dropdownlist is not posting back when the selection changes.
Set AutoPostBack to true and you should be all set.

I noticed your DropDownList Id is "nav_dd_parent_edit", but your Page_Load is calling the Databind method on "nav_dd_parent" - could that be part of the problem?
Anyway, I did a simplified version of your DropDownList that works fine - perhaps it might help.
<table>
<tr>
<td width="55%" class="style1" height="20px">
<asp:DropDownList
ID="nav_dd_parent"
runat="server"
DataTextField="Name"
DataValueField="NavItemId"
Height="24px"
ReadOnly="FALSE"
Width="375px"
onselectedindexchanged="nav_dd_parent_edit_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
</td>
</tr>
</table>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nav_dd_parent.Items.Add(new ListItem("Item 1", "1"));
nav_dd_parent.Items.Add(new ListItem("Item 2", "2"));
nav_dd_parent.Items.Add(new ListItem("Item 3", "3"));
}
}
protected void nav_dd_parent_edit_SelectedIndexChanged(object sender, EventArgs e)
{
int codeGetsHere = 0;
}

Few notes to keep in mind as below:
a. Set 'AutoPostBack' to true :
<asp:DropDownList ID="nav_dd_parent_edit" runat="server" AutoPostBack="true"
DataSourceID="sp_GetNavParents_Edit" DataTextField="Name"
DataValueField="NavItemId"
onselectedindexchanged="nav_dd_parent_edit_SelectedIndexChanged">
</asp:DropDownList>
b. Always bind on not postback time :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nav_dd_parent_edit.DataSource = yourDataSourceName;
nav_dd_parent_edit.DataBind();
}
}

Related

Listbox SelectedIndex always returns -1

I have a problem, when I try to get selectedindex from asp:listbox in codebehind, it always stays -1, even thought on the page it is selected. I am fully updating this list every minute. For loading listItems, the entire list gets erased and written back again.
Code samples:
<asp:ListBox ID="ListBox1" runat="server"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
AutoPostBack="false">
</asp:ListBox>
In codebehind:
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
itemsIndex = ListBox1.SelectedIndex.ToString(); //It is always -1
itemToBeRescheduled = ListBox1.SelectedItem.Value;
}
Tried using if(!isPostBack).. but the index stayed -1 and it only erased my items from the list.
Thanks in advance!
In my experience, it happens when you rebind ListView on postback again. In order to prevent it, we normally place binding inside !IsPostback.
For example,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.DataSource = new List<ListItem>()
{
new ListItem("One", "1"),
new ListItem("Two", "2"),
new ListItem("Three", "3")
};
ListBox1.DataBind();
}
}
When I have:
<asp:ListBox ID="ListBox1" runat="server" Height="140px" Width="290px" Font-Size="22px" autopostback="true" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:ListBox>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
And:
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex == -1)
TextBox1.Text = "-1";
else
TextBox1.Text = ListBox1.SelectedItem.Text;
}
I don't get ListBox1.SelectedIndex = -1.

ASP.NET checkbox autopostback not working

I designed a simple page with, two text boxes, one checkbox, one button, and one label.
When I start I want to check the checkbox to make the button enabled, and then enter two numbers into the two textboxes, click the button to do addition, and show the result in the label.
But when I click the checkbox the page postback is not working; it's not writing Page is posted back on the page and the button is still disabled.
However, if I make the button enabled and do the addition it invokes the page postback and also invokes the checkedchanged method.
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>
<asp:Label ID="result" runat="server"></asp:Label>
<td>
<asp:CheckBox ID="cboptions" runat="server" AutoPostBack="True"
onCheckedChanged="cboptions_CheckedChanged" />
</td>
<asp:Button ID="submit" runat="server" Text ="addition" onclick="Button_Click"/>
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
Response.Write("Page is posted back");
}
}
protected void cboptions_CheckedChanged(object sender, EventArgs e)
{
submit.Enabled = cboptions.Checked;
}
protected void submit_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirst.Text);
int b = Convert.ToInt32(txtSecond.Text)+a;
result.Text = b.ToString();
}
There were many formatting errors in your code, do it this way
Aspx
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>
<asp:Label ID="result" runat="server"></asp:Label>
<asp:CheckBox ID="cboptions" runat="server" AutoPostBack="True"
onCheckedChanged="cboptions_CheckedChanged" />
<asp:Button ID="btn" runat="server" Text ="addition" onclick="Button_Click"/>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
Response.Write("Page is posted back");
}
}
protected void cboptions_CheckedChanged(object sender, EventArgs e)
{
btn.Enabled = cboptions.Checked;
}
protected void Button_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirst.Text);
int b = Convert.ToInt32(txtSecond.Text) + a;
result.Text = b.ToString();
}

Drop Down List populated from SQL

I have a drop down list and the values I get them from SQL.
There are 4 choices. I need on one of the choices to turn a textbox.visible = false;
I am not sure that is correct. I have it in SQL as Cancel_Reason
protected void ddlCancelReason_SelectedIndexChanged(object sender, EventArgs e)
{
string Item = ddlCancelReason.SelectedValue;
if (Item == "Non-Payment")
{
tbReturn.Visible = false;
}
}
Did you bind a SelectedIndexChanged event to your DropDownList? If you did:
In your case it won't work because you didn't enable the AutoPostBack-property of the DropDownList.
Change your DropDownList code from:
<asp:dropdownlist id="ddlCancelReason" runat="server" datatextfield="Cancel_Reason" datavaluefield="ID"> </asp:dropdownlist>
To:
<asp:dropdownlist id="ddlCancelReason" AutoPostback="true" runat="server" datatextfield="Cancel_Reason" datavaluefield="ID"> </asp:dropdownlist>
Just add AutoPostback="true".
Then this will work:
protected void ddlCancelReason_SelectedIndexChanged(object sender, EventArgs e)
{
string Item = ddlCancelReason.SelectedValue;
if (Item == "Non-Payment")
{
tbReturn.Visible = false;
}
}

Datalist_ItemCommand event not firing

I have a datalist that has an item_Command event that is never firing.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
DataList1.DataSource = mySource;
}
protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e)
{
//dostuff
}
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand"
OnItemCreated="DataList1_ItemCreate">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Button ID="ButtonEditTask" runat="server" Width="60px" Text="Edit" CommandName="edit" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
When I do the postback, my datasource is null since I am not re assigning, therefore my event handler doesnt fire. So in order to fix it being null, i tried overriding init
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
DataList1.DataSource = mySessionSource;
}
Now I know the reason why its not working is because either
1. My Datasource is null BEFORE I added in the Override method and
2. My Controls event handler isn't being created in time since its rebinding every postback.
To fix 1, I added a datasource.
To fix 2 I added a datasource in init.
This didn't seem to fix anything however, and I do not know why. I also tried adding an event_handler in my init and it didn't do anything.
Make sure that you're binding the datasource to DataList1 otherwise the DataList will always be empty and the OnItemCreated event will never fire. You shouldn't need the OnInit override.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataList1.DataSource = mySource;
DataList1.DataBind();
}
}

catching SelectedIndexChanged of a dropdown list inside a repeater

I am trying to catch the SelectedIndexChanged of a DropDownList inside a Repeater. I have searched the internet but could not find a specific answer any help would be great. This is my code.
page.aspx
<asp:Repeater id="CategoryMyC" OnItemCommand="SomeEvent_ItemCommand" runat="server">
<HeaderTemplate>
<table><tr>
</HeaderTemplate>
<ItemTemplate>
<td>
<table width="100%">
<tr>
<th>Edit Carousel Item</th>
</tr>
<tr>
<td>Choose a product:</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlMcProducts"
DataTextField="Name"
onselectedindexchanged="MyListIndexChanged"
AutoPostBack="true"
DataSource='<%# ProductsManager.GetMerchantProductRepeater(Convert.ToInt32(Eval("MID"))) %>'
runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
</td>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
page.aspx.cs
In the Page_Load:
List<CarouselProducts> CP = CarouselProductsManager.GetCarouselItems(Convert.ToInt32(Session["Mid"]));
CategoryMyC.DataSource = CP;
CategoryMyC.ItemDataBound += new RepeaterItemEventHandler(RepeaterItemDataBound);
CategoryMyC.DataBind();
Other events:
protected void ddlMcProducts_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList d = (DropDownList)sender;
// Use d here
System.Windows.Forms.MessageBox.Show("I am changing");
}
protected virtual void PageInit(object sender, EventArgs e)
{
//get all the Carousel item of the merchant
List<CarouselProducts> CP = CarouselProductsManager.GetCarouselItems(Convert.ToInt32(Session["Mid"]));
//MerchantCategoryMyCarousel.DataSource = CP;
//MerchantCategoryMyCarousel.DataBind();
MerchantCategoryMyCarousel.DataSource = CP;
MerchantCategoryMyCarousel.ItemDataBound += new RepeaterItemEventHandler(RepeaterItemDataBound);
MerchantCategoryMyCarousel.DataBind();
}
protected virtual void RepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList theDropDown = sender as DropDownList;
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList MyList = (DropDownList)e.Item.FindControl("ddlMcProducts");
if (MyList == null)
{
System.Windows.Forms.MessageBox.Show("Did not find the controle");
}
else
MyList.SelectedIndexChanged += new EventHandler(MyListIndexChanged);
}
}
protected virtual void MyListIndexChanged(object sender, EventArgs e)
{
System.Windows.Forms.MessageBox.Show("I am changing");
}
protected void SomeEvent_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandSource.GetType() == typeof(DropDownList))
{
DropDownList ddlSomething = (DropDownList)e.Item.FindControl("ddlSomething");
System.Windows.Forms.MessageBox.Show("I am changing");
//Now you can access your list that fired the event
//SomeStaticClass.Update(ddlSomething.SelectedIndex);
}
}
I need to catch the SelectedIndexChanged of the populated DropDownList for each one generated.
You've got quite a mismatch of code going on here - using System.Windows.Forms in an ASP.NET application is just one of the issues. You appear to be assigning event handlers in the code-behind and in the markup (nothing necessarily bad about that, but there's seems to be no rhyme or reason to how you're doing it).
You're Repeater's ItemCommand event is bound to a method that is looking for a DropDownList that has a different ID than the one in your markup.
If you're using the System.Windows.Forms.MessageBox to debug (ala old school JavaScript and other language "debugging" methods), save yourself a world-class headache (not to mention a lot of unnecessary code cleanup when you're done with development) and step through your code in the debugger.
I'm not sure how the page will render, but I don't think you're using the HeaderTemplate and FooterTemplate quite the way they're intended.
All that said, try something like this:
Markup (ASPX page):
<asp:Repeater id="CategoryMyC" OnItemCommand="CategoryMvC_ItemCommand" OnItemDataBound="CategoryMvC_ItemDataBound" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Edit Carousel Item</th>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="100%">
<tr>
<td>Choose a product:</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlMcProducts"
DataTextField="Name"
OnSelectedIndexChanged="ddlMcProducts_SelectedIndexChanged"
AutoPostBack="true"
DataSource='<%# ProductsManager.GetMerchantProductRepeater(Convert.ToInt32(Eval("MID"))) %>'
runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Code Behind (APSX.CS)
protected void Page_Load(object sender, EventArgs e)
{
List<CarouselProducts> CP = CarouselProductsManager.GetCarouselItems(Convert.ToInt32(Session["Mid"]));
CategoryMyC.DataSource = CP;
//This can be assigned in the markup
//CategoryMyC.ItemDataBound += new RepeaterItemEventHandler(RepeaterItemDataBound);
CategoryMyC.DataBind();
}
protected void ddlMcProducts_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList d = (DropDownList)sender;
// Use d here
}
protected void CategoryMyC_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList theDropDown = sender as DropDownList;
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList MyList = (DropDownList)e.Item.FindControl("ddlMcProducts");
// This section is not needed for what you are doing with it:
// If the control is null, handle it as an error
// There's no need to give it an event handler if it does exist, because
// you already did so in the markup
//if (MyList == null)
//{
//System.Windows.Forms.MessageBox.Show("Did not find the controle");
//}
//else
//MyList.SelectedIndexChanged += new EventHandler(MyListIndexChanged);
//}
}
}
protected void CategoryMyC_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandSource.GetType() == typeof(DropDownList))
{
// Note the correct control name is being passed to FindControl
DropDownList ddlSomething = (DropDownList)e.Item.FindControl("ddlMcProducts");
//System.Windows.Forms.MessageBox.Show("I am changing");
//Now you can access your list that fired the event
//SomeStaticClass.Update(ddlMcProducts.SelectedIndex);
}
There may be more issues at hand as well - but this will hopefully streamline it enough for you to make some progress.
protected void drpOrganization_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
RepeaterItem item = (RepeaterItem)ddl.NamingContainer;
if (item != null)
{
CheckBoxList list = (CheckBoxList)item.FindControl("chkSite");
if (list != null)
{
}
}
}

Categories

Resources