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. Follow

4 Replies
1550 Views

Similar Articles

[PageSpeed] 40

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:

Similar Artilces:

Usercontrol vs Custom Server Controls, Any need for Custom Server Controls
Hello I'm new to ASP.Net so this is probably a stupid question but, is there any need to create customer server controls now that you can pre-compile usercontrols into a dll and thus easily share between applications? This blog post from David Ebb describes how to do it and the restrictions he note don't seem that bad (other than being able to embed resources such as images in the dll). http://blogs.msdn.com/davidebb/archive/2005/10/30/487160.aspx His article desribes the techniques for ASP.Net 2.0 and he suggests that there will be further improvements in .Net 3.5 in this ar...

Custom Server Control Vs Web Parts, Any need for Custom Server Control
Hello I've just posted a similar question on why you need server controls when you can pre-compile usercontrols and redistribute them. http://forums.asp.net/p/1397433/3010100.aspx#3010100 I've seen lots of (usercontrol vs web part) but not much on web part vs custom server control. According to the MSDN site you can put web parts onto a normal asp.net page outside of a web part zone and they behave just like custom server controls. So, if you are creating a bunch of controls for redistribution to customer asp.net sites and/or SharePoint sites why don't you just create Web Par...

problems listenening to events on a custom server control from another custom server control
 Hi all,I am having problems listening to events on one of custom server controls(AddBookmark) from another custom server control(BookmarkList). The listener or the BookmarkList control has a property that can be set to the AddBookmark control (string at design but the reference is obtained at run-time). I am able to get a reference to the AddBookmark control and add a listener to it on the BookmarkList control on the OnPrender of BookmarkList. But the problem is that when the events on AddBookmark gets fired, the listener added from the BookmarkList is no longer present and the listene...

Custom Server Control Made of Custom Controls?
I'm giving myself a crash course in custom server controls, and I have a quick general question. Can I have a custom server control that is composed of other server controls I created. I'm pretty sure the answer is yes, but I want to be positive. Yes http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondevelopingcompositecontrols.asp Shows how to combine existing controls into a new control using class composition. -ron Thanks! ASP.NET so rocks! Making page components extensible objects is such a great idea! I love it....

How to render a repeater control in the custom server control?
 How to render a repeater control in the custom server  control because there is a lot of element inside the repeater control? My code is as follows: <div> <asp:Repeater ID="rptFields" runat="server"> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td><%# Eval("Prompt") %>:</td> <td><asp:PlaceHolder ID="plControl" runat="server" /> <input type="...

Custom Server Control to render multiple Controls
I have a need to build a reusable "Custom Server Control" that will in turn render multiple controls at runtime. The four controls I need it to render are as follows  <asp:Label ID="divLogin" runat="server" Text=" | " /> <asp:LoginStatus CssClass="footer" ID="LoginStatus1" runat="server" LogoutPageUrl="Default.aspx" /> <asp:Label ID="divAdmin" runat="server" Text=" | " /> <asp:HyperLink CssClass="footer" ID="btnAdmin" NavigateUrl=&...

Using a server control in the rendering a custom control
Hi. I have a custom control that overrides the Render method to draw itself.  Is it possible to use a server control (asp:Image, wwHoverPanel, etc.) inside such a custom control or would I have to rewrite my custom control as a composite control? I'd like to do something something like:protected override void Render(HtmlTextWriter w) { w.RenderBeginTag(HtmlTextWriterTag.Table); w.RenderBeginTag(HtmlTextWriterTag.Tr) w.RenderBeginTag(HtmlTextWriterTag.Td) // somehow place asp:Label here w.RenderEndTag(); // column w.RenderEndTag(); // row ...

Custom Control within a Control Control
I am developing a custom controls that will render headers for different web sites. Within these controls, I want to place another custom control that will change the language. I created the language control and had it implement IPostBackDataHandler. This control works fine if I drag it on a new web form, but if I tell a header custom control to render it problems occur. The Language Selector custom control does not have a Page member anymore and the event handlers do not work. Does anyone have any experience with doing something similiar? Anyone have any suggestions? Thanks, ...

