PDA

Click to See Complete Forum and Search --> : LoadControl() Postback and events


Magiaus
Aug 23rd, 2004, 11:35 PM
I am having a problemn where I am loading an UserControl. The user control has two buttons Move Up and Move Down.

The events are wired, and there is code in the buttons; but nothing happens. Even when I click the button. Nothing happens. Post back occurs but the event does not fire and my picture doesn't move up one in it's sequence.

I had the problem before and I messed with it for days and it started to work. I don't know or remember why. HELP!!!!



Thanks.

Magiaus
Aug 23rd, 2004, 11:52 PM
I'm seeing the problem again. The controls have to be loaded in the same order to load the viewstate and have postback work.

I just made a simple one page web that loads a control with a button. It works. When I click the button a label says clicked.

It works with ViewState disabled for the panel I'm loading too, but of cource only one control keeps the record of being clicked.

urg

nemaroller
Aug 24th, 2004, 09:06 AM
So you solved it...?

Magiaus
Aug 24th, 2004, 09:38 AM
not yet.

nemaroller
Aug 24th, 2004, 09:41 AM
Post your code please...

Magiaus
Aug 24th, 2004, 05:01 PM
i will tomorrow

Magiaus
Aug 25th, 2004, 07:50 AM
Business.Images.SourceCollection imgs = new Business.Images.SourceCollection(ProductExKey);
System.Web.UI.Control img;

pnl_newSourcePictures.Controls.Clear();
for(int i = 0; i < pnl_newSourcePictures.Controls.Count; i++)
{
pnl_newSourcePictures.Controls.RemoveAt(i);
}

if(imgs.Count > 0)
{
for(int i = 0; i < imgs.Count;i++)
{
img = LoadControl("../../UserControls/Forms/Edit/Images/SourceSequence.ascx");

((Web.UserControls.Forms.Edit.Images.SourceSequence)img).SourceKey = imgs[i].Key;
((Web.UserControls.Forms.Edit.Images.SourceSequence)img).ImageKey = imgs[i].ImageKey;

pnl_newSourcePictures.Controls.Add(img);
}
}

Magiaus
Aug 25th, 2004, 07:52 AM
The Control loads fine and it in turn loads the image fine. But the events for the buttons won't fire. At first I was loading inside another user control but last night I put it on a page just to see... it didn't work there either.

Magiaus
Aug 25th, 2004, 07:53 AM
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for SourceSequence.
/// </summary>
public abstract class SourceSequence : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button btn_down;
protected System.Web.UI.WebControls.Button btn_up;
protected System.Web.UI.WebControls.Button btn_delete;
protected System.Web.UI.WebControls.Image imgSrc;


private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
try
{
SourceKey = SourceKey;
}
catch(System.Exception ex)
{
ex = ex;
SourceKey = System.Guid.Empty;
}

try
{
ImageKey = ImageKey;
}
catch(System.Exception ex)
{
ex = ex;
ImageKey = System.Guid.Empty;
}
}
}
public System.Guid SourceKey
{
get{return (System.Guid)ViewState["-SourceKey"];}
set{ViewState["-SourceKey"] = value;}
}

public System.Guid ImageKey
{
get{return (System.Guid)ViewState["pid"];}
set
{
ViewState["pid"] = value;

if(ImageKey != System.Guid.Empty)
{
imgSrc.ImageUrl = Helpers.Common.AppSetting("-baseURL") + "Images/Image.aspx?pid=" + value.ToString();
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btn_up.Click += new System.EventHandler(this.btn_up_Click);
this.btn_down.Click += new System.EventHandler(this.btn_down_Click);
this.btn_delete.Click += new System.EventHandler(this.btn_delete_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btn_up_Click(object sender, System.EventArgs e)
{
if(SourceKey != System.Guid.Empty)
{
Business.Images.Source src = new Business.Images.Source(SourceKey);

if(src.Sequence != Business.Images.Source.Count() - 1)
{
Business.Images.Source.ReSequence(src, src.Sequence + 1);
}
src = null;
}
}

private void btn_down_Click(object sender, System.EventArgs e)
{
if(SourceKey != System.Guid.Empty)
{
Business.Images.Source src = new Business.Images.Source(SourceKey);
if(src.Sequence != 0)
{
Business.Images.Source.ReSequence(src, src.Sequence - 1);
}
src = null;
}
}

private void btn_delete_Click(object sender, System.EventArgs e)
{
Business.Images.Source.Delete(new Business.Images.Source(SourceKey));
}
}

Magiaus
Aug 25th, 2004, 07:57 AM
I don't know. I know I had fixed this before somehow but I had thought SmartNavigation = true was causing it..... SmartNav is off......

nemaroller
Aug 25th, 2004, 08:31 AM
Well since it worked before....

Problems I have experienced with events not wiring usually indicate the control is not matching up with the post data. So the framework ignores the events either because the control is reloading itself anew, instead of restructuring itself from the posted viewstate.

Your code seems pretty spot on,so my guess is during initialization, you are not assigning an ID for the control, so the framework gives it a new ID, thus the events don't wire.

Try explicitly declaring an ID for your control first during the Load, and see if that fixes it. Or it could be the ID's of the buttons...

Magiaus
Aug 25th, 2004, 10:19 AM
It helps to declare your Control outside the method that loads it......

Magiaus
Aug 27th, 2004, 02:48 PM
I had another glitch where it wasn't working after the first postback so I did what you suggested and set an id. It fixed everything.

thanks