how to have both server controls and html controls rendered in a custom control?

I have a custom control in which there is an html text box.when you click on it , a div containing a table is appeared and according to what you select in that table a text is set to the text box.

It works fine but the problem is: as the text box is an html element it doesn't have viewstate and when a post back occures in the page its value disappeares.I tried a hidden field to keep its value.again there is another problem : I put this custom control in a usercontrol and put the usercontrol in a multiview and by clicking on a button this view is selected and by clicking another button another view.and when the views are changed the textbox's value is gone.

do you have any idea to solve this?is there a way to render a asp textbox(as in createChildControl) and at the same time render the html table and its contents?

0
eea61
8/13/2006 4:27:56 PM
📁 asp.net.custom-server-controls
📃 3498 articles.
⭐ 0 followers.

💬 4 Replies
👁️‍🗨️ 1857 Views

Hi,

yes it is. You can render a child control out with its RenderControl method. Essentially render HTML out in Render method of the custom control with the HtmlTextWriter (writer argument) and where you want the child control to render itself, call its RenderControl by passing the HtmlTextWriter in.

However, to give more exact reply, you'd need to post a bit of the code you have so far.


Thanks,

Teemu Keiski
Finland, EU
0
joteke
8/13/2006 5:58:29 PM

I Copied some parts of the code here:The pink line is the part that my textbox is rendered and after then the table in which a datepicker shoud be placed and when you click on textbox this table is visible:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DatePickerControl
{
    [ToolboxData("<{0}:DatePicker runat=server></{0}:DatePicker>")]
    public class DatePicker : WebControl
    {
.

.//properties eliminated.

        protected override void OnPreRender(EventArgs e)
        {
          //register the Hidden field which keeps the value of DateTextBox(viewstate)
          Page.RegisterHiddenField("hiddenDate" + this.UniqueID, this.Page.Request.Form["hiddenDate" + this.UniqueID]);

            if (!this.Page.IsClientScriptBlockRegistered("LoadDate"))
            {
                StringBuilder str = new StringBuilder();
                str.Append("<script type='text/javascript'>");
                str.Append("function loadDate(){}"); //function's body eliminated
                str.Append("</script>   ");

                string script = str.ToString();
                this.Page.RegisterClientScriptBlock("LoadDate", script);
            }

        }

        protected override void Render(HtmlTextWriter writer)
        {
           writer.Write("<input type='text' id='txtDate" + this.UniqueID + "' onClick=\"loadDate();document.getElementById('calDiv" + this.UniqueID + "').style.display='';\" class='" + this.CssClass + "' />");
            writer.Write("<div id='calDiv" + this.UniqueID + "' style='cursor: hand;position: absolute;z-index: 1;display: none;top:" + this.Top + "px ;left:" + this.Left + "px ;'>");
            writer.Write("  <table border='0'id='table1' width='100' dir='ltr' cellspacing='0' cellpadding='0' height='200'>");

//contents of table eliminated
           writer.Write("  </table>");
            writer.Write("</div>");

            writer.Write("<script type=text/javascript>");
            writer.Write("document.getElementById('" + this.UniqueID + "').innerText='" + this.Page.Request.Form["hiddenDate" + this.UniqueID] + "';");
            writer.Write("</script>");
        }
    }
}

 

 

 

 

 

0
eea61
8/13/2006 6:56:10 PM

I don't get all the idea you have here (you have some due to writing all hardcoded because of the TB) but it is pretty much like this

namespace DatePickerControl
{
    [ToolboxData("<{0}:DatePicker runat=server></{0}:DatePicker>")]
    public class DatePicker : WebControl, INamingContainer
    {

        //Added these to make the code compile
        public int Top
        {
            get
            {
                return 0;
            }
            set
            {

            }
        }

        public int Left
        {
            get
            {
                return 0;
            }
            set
            {

            }
        }

        TextBox tb = null;
        protected override void CreateChildControls()
        {
            Controls.Clear();

            tb = new TextBox();
            tb.ID = "dateBox";
            tb.CssClass = this.CssClass;
            Controls.Add(tb);
        }

        protected override void OnPreRender(EventArgs e)
        {
          //register the Hidden field which keeps the value of DateTextBox(viewstate)
         // Page.RegisterHiddenField("hiddenDate" + this.UniqueID, this.Page.Request.Form["hiddenDate" + this.UniqueID]);


            if (!this.Page.IsClientScriptBlockRegistered("LoadDate"))
            {
                StringBuilder str = new StringBuilder();
                str.Append("<script type='text/javascript'>");
                str.Append("function loadDate(){}"); //function's body eliminated
                str.Append("</script>   ");

                string script = str.ToString();
                this.Page.RegisterClientScriptBlock("LoadDate", script);
            }

        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "loadDate();document.getElementById('calDiv" + this.UniqueID + "').style.display=''"); 
            tb.RenderControl(writer);
          
            writer.Write("<div id='calDiv" + this.UniqueID + "' style='cursor: hand;position: absolute;z-index: 1;display: none;top:" + this.Top + "px ;left:" + this.Left + "px ;'>");
            writer.Write("  <table border='0'id='table1' width='100' dir='ltr' cellspacing='0' cellpadding='0' height='200'>");

//contents of table eliminated
           writer.Write("  </table>");
            writer.Write("</div>");

            writer.Write("<script type=text/javascript>");
            writer.Write("document.getElementById('" + this.UniqueID + "').innerText='" + tb.Text  + "';");
            writer.Write("</script>");
        }
    }
}


Thanks,

Teemu Keiski
Finland, EU
0
joteke
8/13/2006 7:23:41 PM
jotekethanks for your response.I tried this and it renderes textbox but the problem of viewstate still remians.although I added these lines it doesn't keep its value:

protected override void CreateChildControls()

{

TextBox txt = new TextBox();

txt.CssClass =

this.CssClass;

txt.EnableViewState =

true;

txt.Attributes.Add(

"onClick", "loadDate2('txtDate" + this.ClientID + "');document.getElementById('calDiv" + this.ClientID + "').style.display='';");

txt.Text =

this.Text;

this.Controls.Add(txt);

ChildControlsCreated =

true;

}

 

Do you have any idea?

0
eea61
8/14/2006 7:07:31 AM
Reply: