Groups | Blog | Home
all groups > asp.net webcontrols > november 2004 >

asp.net webcontrols : Conditional dynamic loading of a web control?


haile
11/29/2004 5:05:04 AM
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
11/29/2004 5:31:05 AM
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





[quoted text, click to view]
haile
11/29/2004 6:15:03 AM
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




[quoted text, click to view]
Leon Friesema
11/29/2004 2:20:44 PM
On Mon, 29 Nov 2004 05:05:04 -0800, "haile"
[quoted text, click to view]

You want to add a button to the page, run-time? Why not add the
button, Visible = false and set the Visible to true when needed?

Leon Friesema
11/29/2004 3:02:24 PM
On Mon, 29 Nov 2004 05:31:05 -0800, "haile"
[quoted text, click to view]

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,
AddThis Social Bookmark Button