By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,929 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,929 IT Pros & Developers. It's quick & easy.

Help with user control

P: n/a
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).AuditLo g(), "History");
hisTest.DataAddHistoryTable(new
CivicWeb.eAgenda.Package.PackageManager(20).AuditL og(), "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.
Jan 25 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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)

"Ryan Ternier" <rt******@icompasstech.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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).AuditLo g(), "History");
hisTest.DataAddHistoryTable(new
CivicWeb.eAgenda.Package.PackageManager(20).AuditL og(), "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.

Jan 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.