Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to find a control that was created through a custom control (dll file)


  • Please log in to reply
No replies to this topic

#1 ManyTimes

ManyTimes

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 31 May 2011 - 03:08 PM

Hello!

I've created a simple custom control

The custom control:
using System.Web.UI;                                //System.Web
using System.Web.UI.WebControls;

[assembly: TagPrefix("CustomControl", "temp")]    //the dll file and its prefix "temp"
namespace CustomControl                                  //which is added in the web.config file
{
    [ToolboxData(@"<{0}:Temp runat=""server"" ID=""temp"" />")]
    public class Temp : CompositeControl
    {
        public Label lbl;
        protected override void CreateChildControls()
        {
            lbl = new Label();
            lbl.Text = "Test of a label's text";
            lbl.ID = "lbl_1";
            this.Controls.Add(lbl);                     //Adds a label to this class
            base.CreateChildControls();                 //Create the control
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (!this.DesignMode)
            {
                lbl = new Label();
                lbl.Text = "Test of a label's text 2!";
                lbl.ID = "innerLBL";
                lbl.RenderControl(writer);
            }
        }
    }
}

My aspx-page:
<body>
    <form id="form1" runat="server">
    <temp:Temp runat="server" ID="testControl" /> <br />
    <asp:Button runat="server" ID="testbtn" Text="test" onclick="testbtn_Click" /><br />
    </form>
</body>

The label renders just fine, and at page load I am able to find the control named "testControl" which is of type "temp:Temp", through this code-behind code (C#):
protected void Page_Load(object sender, EventArgs e)
        {
            Control c = (Control)FindControl("testControl");
            if (c != null)
            {
                lbl1.Text += [COLOR="#2e8b57"]"Control has been found[/COLOR]";
                Temp tt = (Temp)c;
                lbl1.Text += "[COLOR="lime"]No error upon casting control to my Temp-custom-control"[/COLOR]
               [COLOR="red"] tt.lbl.Text = "This text never appears";[/COLOR]
                //tt.lbl.Text does not change, although its normal text from the DLL file,
                //"Test of a label's text 2" shows up every time. How can I get this control
                //to change!
            }
        }

The button click
protected void testbtn_Click(object sender, EventArgs e)
        {

            Control c = (Control)FindControl("testControl");
            if (c != null)
            {
                Temp lbl = (Temp)c;
               [COLOR="red"] lbl.lbl.Text += "BUTTON CLICKED";[/COLOR]
            }
        }

The red lines does not work, they will not change my object from a custom control, or maybe it does change, but it just does not render? Hmmm...

What am I doing wrong?
Some rendering mistake?

Solved: Rendering mistake
base.Render(w);                 //Base render to writer
labelControl.RenderControl(w);    //After send your controls to render to browser

Edited by ManyTimes, 01 June 2011 - 08:11 AM.

  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download