DropDownList in Repeater control, can't fire SelectedIndexChanged - c#

I have a repeater control where in the footer I have a DropDownList. In my code-behind I have:
protected void ddMyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.AlternatingItem)
{
// Item binding code
}
else if (e.Item.ItemType == ListItemType.Footer)
{
DropDownList ddl = e.Item.FindDropDownList("ddMyDropDownList");
// Fill the list control
ddl.SelectedIndexChanged += new
EventHandler(ddMyDropDownList_SelectedIndexChanged);
ddl.AutoPostBack = true;
}
}
The page appear to PostBack however my EventHandler does not get called. Any ideas?

If you just want to fire the OnSelectedIndexChanged, this is how it should look:
Page.aspx - Source
<FooterTemplate>
<asp:DropDownList ID="ddlOptions"
runat="server"
AutoPostBack="true"
onselectedindexchanged="ddlOptions_SelectedIndexChanged">
<asp:ListItem>Option1</asp:ListItem>
<asp:ListItem>Option2</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
Page.aspx.cs - Code-behind
protected void ddlOptions_SelectedIndexChanged(object sender, EventArgs e)
{
//Event Code here.
}
And that's it. Nothing more is needed.

If the DropDownList is within a Repeater then to make the SelectIndexChanged event fire, you need to disable EnableViewState on the GridView / Repeater.
e.g.
EnableViewState="false"
You also need to databind the GridView / Repeater on each postback so databind it in the Page Load method.

I think it's because you're probably not databinding on postbacks. I haven't tested this, but try hooking that code up to the ItemCreated event for your repeater instead.

I think the problem comes from the fact that the dropdownlist control is not inside the repeter, but on the footer. I don't think that the envent of the reperter fires for the controls that are on the footer. You should try to put the dropdowncontrol out of the repeater control.

Is the AutoPostBack property set to True on the DropDownLists on the ASPX side? I know sometimes this property doesn't get set initially and it will prevent the SelectedIndexChanged event from firing.

In this case your parent repeater (ddMyRepeater) must databind itself in page_load on every postback. This is the only way I've found to get nested controls to fire their events.
This may not be the ideal scenario for you, though. Depending on what your page is doing, you may have to databind this control, twice. Once to get the events to fire and a second time if a fired event causes the repeater's data to change in any way.

Make sure ViewState is enabled for dropdownlist

Related

OnCheckedChanged not firing when clicked

I'm trying to run my OnCheckedChanged inside an itemtemplate, but it is not firing. What I did was I typed the OnCheckChanged in the asp:CheckBox tag and also typed the entire method manually. Would this affect the process??
<asp:CheckBox runat="server" ID="uoCheckBoxTagtoVehicle" OnCheckedChanged="ChkChanged" AutoPostBack="true" Width="50px" />
and my event:
protected void ChkChanged(object sender, EventArgs e)
{
uoHiddenFieldVehicle.Value = "1";
}
Note: I'm using Visual studio 2008
Since your control is inside a GridView (since you said ItemTemplate I assume you do) you can't use your approach to attach the event as you did. Because there will be multiple check boxes once you populate the GridView. Therefore, do the following
In you GridView's DataBinding event find the CheckBox by ID (use FindControl method)
Then attach the event OnCheckedChanged to the method you've written
Maybe you are databinding the page also on postback. You should do that only ...
if(!IsPostBack)
{
DataBindPage(); // method which databinds your controls like GridView
}
Otherwise you prevent that events are triggered.

Why DropDownList event fires after another event

I have a DropDownList in my page:
<asp:DropDownList ID="ddlPra" ClientIDMode="Static" CssClass="chosen-select" runat="server" OnSelectedIndexChanged="Practice_SelectedIndexChanged"></asp:DropDownList>
Code Behind:
protected void Practice_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(ddlCli.SelectedItem.Text);
}
I also have an UpdatePanel which has a GridView that displays some data with links. The issue I am having is when the page loads and I change the option from the select drop down, I don't get an alert but when I click on anything inside theUpdatePanel` the alert is displayed.
How do I fix it so that the DropDownList works independent from the UpdatePanel
Why DropDownList event fires after another event?
Because you have not set AutoPostBack="true", it is false by default. That means that it will not post back immediately after the user selected another item. But the event will be triggered on the next postback anyway, independent of the control that caused it.

UserControl DropDownList loses its selection

