.NET ParseControl() method problem

Discussion in 'ASP .Net' started by Chris Simeone, Dec 10, 2003.

  1. 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>
    Chris Simeone, Dec 10, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Lohrer

    Page.ParseControl drives me crazy

    Matthias Lohrer, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,724
    Matthias Lohrer
    Jul 8, 2003
  2. Matthias Lohrer

    Using Validation Controls with Page.ParseControl

    Matthias Lohrer, Jul 29, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,577
    Satish Appasani
    Jul 29, 2003
  3. Jeremy McPeak

    ParseControl()

    Jeremy McPeak, Dec 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,901
    Rick Strahl [MVP]
    Dec 21, 2003
  4. Chris Simeone

    Is this a ParseControl bug?

    Chris Simeone, Dec 22, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    633
    Patrice Scribe
    Dec 23, 2003
  5. Francois Beauchemin

    Design-time problem with ParseControl ...

    Francois Beauchemin, Aug 1, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    144
    Francois Beauchemin
    Aug 4, 2003
Loading...

Share This Page