On Mon, 29 Nov 2004 05:31:05 -0800, "haile"
[quoted text, click to view] <haile@discussions.microsoft.com> wrote:
>
>"Leon Friesema" wrote:
>
>> On Mon, 29 Nov 2004 05:05:04 -0800, "haile"
>> <haile@discussions.microsoft.com> wrote:
>>
>> >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
>>
>> 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.
>>
>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,