I have a user control with a DropDownList with AutoPostBack = true, also I have an aspx page to display this control.
I re-create user control in OnInit method of the page
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
var list = (List<Control>)Session[Controls];
if (list != null)
{
foreach (var control in list)
{
var uc = (Control)LoadControl(ControlPath);
uc.SetDropDownState(control.state);
PlaceHolderQuestion.Controls.Add(uc);
}
}
}
So while re-creating I am restoring DropDown selection, it works for the first time, but when I change selection again, OnSelectedIndexChanged event does not fire and it is obvious because I first restore DropDown selection in OnInit and so no OnSelectedIndexChanged event, cause nothing was changed, can you suggest some workaround?
UPDATE
var uc = (Control)LoadControl(ControlPath);
is required to be keep user controls events
After a deep research, I figured out that the issue was in the SelectedIndexChanged event logic.
I am using "Indirect Subscription" approach to handle it within my Page logic,
for more info please take a look
Indirect Subscription Approach
Originally I was reloading controls inside the handler but it was not required since I am doing it in the Page_Load method as well, so all I need to do is to update control inside my PlaceHolder controls list.
I hope it is clear, if not, please ask, and I will provide more description.

Multiple update panel triggers inside repeater

I'm having some trouble creating triggers for items inside of a repeater. I would like a Linkbutton control to trigger a postback from within an update panel, I have a trigger defined in markup for a Button control which works fine:
<Triggers>
<asp:PostBackTrigger ControlID="button" />
</Triggers>
However, I can't do this for the LinkButtons as they're created dynamically, only solution would be to add a trigger for each button in my repeaters data bound event like so:
//Inside repeater itemdatabound...
var trigger = new PostBackTrigger();
trigger.ControlID = linkButton.UniqueID;
updatepanel.Triggers.Add(trigger);
When running this code I receive an error:
A control with ID 'ctl00$content$repeater$ctl01$linkButton' could not be found for the trigger in UpdatePanel 'updatepanel'.
How can I dynamically add triggers for each of my LinkButtons?
Solved this. I'm assuming the reason that it didn't work in my OP is because the repeater controls aren't directly visible to the update panel.
I suspect moving them outside of the repeater would have solved it or making a tweak to the FindControl("linkbutton") call to drill down into the repeater for the control, using this method would mean that I need to create two link button objects for each level which is undesirable.
However, I think the cleaner solution is to register the LinkButton controls as postback controls using the scriptmanager:
//Create triggers for each 'remove' button
ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
if (scriptManager != null)
{
scriptManager .RegisterPostBackControl(linkbutton);
}
Within the repeaters OnItemDataBound event, solved it.
I seem to recall that you can use the clientID rather than the uniqueID property for this.
Neat solution would be:
protected void MyRepeater_OnItemCreated(object sender, RepeaterItemEventArgs e)
{
//Inside ItemCreatedEvent
ScriptManager scriptMan = ScriptManager.GetCurrent(this);
LinkButton btn = e.Item.FindControl("btnSubmit") as LinkButton;
if (btn != null)
{
btn.Click += btnSubmit_Click;
scriptMan.RegisterAsyncPostBackControl(btn);
}
}
This is the source thread

OnCheckedChanged event not firing in GridView at all

I have a GridView with an asp CheckBox in a TemplateField. The TemplateField is defined as follows:
<asp:TemplateField HeaderText="HeaderName">
<ItemTemplate>
<asp:CheckBox ID="checkBoxId" runat="server" OnCheckedChanged="MyCheckChangedMethod" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
When I run my web project with a breakpoint inside MyCheckChangedMethod and click the checkbox nothing happens. The breakpoint is not hit. My Visual Studio debugger is running.
Additionally, I have AutoEventWireup = True in my page defnition so I don't have to manually hook up the event. I have never had a problem doing it this way before. I have a button on the same page setup the exact same way with a click event and the breakpoint gets hit fine in that.
Any ideas?
The problem occurs when DataBind is called before the control event is firing.
If you call DataBind in Page_Load put it in
if (!isPostBack) {} and call DataBind in the event handler itself.
You need to add AutoPostback = True in asp:CheckBox tag.
The postback event for the checkbox control won't fire correctly because it's within a GridView that mangles the ID of the control.
If you need the Checkbox to reflect data you can use the CheckBoxField object and bind that way.
If you need it perform an action for the row, you may want to look at the ButtonField object using the CommandName property and the RowCommand event.
There are ways to access the checkboxes within the GridView server side.
try:
<asp:CheckBox ID="checkBoxId" runat="server" AutoPostBack=true OnCheckedChanged="MyCheckChangedMethod"/>
Make sure that the aspx page has CodeFile="YOUR_FILE.aspx.cs" at the top.
Also see to it that your function MyCheckChangedMethod is defined as
Function should have object sender, EventArgs e.
public void MyCheckChangedMethod(object sender, EventArgs e)
{
bool b = false;//your data here
}
Also make sure that the web.config has debug set to true (think already done).

Categories