Custom Server Control with DataList and ItemCommand not firing

R

Ryan Shaw

I’m creating a custom server control which contains a DataList.
The ItemDataBound event fires fine but the ItemCommand event does not fire.
I am calling DataBind only on non post backs but still can’t figure this out.
Bellow is the control I’m working with. I know it’s a lot but its just the
ItemCommand event which is not working.
_______________________________________________________
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MSTRWebPart
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class DisplayReport:Control,INamingContainer
{
//private MSTRFacade.Provider mstrProvider=new
MSTRFacade.Provider("D818343U-21422","Administrator","");
private MSTRFacade.Provider mstrProvider=new
MSTRFacade.Provider("localhost","Administrator","");
private ArrayList mstrProjects;

protected DropDownList ddlProjects;
protected RadioButtonList rdoFolders;
protected Button cmdGetObjects;
protected DataList dlObjects;

public DisplayReport()
{
}

protected override void CreateChildControls()
{
base.CreateChildControls ();

ddlProjects=new DropDownList();
ddlProjects.DataSource=mstrProjects;
ddlProjects.DataTextField="ObjName";
ddlProjects.DataValueField="ObjID";
Controls.Add(ddlProjects);

rdoFolders=new RadioButtonList();
rdoFolders.Items.Add(new ListItem("All", ""));
rdoFolders.Items.Add(new ListItem("My Reports",""));
rdoFolders.Items.Add(new ListItem("Shared Reports",""));
Controls.Add(rdoFolders);

cmdGetObjects=new Button();
cmdGetObjects.Text="Get Objects";
cmdGetObjects.Click+=new EventHandler(this.cmdGetObjects_Click);
Controls.Add(cmdGetObjects);

dlObjects=new DataList();
dlObjects.ItemDataBound+=new
DataListItemEventHandler(this.dlObjects_ItemDataBound);
dlObjects.ItemCommand+=new
DataListCommandEventHandler(this.dlObjects_ItemCommand);
Controls.Add(dlObjects);

if(!this.Page.IsPostBack)
this.DataBind();

}

protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
if(mstrProjects==null)
{
mstrProjects=mstrProvider.GetProjects();
this.Page.Session["Projects"]=mstrProjects;
}
else
{
mstrProjects=(ArrayList)this.Page.Session["Projects"];
}
}

protected override void Render(HtmlTextWriter output)
{
EnsureChildControls();
ddlProjects.RenderControl(output);
cmdGetObjects.RenderControl(output);
rdoFolders.RenderControl(output);
dlObjects.RenderControl(output);
}


private void cmdGetObjects_Click(Object sender, EventArgs e)
{
//TODO: insert if statement for path selection
MSTRFacade.ProjectInfo
proj=(MSTRFacade.ProjectInfo)mstrProjects[ddlProjects.SelectedIndex];

dlObjects.DataSource=mstrProvider.GetObjectsByPath(proj.ObjName,proj.SharedReportsPath);
dlObjects.DataBind();
}
private void dlObjects_ItemDataBound(Object sender, DataListItemEventArgs e)
{
LinkButton btnLink=new LinkButton();
MSTRFacade.ObjectInfo obj=(MSTRFacade.ObjectInfo)e.Item.DataItem;

btnLink.Text=obj.ObjName;
btnLink.CommandName=obj.ObjID;
e.Item.Controls.Add(btnLink);
}

private void dlObjects_ItemCommand(Object sender, DataListCommandEventArgs
e)
{
System.Diagnostics.Debug.Write("ItemCommand:"+e.CommandName);
//dlObjects.DataSource=mstrProvider.GetObjects(ddlProjects.SelectedItem.ToString(),e.CommandName);
//dlObjects.DataBind();
}
}
}
 
S

Sergey Polyakov

Obviously the original control which initiated the postback (LinkButton)
does not exist at the moment the Page tries to find it (in order to pass the
postback processing to it). Try to ensure that the entire hierarchy of your
child controls is correctly restored for example at the end of the Load
phase.

--
Best regards,
Sergey


Ryan Shaw said:
I'm creating a custom server control which contains a DataList.
The ItemDataBound event fires fine but the ItemCommand event does not
fire.
I am calling DataBind only on non post backs but still can't figure this
out.
Bellow is the control I'm working with. I know it's a lot but its just
the
ItemCommand event which is not working.
_______________________________________________________
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MSTRWebPart
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class DisplayReport:Control,INamingContainer
{
//private MSTRFacade.Provider mstrProvider=new
MSTRFacade.Provider("D818343U-21422","Administrator","");
private MSTRFacade.Provider mstrProvider=new
MSTRFacade.Provider("localhost","Administrator","");
private ArrayList mstrProjects;

protected DropDownList ddlProjects;
protected RadioButtonList rdoFolders;
protected Button cmdGetObjects;
protected DataList dlObjects;

public DisplayReport()
{
}

protected override void CreateChildControls()
{
base.CreateChildControls ();

ddlProjects=new DropDownList();
ddlProjects.DataSource=mstrProjects;
ddlProjects.DataTextField="ObjName";
ddlProjects.DataValueField="ObjID";
Controls.Add(ddlProjects);

rdoFolders=new RadioButtonList();
rdoFolders.Items.Add(new ListItem("All", ""));
rdoFolders.Items.Add(new ListItem("My Reports",""));
rdoFolders.Items.Add(new ListItem("Shared Reports",""));
Controls.Add(rdoFolders);

cmdGetObjects=new Button();
cmdGetObjects.Text="Get Objects";
cmdGetObjects.Click+=new EventHandler(this.cmdGetObjects_Click);
Controls.Add(cmdGetObjects);

dlObjects=new DataList();
dlObjects.ItemDataBound+=new
DataListItemEventHandler(this.dlObjects_ItemDataBound);
dlObjects.ItemCommand+=new
DataListCommandEventHandler(this.dlObjects_ItemCommand);
Controls.Add(dlObjects);

if(!this.Page.IsPostBack)
this.DataBind();

}

protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
if(mstrProjects==null)
{
mstrProjects=mstrProvider.GetProjects();
this.Page.Session["Projects"]=mstrProjects;
}
else
{
mstrProjects=(ArrayList)this.Page.Session["Projects"];
}
}

protected override void Render(HtmlTextWriter output)
{
EnsureChildControls();
ddlProjects.RenderControl(output);
cmdGetObjects.RenderControl(output);
rdoFolders.RenderControl(output);
dlObjects.RenderControl(output);
}


private void cmdGetObjects_Click(Object sender, EventArgs e)
{
//TODO: insert if statement for path selection
MSTRFacade.ProjectInfo
proj=(MSTRFacade.ProjectInfo)mstrProjects[ddlProjects.SelectedIndex];

dlObjects.DataSource=mstrProvider.GetObjectsByPath(proj.ObjName,proj.SharedReportsPath);
dlObjects.DataBind();
}
private void dlObjects_ItemDataBound(Object sender, DataListItemEventArgs
e)
{
LinkButton btnLink=new LinkButton();
MSTRFacade.ObjectInfo obj=(MSTRFacade.ObjectInfo)e.Item.DataItem;

btnLink.Text=obj.ObjName;
btnLink.CommandName=obj.ObjID;
e.Item.Controls.Add(btnLink);
}

private void dlObjects_ItemCommand(Object sender, DataListCommandEventArgs
e)
{
System.Diagnostics.Debug.Write("ItemCommand:"+e.CommandName);
//dlObjects.DataSource=mstrProvider.GetObjects(ddlProjects.SelectedItem.ToString(),e.CommandName);
//dlObjects.DataBind();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top