Conditional dynamic loading of a web control?

H

haile

Anyone know how to load a button and its event handler to a web page as a
result of a user action? This can't be done from Page_Load( ), because the
Page_Load event fires before the user action can set a property. And yet, if
the event handler is added at any time after Page_Load, the button doesn't
respond to events. Example:

protected void btnUserClicksMe(o,e)
{
// User has just clicked me, instructing a dynamic button to appear on the
page.
Button btnDynamic = new Button();
btnDynamic += new System.EventHandler(btnDynamic_Click);
this.Controls.Add(btnDynamic); // In a practical application this would
be in a table cell or panel.
}

protected void btnDynamic_Click(o,e)
{
// This code should run if user clicks on the dynamically created button.
// But if event handler is declared at any time after Page_Load, this code
never runs,
// yet no compiler errors or exceptions are thrown.
Response.Write("Hi there!");
}

I have been attempting various workarounds to this behavior, but the only
methods that have worked have involved client-side code that forces a
multiple round trip.

Has anyone found a way to make this work?

Haile
 
H

haile

I am just keeping the example simple. Practical example: The web page has a
textbox that can take filter values, and the contents of the textbox
determine how many rows appear in an ASP:TABLE. That table can contain
several buttons per row. There is no way to know in advance how many rows
are to appear in the table, so the controls in the table can't be created in
advance.

I know the canned datagrid control handles this sort of scenario, but again,
this is just an example.

Thanks for taking an interest in my question.

Haile
 
H

haile

Leon:

Thanks very much for your reply.

You have confirmed my suspicions, because I have been beating myself up on
this off and on for the past couple months.

I am currently using your method. At the moment I am working on a datagrid
with buttons called "Assign," "Lockout," "Edit," and "Refresh" on each row.
If I need a postback to the same viewstate, I use buttons. If I need a
redirect, I use linkbuttons. So it sounds like we're doing the same thing.

Sure would be nice to just make a table in a pinch, though. Or for that
matter, any control at all. Let's say I want to load controls on the basis
of a state saved in a database (say, for a web form designer application).
Datagrids are no help there.

If I find a way to do it, I will post it to the group. Though the prospects
are dim...

Haile




Leon Friesema said:
I am just keeping the example simple. Practical example: The web page has a
textbox that can take filter values, and the contents of the textbox
determine how many rows appear in an ASP:TABLE. That table can contain
several buttons per row. There is no way to know in advance how many rows
are to appear in the table, so the controls in the table can't be created in
advance.

I know the canned datagrid control handles this sort of scenario, but again,
this is just an example.

Thanks for taking an interest in my question.

Haile

What you want to do is hard (not even sure it will ever work) and
therefor I'd advise for the easy approach.
I've built something simular with a datagrid. I added the datagrid and
added buttons to the rows (even to be seen in design-time), the only
thing that changed when loading was the command argument for each
button. In the ItemCommand for the DataGrid even these button-events
get posted, there I check for something like
if MSG_[Id]. substring(0,3) == "MSG" then do this with that Id or even
use some column from the sender-row.
You could actually create all sorts of buttons onto the rows of the
datagrid, each command gets posted to ItemCommand eventhandler.

An little, not-exactly-what-you-want-but-comparable, example:

In the datagrid:
<asp:TemplateColumn HeaderText="Edit">
<ItemStyle Width="60px"></ItemStyle>
<ItemTemplate>
<asp:ImageButton id="ImageButton1"
CausesValidation="False" CommandName="Edit" AlternateText="Edit"

ImageUrl="../images/admin/icon-pencil.gif"
Runat="server"></asp:ImageButton><IMG src="../images/spacer.gif"
width="3">
<asp:ImageButton id="Imagebutton2"
CausesValidation="False" CommandName="Delete" AlternateText="Delete"

ImageUrl="../images/admin/icon-delete.gif"
Runat="server"></asp:ImageButton>
<asp:ImageButton id="Imagebutton6"
CausesValidation="False" CommandName="SortUp" AlternateText="Down"

ImageUrl="../images/admin/icon-down.gif"
Runat="server"></asp:ImageButton>
<asp:ImageButton id="Imagebutton7"
CausesValidation="False" CommandName="SortDown" AlternateText="Up"

ImageUrl="../images/admin/icon-up.gif"
Runat="server"></asp:ImageButton>
</ItemTemplate>


In the code-behind:
private void dgNewsItems_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
switch (e.CommandName)
{
case "SortUp":
{

SortIndex(Convert.ToInt32(((Label)e.Item.Cells[0].Controls[1]).Text),1);
BindData(true);
break;
}
case "SortDown":
{

SortIndex(Convert.ToInt32(((Label)e.Item.Cells[0].Controls[1]).Text),-1);
BindData(true);
break;
}
case ...

The Imagebuttons 6 and 7 contain the commands "SortUp" and "SortDown",
these will be handled by the ItemCommand using function SortIndex(Id,
SortChange)

Hope this helps,
Leon.
 

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
474,312
Messages
2,571,621
Members
48,435
Latest member
EHBEmilio5

Latest Threads

Top