Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Panel from separate class

Status
Not open for further replies.

JLizOB

Programmer
Dec 5, 2007
45
CA
That references another class for various reasons. ONe of the reasons is to create and add an image button to the .aspx page. I then created a onclick event for the image button. That event is located in the separate class. In that event I want to reference a panel that is in my .aspx page. The only problem is that the class cannot see/find the panel that is located on my .aspx page. How do I make this happen?
 
You might try looking at the System.Reflection namespace. You'd also need to pass your class a reference to your form somehow.

But I really don't know that you want to do this, your class should not really need to be aware of your form/web page/etc.

Maybe you could have the class extend the image button class, and just add it to your form directly? Or better yet, make your panel a UserControl that contains all the logic and is reusable?

If you post more about what your trying to do (and maybe the code you have so far) I'm sure someone will be able to help you find a better way to do this.

Good Luck,

Alex

[small]----signature below----[/small]
I'm pushing an elephant up the stairs

My Crummy Web Page
 
I ended up adding an EventHandler to transfer control back to the .aspx.cs page with the code below.

Here is the code I added to the separate class
Code:
[green]//these were at the class level[/green]
public delegate void ImageButtonClickEventHandler(object WorkplanTable, ImageButtonClickEventArgs btnClick);

public event ImageButtonClickEventHandler ImageButtonClick;

protected void ImageButtonDelete_OnClick(object sender, CommandEventArgs e)
{
ImageButtonClickEventArgs imgclick = new ImageButtonClickEventArgs(e.CommandName, e.CommandArgument.ToString());
OnImageButtonClick(this, imgclick);
}


protected void OnImageButtonClick(object WorkplanTable, ImageButtonClickEventArgs btnClick)
{
//Check for Subscribers
if (ImageButtonClick != null)
{
    ImageButtonClick(WorkplanTable, btnClick);
}
}

public class ImageButtonClickEventArgs : EventArgs
{
    private string _projid = "";
    private string _towid = "";

    public string ProjectID
    {
        get { return _projid; }
    }

    public string TypeofWorkID
    {
        get { return _towid; }
    }
    public ImageButtonClickEventArgs(string projid, string towid)
    {
        _projid = projid;
        _towid = towid;
    }
}

This was added to my .aspx.cs page.
Code:
[green]//This was in the page load event[/green]
wp = new WorkplanTable(EmpID, TOID, ConfigurationManager.ConnectionStrings["DbConnectionString"].ConnectionString, StartDt);

wp.ImageButtonClick += new WorkplanTable.ImageButtonClickEventHandler(wp_ImageButtonClickDelete);


void wp_ImageButtonClickDelete(object workplantable, WorkplanTable.ImageButtonClickEventArgs imgClick)
{
    string projid = imgClick.ProjectID;
    string toid = imgClick.TypeofWorkID;

    MainPanel.Controls.Add(wp.CreateMasterTable());
}



But now I am having a problem attaching a Command event to my image button. below is the code I am trying, but can't seem to get it to work. Any suggestions?
Code:
ImageButton btn = new ImageButton();

    btn.ID = projid + towid;
    btn.Command += new CommandEventHandler(ImageButtonDelete_OnClick);
    btn.CommandName = projid;
    btn.CommandArgument = towid;
    btn.ImageUrl = "~/images/ico_delete.gif";
 
I fixed this problem too. It has something to do with the postback. The portion that I was creating the button in was only being created if the page was not postback so I took it out of the if(!isPostBack) check and it works fine now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top