Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ParseControl (ASP.NET - C#) 1

Status
Not open for further replies.

clsimeone

Programmer
Oct 10, 2003
4
US
Hello,

I'm posting this here because I could not find an ASP.NET forum as well as it involves C#.

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:

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(&quot;<asp:button text='Click here!'
runat='server' />&quot;);
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 &quot;oncommand&quot; 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(&quot;<asp:button id='Button3'
text='Btn3'
oncommand='OnButton'
commandname='Btn'
commandargument='b3'
runat='server' />&quot;);

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

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

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

...

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

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

<html>
<body>
<form id=&quot;Form1&quot; runat=&quot;server&quot; >
<h3>PlaceHolder Example</h3>

<p><asplaceholder id=&quot;myPlaceHolder&quot; runat=&quot;server&quot; /></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=&quot;Label1&quot; runat=&quot;server&quot;>Label</asp:Label></p>
</form>
</body>
</html>


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

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

<h3>PlaceHolder Example</h3>

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

<input type=&quot;submit&quot; name=&quot;Button0&quot; value=&quot;Btn0&quot; id=&quot;Button0&quot; />
<input type=&quot;submit&quot; name=&quot;Button1&quot; value=&quot;Btn1&quot; id=&quot;Button1&quot; />

<p><span id=&quot;Label1&quot;>Label</span></p>
</form>
</body>
</html>
 
Try this:
Code:
private void Page_Load(object sender, System.EventArgs e)
{
 Button c3 = new Button();
 c3.Text	= &quot;Btn3&quot;;
 c3.CommandName = &quot;Btn&quot;;
 c3.CommandArgument = &quot;b3&quot;;
 c3.Command += new CommandEventHandler(OnButton);

 Button c4 = new Button();
 c4.Text = &quot;Btn4&quot;;
 c4.CommandName	= &quot;Btn&quot;;
 c4.CommandArgument = &quot;b4&quot;;
 c4.Command += new CommandEventHandler(OnButton);
						
 myPlaceHolder.Controls.Add(c3);
 myPlaceHolder.Controls.Add(c4);
}

private void OnButton(Object Sender, CommandEventArgs  e)
{
 switch (e.CommandArgument.ToString().ToLower())
 {
   case &quot;b3&quot;:
    Label1.Text = &quot;Button 3&quot;; 
    break;
   case &quot;b4&quot;:
    Label1.Text = &quot;Button 4&quot;;
    break;
 }
}
You can create a method that will return a Button control and just pass parameters to it(such as Text, CommandArgument , etc.), in order not to repeat the same code when creating Button controls.
 
LV,

Thanks for the reply. Your suggestion is very helpful to know. However, the reason I need to use ParseControl() is to dynamically create the controls with XML data and an XSLT template.

Before getting the XSLT template to work I need to get the ParseControl() method working.

I'm going to further review your suggestion and see if I can use it to solve my problem. Do you have any other possible ideas taking into account my need to use XSLTs?

Thanks Again,
Chris
 
So the ParseControl method will take a parced XML string, not a hardcoded one like in your sample. Maked sense now. Unfortunately, I don't have much of experience with XSLT, we use XML all over but primarely as data sources. Sorry for not being helpful with this one.
 
Well, thanks for responding.

If you come up with any idea's why ParseControl() does not work in the sample above, I would appreciate any input. If I can get it to work in the C# code, it should work from the XML.

If I find an answer I will update this post.

Thanks Again!
Chris
 
I'll give it a shot and let you know.
 
OK, I think I got it. No changes in the HTML, some in the code behind:
Code:
private void Page_Load(object sender, System.EventArgs e)
{
 Control c3 = ParseControl(&quot;<asp:button id='Button3' text='Btn3' oncommand='OnButton' commandname='Btn' commandargument='b3' runat='server' />&quot;); 
 Control c4 = ParseControl(&quot;<asp:button id='Button4' text='Btn4' oncommand='OnButton' commandname='Btn' commandargument='b4' runat='server' />&quot;); 
			
 myPlaceHolder.Controls.Add(c3); 
 myPlaceHolder.Controls.Add(c4); 

 ((Button)Page.FindControl(&quot;Button3&quot;)).Command += new CommandEventHandler(OnButton);			
 ((Button)Page.FindControl(&quot;Button4&quot;)).Command += new CommandEventHandler(OnButton);			
}

public void OnButton(Object Sender, CommandEventArgs e)
{
 switch(e.CommandArgument.ToString().ToLower())
 {
   case &quot;b0&quot;:
    Label1.Text = &quot;Button 0&quot;; 
    break;
   case &quot;b1&quot;:
    Label1.Text = &quot;Button 1&quot;;
    break;
   case &quot;b3&quot;:
    Label1.Text = &quot;Button 3&quot;;
    break;
   case &quot;b4&quot;:
    Label1.Text = &quot;Button 4&quot;;
    break;
 }
}
 
LV,

What can I say except totally awesome. Thanks so much.

I look forward to seeing how this works with my XML/XSLT based menu.

Thanks Again,
Chris



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top