repost: custom control client values gone with postback -- no solution found...

Discussion in 'ASP .Net Web Controls' started by Pipo, Nov 27, 2005.

  1. Pipo

    Pipo Guest

    Nobody knows how to get the values provided in the client can be read in the
    user-control?
    If have made a Web Custom Control with 2 textboxes and 1 dropdownlist.
    The user fills in my control (the textboxes and the dropdownlist) and lots
    more stuff on the page.
    When the user wants to save the page he'll click the save button.
    The server gets the postback but I can read out the filled in controls (in
    my control).
    The textboxes text = "" and the dropdown.selectedindex = -1.
    What do I forget/do wrong....many thanks in advance!!!
    VS2005 Beta:

    //Class wich containts the user controls:

    using System;
    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Drawing;

    namespace cusControls

    {

    [ToolboxBitmap(typeof(TextBox))]

    [DefaultProperty("Text")]

    [ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>")]

    public class Textbox : TextBox

    {

    public enum TextBoxStyle

    {

    Text = 0,

    Numeric = 1

    }



    [Bindable(true)]

    [Category("Behavior")]

    public TextBoxStyle StyleMode

    {

    get

    {

    if (ViewState["cusMode"] == null)

    return TextBoxStyle.Text;

    else

    return (TextBoxStyle)ViewState["cusMode"];

    }

    set

    {

    ViewState["cusMode"] = value;

    }

    }

    protected override void OnPreRender(EventArgs e)

    {

    if (StyleMode == TextBoxStyle.Numeric)

    {

    if (!this.Page.ClientScript.IsClientScriptBlockRegistered(

    "ValidateNumericScript"))

    this.Page.ClientScript.RegisterClientScriptBlock(typeof(string),

    "ValidateNumericScript",

    "<script language=javascript>" +

    "function ValidateNumeric(){" +

    "var keyCode = window.event.keyCode;" +

    "if (keyCode > 57 || keyCode < 48)" +

    "window.event.returnValue = false;}" +

    "</script>");

    Attributes.Add("onKeyPress", "ValidateNumeric()");

    }

    base.OnPreRender(e);

    }

    public override string Text

    {

    get { return (base.Text); }

    set

    {

    if (StyleMode == TextBoxStyle.Numeric)

    {

    try

    {

    base.Text = Convert.ToInt32(value).ToString();

    }

    catch { };

    }

    else

    {

    base.Text = value;

    }

    }

    }

    }

    [Designer(typeof(DatePickerDesigner))]

    [ToolboxBitmap(typeof(Calendar))]

    [ToolboxData("<{0}:DatePicker runat=server></{0}:DatePicker>")]

    public class DatePicker : WebControl

    {

    public enum FormatStyle

    {

    DMY = 0,

    YMD = 1,

    MDY = 2

    }

    [Bindable(true)]

    [Category("Appearance")]

    public FormatStyle Format

    {

    get

    {

    if (ViewState["cusFormat"] == null)

    return FormatStyle.DMY;

    else

    return (FormatStyle)ViewState["cusFormat"];

    }

    set

    {

    ViewState["cusFormat"] = value;

    }

    }

    public string SelectedDate

    {

    get

    {

    // How to retrieve the values of the Textboxes txtDay and txtYear and the
    selected index of the dropdowlist ddlMonth?

    //Controls[x] will get an nullreference....Controls.Count gives 0 controls
    back after postback.

    }

    set

    {

    //Not really needed cos the client provides these values(???)

    }

    }



    protected override void CreateChildControls()

    {

    Textbox txtDay = new Textbox();

    DropDownList ddlMonths = new DropDownList();

    Textbox txtYear = new Textbox();

    txtDay.StyleMode = Textbox.TextBoxStyle.Numeric;

    txtDay.MaxLength = 2;

    txtDay.Width = 15;

    ddlMonths.Items.Add("January");

    ddlMonths.Items.Add("February");

    ddlMonths.Items.Add("March");

    ddlMonths.Items.Add("April");

    ddlMonths.Items.Add("May");

    ddlMonths.Items.Add("Jun");

    ddlMonths.Items.Add("July");

    ddlMonths.Items.Add("August");

    ddlMonths.Items.Add("September");

    ddlMonths.Items.Add("October");

    ddlMonths.Items.Add("November");

    ddlMonths.Items.Add("December");

    ddlMonths.SelectedIndex = (DateTime.Now.Month) - 1;

    txtYear.StyleMode = Textbox.TextBoxStyle.Numeric;

    txtYear.Width = 30;

    switch (Format)

    {

    case FormatStyle.MDY:

    Controls.Add(ddlMonths);

    Controls.Add(txtDay);

    Controls.Add(txtYear);

    break;

    case FormatStyle.YMD:

    Controls.Add(txtYear);

    Controls.Add(ddlMonths);

    Controls.Add(txtDay);

    break;

    default:

    case FormatStyle.DMY:

    Controls.Add(txtDay);

    Controls.Add(ddlMonths);

    Controls.Add(txtYear);

    break;

    }

    }

    }

    }

    //Second class the designer:

    using System;

    using System.IO;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.Design;

    namespace cusControls

    {

    public class DatePickerDesigner : System.Web.UI.Design.ControlDesigner

    {

    public override string GetDesignTimeHtml()

    {

    try

    {

    DatePicker ctl = (DatePicker)Component;

    StringWriter sw = new StringWriter();

    HtmlTextWriter tw = new HtmlTextWriter(sw);

    TextBox txtDay = new TextBox();

    txtDay.Width = 15;

    DropDownList ddl = new DropDownList();

    ddl.Items.Add("January");

    ddl.Width = ctl.Width;

    TextBox txtYear = new TextBox();

    txtYear.Width = 30;

    switch (ctl.Format)

    {

    case DatePicker.FormatStyle.MDY:

    ddl.RenderControl(tw);

    txtDay.RenderControl(tw);

    txtYear.RenderControl(tw);

    break;

    case DatePicker.FormatStyle.YMD:

    txtYear.RenderControl(tw);

    txtDay.RenderControl(tw);

    ddl.RenderControl(tw);

    break;

    default:

    case DatePicker.FormatStyle.DMY:

    txtDay.RenderControl(tw);

    ddl.RenderControl(tw);

    txtYear.RenderControl(tw);

    break;

    }

    return sw.ToString();

    }

    catch (Exception ex)

    {

    return ex.Message;

    }

    }

    }

    }
    Pipo, Nov 27, 2005
    #1
    1. Advertising

  2. Pipo

    Pipo Guest

    I have found it myself.....
    Problem solved.

    "Pipo" <> schreef in bericht
    news:...
    > Nobody knows how to get the values provided in the client can be read in
    > the user-control?
    > If have made a Web Custom Control with 2 textboxes and 1 dropdownlist.
    > The user fills in my control (the textboxes and the dropdownlist) and lots
    > more stuff on the page.
    > When the user wants to save the page he'll click the save button.
    > The server gets the postback but I can read out the filled in controls (in
    > my control).
    > The textboxes text = "" and the dropdown.selectedindex = -1.
    > What do I forget/do wrong....many thanks in advance!!!
    > VS2005 Beta:
    >
    > //Class wich containts the user controls:
    >
    > using System;
    > using System.Collections.Generic;
    >
    > using System.ComponentModel;
    >
    > using System.Web.UI;
    >
    > using System.Web.UI.WebControls;
    >
    > using System.Drawing;
    >
    > namespace cusControls
    >
    > {
    >
    > [ToolboxBitmap(typeof(TextBox))]
    >
    > [DefaultProperty("Text")]
    >
    > [ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>")]
    >
    > public class Textbox : TextBox
    >
    > {
    >
    > public enum TextBoxStyle
    >
    > {
    >
    > Text = 0,
    >
    > Numeric = 1
    >
    > }
    >
    >
    >
    > [Bindable(true)]
    >
    > [Category("Behavior")]
    >
    > public TextBoxStyle StyleMode
    >
    > {
    >
    > get
    >
    > {
    >
    > if (ViewState["cusMode"] == null)
    >
    > return TextBoxStyle.Text;
    >
    > else
    >
    > return (TextBoxStyle)ViewState["cusMode"];
    >
    > }
    >
    > set
    >
    > {
    >
    > ViewState["cusMode"] = value;
    >
    > }
    >
    > }
    >
    > protected override void OnPreRender(EventArgs e)
    >
    > {
    >
    > if (StyleMode == TextBoxStyle.Numeric)
    >
    > {
    >
    > if (!this.Page.ClientScript.IsClientScriptBlockRegistered(
    >
    > "ValidateNumericScript"))
    >
    > this.Page.ClientScript.RegisterClientScriptBlock(typeof(string),
    >
    > "ValidateNumericScript",
    >
    > "<script language=javascript>" +
    >
    > "function ValidateNumeric(){" +
    >
    > "var keyCode = window.event.keyCode;" +
    >
    > "if (keyCode > 57 || keyCode < 48)" +
    >
    > "window.event.returnValue = false;}" +
    >
    > "</script>");
    >
    > Attributes.Add("onKeyPress", "ValidateNumeric()");
    >
    > }
    >
    > base.OnPreRender(e);
    >
    > }
    >
    > public override string Text
    >
    > {
    >
    > get { return (base.Text); }
    >
    > set
    >
    > {
    >
    > if (StyleMode == TextBoxStyle.Numeric)
    >
    > {
    >
    > try
    >
    > {
    >
    > base.Text = Convert.ToInt32(value).ToString();
    >
    > }
    >
    > catch { };
    >
    > }
    >
    > else
    >
    > {
    >
    > base.Text = value;
    >
    > }
    >
    > }
    >
    > }
    >
    > }
    >
    > [Designer(typeof(DatePickerDesigner))]
    >
    > [ToolboxBitmap(typeof(Calendar))]
    >
    > [ToolboxData("<{0}:DatePicker runat=server></{0}:DatePicker>")]
    >
    > public class DatePicker : WebControl
    >
    > {
    >
    > public enum FormatStyle
    >
    > {
    >
    > DMY = 0,
    >
    > YMD = 1,
    >
    > MDY = 2
    >
    > }
    >
    > [Bindable(true)]
    >
    > [Category("Appearance")]
    >
    > public FormatStyle Format
    >
    > {
    >
    > get
    >
    > {
    >
    > if (ViewState["cusFormat"] == null)
    >
    > return FormatStyle.DMY;
    >
    > else
    >
    > return (FormatStyle)ViewState["cusFormat"];
    >
    > }
    >
    > set
    >
    > {
    >
    > ViewState["cusFormat"] = value;
    >
    > }
    >
    > }
    >
    > public string SelectedDate
    >
    > {
    >
    > get
    >
    > {
    >
    > // How to retrieve the values of the Textboxes txtDay and txtYear and the
    > selected index of the dropdowlist ddlMonth?
    >
    > //Controls[x] will get an nullreference....Controls.Count gives 0 controls
    > back after postback.
    >
    > }
    >
    > set
    >
    > {
    >
    > //Not really needed cos the client provides these values(???)
    >
    > }
    >
    > }
    >
    >
    >
    > protected override void CreateChildControls()
    >
    > {
    >
    > Textbox txtDay = new Textbox();
    >
    > DropDownList ddlMonths = new DropDownList();
    >
    > Textbox txtYear = new Textbox();
    >
    > txtDay.StyleMode = Textbox.TextBoxStyle.Numeric;
    >
    > txtDay.MaxLength = 2;
    >
    > txtDay.Width = 15;
    >
    > ddlMonths.Items.Add("January");
    >
    > ddlMonths.Items.Add("February");
    >
    > ddlMonths.Items.Add("March");
    >
    > ddlMonths.Items.Add("April");
    >
    > ddlMonths.Items.Add("May");
    >
    > ddlMonths.Items.Add("Jun");
    >
    > ddlMonths.Items.Add("July");
    >
    > ddlMonths.Items.Add("August");
    >
    > ddlMonths.Items.Add("September");
    >
    > ddlMonths.Items.Add("October");
    >
    > ddlMonths.Items.Add("November");
    >
    > ddlMonths.Items.Add("December");
    >
    > ddlMonths.SelectedIndex = (DateTime.Now.Month) - 1;
    >
    > txtYear.StyleMode = Textbox.TextBoxStyle.Numeric;
    >
    > txtYear.Width = 30;
    >
    > switch (Format)
    >
    > {
    >
    > case FormatStyle.MDY:
    >
    > Controls.Add(ddlMonths);
    >
    > Controls.Add(txtDay);
    >
    > Controls.Add(txtYear);
    >
    > break;
    >
    > case FormatStyle.YMD:
    >
    > Controls.Add(txtYear);
    >
    > Controls.Add(ddlMonths);
    >
    > Controls.Add(txtDay);
    >
    > break;
    >
    > default:
    >
    > case FormatStyle.DMY:
    >
    > Controls.Add(txtDay);
    >
    > Controls.Add(ddlMonths);
    >
    > Controls.Add(txtYear);
    >
    > break;
    >
    > }
    >
    > }
    >
    > }
    >
    > }
    >
    > //Second class the designer:
    >
    > using System;
    >
    > using System.IO;
    >
    > using System.Web;
    >
    > using System.Web.UI;
    >
    > using System.Web.UI.WebControls;
    >
    > using System.Web.UI.Design;
    >
    > namespace cusControls
    >
    > {
    >
    > public class DatePickerDesigner : System.Web.UI.Design.ControlDesigner
    >
    > {
    >
    > public override string GetDesignTimeHtml()
    >
    > {
    >
    > try
    >
    > {
    >
    > DatePicker ctl = (DatePicker)Component;
    >
    > StringWriter sw = new StringWriter();
    >
    > HtmlTextWriter tw = new HtmlTextWriter(sw);
    >
    > TextBox txtDay = new TextBox();
    >
    > txtDay.Width = 15;
    >
    > DropDownList ddl = new DropDownList();
    >
    > ddl.Items.Add("January");
    >
    > ddl.Width = ctl.Width;
    >
    > TextBox txtYear = new TextBox();
    >
    > txtYear.Width = 30;
    >
    > switch (ctl.Format)
    >
    > {
    >
    > case DatePicker.FormatStyle.MDY:
    >
    > ddl.RenderControl(tw);
    >
    > txtDay.RenderControl(tw);
    >
    > txtYear.RenderControl(tw);
    >
    > break;
    >
    > case DatePicker.FormatStyle.YMD:
    >
    > txtYear.RenderControl(tw);
    >
    > txtDay.RenderControl(tw);
    >
    > ddl.RenderControl(tw);
    >
    > break;
    >
    > default:
    >
    > case DatePicker.FormatStyle.DMY:
    >
    > txtDay.RenderControl(tw);
    >
    > ddl.RenderControl(tw);
    >
    > txtYear.RenderControl(tw);
    >
    > break;
    >
    > }
    >
    > return sw.ToString();
    >
    > }
    >
    > catch (Exception ex)
    >
    > {
    >
    > return ex.Message;
    >
    > }
    >
    > }
    >
    > }
    >
    > }
    >
    >
    >
    >
    Pipo, Nov 28, 2005
    #2
    1. Advertising

  3. Pipo

    conset23 Guest

    so share your knowlege.. :)
    conset23, Dec 1, 2005
    #3
    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. Pipo
    Replies:
    3
    Views:
    470
    Edwin Knoppert
    Nov 25, 2005
  2. Pipo
    Replies:
    1
    Views:
    615
    Edwin Knoppert
    Nov 26, 2005
  3. Pipo
    Replies:
    2
    Views:
    554
    conset23
    Dec 1, 2005
  4. Pipo

    custom control client values gone with postback

    Pipo, Nov 25, 2005, in forum: ASP .Net Web Controls
    Replies:
    9
    Views:
    175
  5. Jason
    Replies:
    0
    Views:
    295
    Jason
    Nov 11, 2006
Loading...

Share This Page