Help with user control

R

Ryan Ternier

I'm playing around with a user control that I think might help one of my
projects.

I'm having an issue with it retaining the controls inside a placeholder that
are added dynamically.
Here's the code:

Calling code from the page that contains the control:
----

protected void btnShowHistory_Click(object sender, EventArgs e)
{
hisTest.DataAddHistoryTable(new
CivicWeb.eAgenda.Package.PackageManager(1).AuditLog(), "History");
hisTest.DataAddHistoryTable(new
CivicWeb.eAgenda.Package.PackageManager(20).AuditLog(), "Test");
}

---

The control layout is:

<Place holder>
<DataGrid>
-----

Here's the control Code:

public void DataAddHistoryTable(DataTable History,string Name)
{
if (dsHistory == null)
dsHistory = new DataSet();

dsHistory.Tables.Add(History);
dsHistory.Tables[dsHistory.Tables.Count - 1].TableName = Name;

Button btnHistory = new Button();
btnHistory.Text = Name;
btnHistory.Click += new EventHandler(btnHistory_Click);
btnHistory.ID = "btnHistory" +
dsHistory.Tables.Count.ToString();

phHistoryButtons.Controls.Add(btnHistory);
}



protected void btnHistory_Click(object sender, EventArgs e)
{
//Find the DT by the name of the button

dgdHistory.DataSource = dsHistory.Tables[((Button)sender).Text];
dgdHistory.DataBind();
}

The control will render the 2 buttons. However, when I press either button,
they both disapear, and nothing happens, it won't hit the History_click
event... but it will hit the on_load on the history control but have no idea
what the sender was. It was like the Buttons just submited as if they were
HTML buttons.

Now on my driver page, I have it set so it only adds 2 datatables on the
first load. If I took that out, it would work, but that is very ineficient.

What I want is a user to add DataTables to the control. With each DataTable
a new button will appear, which the user will then select to render that
code in the DataGrid.

ANy help would be awesome. I'm still new to creating controls, so forgive me
if I'm missing some small detail.

Thanks.
 
B

Bruce Barker

if you create a dynamic control, it must be recreated on postback before
onload, say in onint. you need to remember it was created, usually you store
an indicator in viewstate.

-- bruce (sqlwork.com)
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top