How to Render controls within an UpdatePanel in a Custom Server Control
Hi,Im writting a CompositeControl that contains a few UpdatePanels (TabContainer with an UpdatePanel on each TabPanel to make Tab control panels load "on-demand"). I want to control the renderring of the controls within the UpdatePanel's ContentTemplateContainer.How should I implement the render method of my CompositeControl?Should I use a custom UpdatePanel (inherit from UpdatePanel and override the RenderContents method)? is there a simpler solution? (because I need a different "render" for each tab panel....)Thanks Tal88 Hi, I want to control the renderring o...

Custom Server Control With Child Control
All, I am currently putting together a very simple control. The new control outputs a table with two rows. The top row contains a heading and the bottom row will contain any other content. The control as it stands works perfectly well and I can place text into the content row no problem. Now, to the problem. Once I'd completed the control I figured it would be neat to enable the developer to add controls directly into the bottom row (content row) of the outputted table. As you probably know the WebControl class has a Controls collection which the developer can use to add any other...

Custom Server Control with Child Controls
I have a custom server control <xx.Parent... /> which parses custom child controls <xx.Child... />. In Visual Studio 2008, IntelliSense shows me both when I start typing my control namespace. How can I prevent the child control from showing up in IntelliSense  unless I'm adding it within the parent tags?Thanks. --Rudy...

html server controls and web server controls
hello what is the difference between html server controls and web server controls? what is the main goal of converting html elements to html server controls , so why i can directly use the web server controls instead of converting process? thank you for the help and for the time If you convert a normal html control to a html server control then you can work with and reference it programmatically. MS advises using web controls where possible. But if your application uses vast amounts of html controls its far easier to just apply runat="server" than to re invent them a...

HTML Server Control or Web Server Control?
Hi, Being new to ASP.NET I have one thing confusing me much. 1. Whats the major difference between HTML Server Control and Web Server Control? 2. Whats the advantage of processing all the HTML Controls in Server? Server will not be overloaded with this? Which one should I use?..Plain HTML or HTML Server Control or Web Server Control? Thanks and Regards _________________ ASP 2 ASP.NET FYI: http://msdn.microsoft.com/msdnmag/issues/01/09/asp/toc.asp Colt Hi Colt, Thanks for the link. I personally prefer server controls - - there are so many more possi...

html control, server control
what is the difference between html control, server control and web controls See this post - http://forums.asp.net/thread/1553414.aspx Though if using a templated control I would not use either unless I need the value of the control on postback. Simple use literals with bound attributes. Rgds, Martin.Rgds,Martin.For the benefit of all users please mark any post answers as appropriate. "A square is a rectangle, but a rectangle is not a square."   A 'Control' is the basis for all ASP controls.  It has certain properties and methods that other, more developed controls c...

Web resources about - how to have both server controls and html controls rendered in a custom control? - asp.net.custom-server-controls

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Backburn approved in bid to control bushfire before blaze hit Wye River - HeraldSun Search Search
A BACKBURN was approved to try to control a bushfire in the days before it tore through more than 100 homes in Wye River on Christmas Day.

WA fires: threatening Jindalee blaze brought under control
A fire that was at one stage a threat to lives on homes in Jindalee on Thursday has been brought under control.

Grassfire near Leneva in Victoria's north-east now under control, fire authorities say
A grassfire burning near Leneva in Victoria's north-east is now under control, fire authorities say.

Publicis Said to Weigh Bid for Control of Cheil Worldwide
Publicis Groupe is weighing a bid for a controlling stake in Cheil Worldwide, people with knowledge of the matter said. The French company is ...

Playing around with BMW’s 7 Series gesture control user interface
We take a BMW 7 Series for a short drive to play with its gesture control. Video shot/edited by Jennifer Hahn. (video link) LAS VEGAS—Yesterday, ...

BMW recalls i8 for stability control problem
... , BMW , Safety , Coupe , Hybrid , Performance BMW will recall a small number of 2015 i8s because of a problem with the dynamic stability control ...

Embracing the NRA on gun control
I’m just stealing a massive post from Stonekettle Station on a plausible path forward towards sanity on guns: Those laws should be designed ...

The most common reasons real drivers seize control of a Google self-driving car
... for the Department of Motor Vehicles that Google just published . Each time, the cars "disengaged" from their autonomous mode, giving control ...

Bulls' Joakim Noah confronts limited role with limited personal control
Drafted in 2007, Joakim Noah is, in year nine, the longest-tenured Bull in terms of consecutive seasons of service, as identifiable with the ...

Resources last updated: 1/14/2016 6:49:58 PM