.NET ParseControl() method problem

C

Chris Simeone

Hello,

I'm having problems with the ParseControl() method creating unexpected
output which I think is preventing events from firing.

My code is based on this MSDN article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwebuitemplatecontrolclassparsecontrolto
pic.asp

The C# section of this simple example shows using ParseControl with an
asp:button as follows:

void Page_Load(object sender, System.EventArgs e)
{
Control c = ParseControl("<asp:button text='Click here!'
runat='server' />");
myPlaceholder.Controls.Add(c);
}

This works for me until I add additional parameters.

Below is my C# code, the ASPX file and the rendered HTML. When looking
at the rendered HTML notice the difference between the controls added
to the aspx file (Button0 and Button1) and the html rendered by the
ParseControl method (Button3 and Button4).

Notice the "oncommand" parameter on the html rendered by ParseControl
(Button3 and Button4). I think this is what breaks my code.

When clicking on Button0 or Button1 an event is fired. But, the
buttons rendered by ParseControl do not fire any events.

Am I doing something wrong? Is this a bug? Any help would be greatly
appreciated.

Thanks,
Chris S.


========
C# Code
========

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

....

public class ParseControl : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.PlaceHolder myPlaceHolder;

void Page_Load(object sender, System.EventArgs e)
{
Control c3 = ParseControl("<asp:button id='Button3'
text='Btn3'
oncommand='OnButton'
commandname='Btn'
commandargument='b3'
runat='server' />");

Control c4 = ParseControl("<asp:button id='Button4'
text='Btn4'
oncommand='OnButton'
commandname='Btn'
commandargument='b4'
runat='server' />");

myPlaceHolder.Controls.Add(c3);
myPlaceHolder.Controls.Add(c4);
}

public void OnButton(Object Sender, CommandEventArgs e)
{
switch (e.CommandArgument.ToString().ToLower())
{
case "b0":
Label1.Text = "Button 0";
break;
case "b1":
Label1.Text = "Button 1";
break;
};
}
}

....

=========
ASPX File
=========

<%@ Page Language="c#" CodeBehind="ParseControl.aspx.cs"
AutoEventWireup="false" Inherits="Learn.ParseControl" %>

<html>
<body>
<form id="Form1" runat="server" >
<h3>PlaceHolder Example</h3>

<p><asplaceholder id="myPlaceHolder" runat="server" /></p>

<asp:button id='Button0'
text='Btn0'
oncommand='OnButton'
commandname='Btn'
commandargument='b0'
runat='server' />

<asp:button id='Button1'
text='Btn1'
oncommand='OnButton'
commandname='Btn'
commandargument='b1'
runat='server' />

<p><asp:Label id="Label1" runat="server">Label</asp:Label></p>
</form>
</body>
</html>


============
Rendered HTML
============

<html>
<body>
<form name="Form1" method="post" action="ParseControl.aspx"
id="Form1">
<input type="hidden" name="__VIEWSTATE"
value="dDwxMDM1NjY1MjU7Oz4+z8z1xQCw84hCCjmMrZwjvAz43A==" />

<h3>PlaceHolder Example</h3>

<p>
<input type="submit" name="Button3" value="Btn3" id="Button3"
oncommand="OnButton" />
<input type="submit" name="Button4" value="Btn4" id="Button4"
oncommand="OnButton" />
</p>

<input type="submit" name="Button0" value="Btn0" id="Button0" />
<input type="submit" name="Button1" value="Btn1" id="Button1" />

<p><span id="Label1">Label</span></p>
</form>
</body>
</html>
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top