Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Web part control event not firing View modes: 
User avatar
Member
Member
robert-pulldigital - 8/9/2013 5:19:39 AM
   
Web part control event not firing
A web part has a dropdownlist on it and the selectedindexchanged event is not firing.

I've seen a couple of suggestions one is using my web part in the user control web part from the general tab. The web part itself has fields that are set in the CMS and when using the user control part to contain my custom web part there is no way to set the fields in the CMS.

So how can I ensure the events fire and have the ability to specify the fields.

Cheers

User avatar
Member
Member
robert-pulldigital - 8/9/2013 5:33:40 AM
   
RE:Web part control event not firing
I looked at another way to do it and found the control items are not reloaded on postback so they need adding dynamically everytime which is fine. When a value is selected and the control posts back I do want to know the currently selected value as well and currently the control simply reinitialises and doesn't remember the value that was selected.

User avatar
Member
Member
Swainy - 8/9/2013 5:38:25 AM
   
RE:Web part control event not firing
Hi Robert,

I have had this problem quite a few times. Are you putting the code in the SetupControl() method of the web part? This fires too early in the page lifecycle for drop down lists to work effectively.

If you move this code to either Page_Load or OnLoad methods then it will work as you expect.

Thanks,

Matt

User avatar
Member
Member
robert-pulldigital - 8/9/2013 5:52:55 AM
   
RE:Web part control event not firing
The code I want to fire is in the selectedindexchanged event of the dropdown.

The code to populate the dropdown from the web part properties is in an override of OnLoad

User avatar
Member
Member
Swainy - 8/9/2013 5:55:21 AM
   
RE:Web part control event not firing
ok so this should work fine.

This is just like .NET would usually work.

So in your OnLoad you need to wrap your binding in a if (!IsPostBack).

i.e.
if (!IsPostBack)
{
BindDataHere();
}

On_selectedIndexChanged()
{
index/value = ddl.selectedindex/value
}
Thanks,
Matt

User avatar
Member
Member
robert-pulldigital - 8/9/2013 5:59:10 AM
   
RE:Web part control event not firing
Spot on that's the plan however the SelectedIndexChanged is not firing on my web part.

Not sure why and the dropdown is no longer populated on postback either except Viewstate is likely disabled.

I'll keep looking at it since this has to work! :)

User avatar
Member
Member
Swainy - 8/9/2013 6:19:06 AM
   
RE:Web part control event not firing
yeah that should do it.

Really basic question... but I am assuming you have set AutoPostBack to true on the drop down list?

User avatar
Member
Member
robert-pulldigital - 8/9/2013 6:25:18 AM
   
RE:Web part control event not firing
Yep it's a standard web part as far as i'm concerned but i'm still new to Kentico so the aspx is
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RedirectorDropdown.ascx.cs"
Inherits="CMSWebParts_PullDigital_RedirectorDropdown" %>
<asp:DropDownList ID="ddlRedirector" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlRedirector_SelectedIndexChanged">
</asp:DropDownList>

and the code behind in total is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.GlobalHelper;
using CMS.PortalControls;

public partial class CMSWebParts_PullDigital_RedirectorDropdown : CMSAbstractWebPart
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Page.IsPostBack)
{
EnsureItems();
}
}

public string TextValues
{
get
{
return ValidationHelper.GetString(GetValue("TextValues"), "");
}
set
{
SetValue("TextValues", value);
}
}

public string DataValues
{
get
{
return ValidationHelper.GetString(GetValue("DataValues"), "");
}
set
{
SetValue("DataValues", value);
}
}

protected void Page_Load(object sender, EventArgs e)
{

}

private void EnsureItems()
{
if (ddlRedirector.Items.Count == 0)
{
string[] textValues = TextValues.Split(';');
string[] dataValues = DataValues.Split(';');

if (textValues.Length > 0 && textValues.Length == dataValues.Length)
{
ddlRedirector.Items.Add(new ListItem("", ""));
for (int i = 0; i < textValues.Length; i++)
{
ddlRedirector.Items.Add(new ListItem(textValues, dataValues));
}
}
}
}

protected void ddlRedirector_SelectedIndexChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(ddlRedirector.SelectedValue))
{
Response.Redirect(ddlRedirector.SelectedValue);
}
}
}

The redirector method is never firing and the control is not repopulated on postback from viewstate but this won't be a problem as the control is redirecting the user to a new page.

User avatar
Member
Member
Swainy - 8/9/2013 8:33:13 AM
   
RE:Web part control event not firing
Hmmm how very odd. I can't see anything wrong with your code.

What happens when you debug it? It literally doesn't fire the selected index changed?

Does it fire the page load when you change the drop down list or not doing anything?

Thanks,

Matt

User avatar
Member
Member
robert-pulldigital - 8/9/2013 8:45:52 AM
   
RE:Web part control event not firing
The page load event fires but not the selectedindexchanged event.

In fact no control events fire. I've seen reference to making the control a standard UserControl and placing my control in the usercontrol webpart Kentico provide, this is fine but then I lose the ability to have the properties of the control set by the Kentico framework.

I would like to have both.

I added a button and the click event is firing. It's only this dropdownlist I have trouble with.

User avatar
Member
Member
robert-pulldigital - 8/9/2013 8:56:39 AM
   
RE:Web part control event not firing
If I populate the dropdown in the ASCX the event fires except when index 0 is selected. Definitly some overriding of behaviour happening I just need to know how to dynamically populate the dropdown and still have the events fire.

It's likely related to the framework not understanding the dropdown has been changed as it's losing its values on postback as well

Cheers

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 8/9/2013 8:51:48 AM
   
RE:Web part control event not firing
Webparts have a different page lifecycle than what you are used to with a typical asp.net user control. Try this:
public override void OnContentLoaded()
{
base.OnContentLoaded();
EnsureItems()
}
Simply moving your EnsureItems() method out of the page load and into the OnContentLoaded method will help. AND since you are loading the dropdown dynamically, you will need to do so on each postback.

User avatar
Member
Member
robert-pulldigital - 8/9/2013 8:59:29 AM
   
RE:Web part control event not firing
ummmmm, yes that's worked, the events are being fired now.

Big thanks to Swainy for sticking with me on this one :)

Problem solved Froggy thanks.


User avatar
Member
Member
Swainy - 8/9/2013 9:00:23 AM
   
RE:Web part control event not firing
but won't loading the dropdown dynamically every time the page is loaded remove the item that you have selected (given you are rebinding the data again?).

Or does OnContentLoaded only fire after all of the other events have fired (including selected index changed)?

User avatar
Member
Member
robert-pulldigital - 8/9/2013 9:29:08 AM
   
RE:Web part control event not firing
Well that may be true but in this case if an option is selected the page redirects so forgetting the selected item is not important.

As for loading dynamically it's fine if you load the items before Viewstate is turned as .net automatically reselects the correct item by querying the viewstate for it's selected value. I don't know if Viewstate works as normal in Kentico but this has solved my particular issue.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 8/9/2013 11:13:02 AM
   
RE:Web part control event not firing
No, you are correct, you have to store the selected value in ViewState or session or some other variable in order to set it again after you bind the dropdownlist. Same as if you were creating a dropdown within a standard asp.net website.