Itemplate sample : Access controls on postback

Discussion in 'ASP .Net Web Controls' started by Luhar Powell via .NET 247, Apr 1, 2005.

  1. How to access other controls values in ITemplate?
    Hi,

    I have simple Itemplate implementation with 2 textboxes and button.
    On postback I would like to access values entered by user in the textbox controls.

    The Itemplate sample below has two text boxes for first name and last name. There is Ok button that will do process first name and last name entered by the user.

    Thanks,
    Luhar



    <%@ Page %>
    <%@ Register TagPrefix="TemplateControlSamples" Namespace="TemplateControlSamples" Assembly="TemplateControlSamples" %>
    <script runat=server language=c#>
    void Page_Load()
    {
    DataBind();
    }
    public void ButtonOK_Click(object sender, System.EventArgs e)
    {
    Response.Write("Button Ok clicked");
    //Call some function with firstname and last name entered by user?
    //How do i get access to first name and last name?
    }
    </script>

    <html>

    <body>

    <form method="POST" runat="server">

    Non Templated version:

    <TemplateControlSamples:Template1 Message="Hello World!" runat=server/>

    <hr>

    Templated version:<br>

    <TemplateControlSamples:Template1 Message="Hello World!" runat=server>
    <MessageTemplate>
    <b><i><u>
    <%# Container.Message %>
    <asp:textbox id=TextBoxFirstName runat="server" NAME="Textbox1"></asp:TextBox><br>
    <asp:textbox id=TextBoxLastName runat="server" NAME="Textbox2"></asp:TextBox><br>
    <asp:button id=ButtonOK onclick=ButtonOk_Click Text="Ok" Runat="server"></asp:Button>
    </u></i></b>
    </MessageTemplate>
    </TemplateControlSamples:Template1>

    </form>

    </body>

    </html>

    /*******************************************************/
    using System;
    using System.Web;
    using System.Web.UI;

    namespace TemplateControlSamples
    {

    public class TemplateItem : Control, INamingContainer
    {
    private String _message = null;

    public TemplateItem(String message)
    {
    _message = message;
    }

    public String Message
    {

    get
    {
    return _message;
    }
    set
    {
    _message = value;
    }
    }
    }

    [
    ParseChildren(true)
    ]
    public class Template1 : Control, INamingContainer
    {

    private ITemplate _messageTemplate = null;
    private String _message = null;

    public String Message
    {

    get
    {
    return _message;
    }
    set
    {
    _message = value;
    }
    }

    [
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(TemplateItem))
    ]
    public ITemplate MessageTemplate
    {
    get
    {
    return _messageTemplate;
    }
    set
    {
    _messageTemplate = value;
    }
    }

    public override void DataBind()
    {
    EnsureChildControls();
    base.DataBind();
    }


    protected override void CreateChildControls()
    {

    // If a template has been specified, use it to create children.
    // Otherwise, create a single literalcontrol with message value

    if (MessageTemplate != null)
    {
    Controls.Clear();
    TemplateItem i = new TemplateItem(this.Message);
    MessageTemplate.InstantiateIn(i);

    Controls.Add(i);
    }
    else
    {
    this.Controls.Add(new LiteralControl(this.Message));
    }
    }
    }
    }



    --------------------------------
    From: Luhar Powell

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>74yo8e5ODEqJcNxJ8aYlSQ==</Id>
     
    Luhar Powell via .NET 247, Apr 1, 2005
    #1
    1. Advertising

  2. Luhar Powell via .NET 247

    Brock Allen Guest

    Use YourTemplateControl.FindControl("Textbox1") or if your template is a
    data binding control then you'd need to access the row in your template control
    then call FindControl("Textbox1") on that.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > How to access other controls values in ITemplate? Hi,
    >
    > I have simple Itemplate implementation with 2 textboxes and button. On
    > postback I would like to access values entered by user in the textbox
    > controls.
    >
    > The Itemplate sample below has two text boxes for first name and last
    > name. There is Ok button that will do process first name and last name
    > entered by the user.
    >
    > Thanks, Luhar
    >
    > <%@ Page %>
    > <%@ Register TagPrefix="TemplateControlSamples"
    > Namespace="TemplateControlSamples" Assembly="TemplateControlSamples"
    > %>
    > <script runat=server language=c#>
    > void Page_Load()
    > {
    > DataBind();
    > }
    > public void ButtonOK_Click(object sender, System.EventArgs e)
    > {
    > Response.Write("Button Ok clicked");
    > //Call some function with firstname and last name entered by user?
    > //How do i get access to first name and last name?
    > }
    > </script>
    > <html>
    >
    > <body>
    >
    > <form method="POST" runat="server">
    >
    > Non Templated version:
    >
    > <TemplateControlSamples:Template1 Message="Hello World!"
    > runat=server/>
    >
    > <hr>
    >
    > Templated version:<br>
    >
    > <TemplateControlSamples:Template1 Message="Hello World!"
    > runat=server>
    > <MessageTemplate>
    > <b><i><u>
    > <%# Container.Message %>
    > <asp:textbox id=TextBoxFirstName runat="server"
    > NAME="Textbox1"></asp:TextBox><br>
    > <asp:textbox id=TextBoxLastName runat="server"
    > NAME="Textbox2"></asp:TextBox><br>
    > <asp:button id=ButtonOK onclick=ButtonOk_Click
    > Text="Ok" Runat="server"></asp:Button>
    > </u></i></b>
    > </MessageTemplate>
    > </TemplateControlSamples:Template1>
    > </form>
    >
    > </body>
    >
    > </html>
    >
    > /*******************************************************/ using
    > System; using System.Web; using System.Web.UI;
    >
    > namespace TemplateControlSamples {
    >
    > public class TemplateItem : Control, INamingContainer { private String
    > _message = null;
    >
    > public TemplateItem(String message) { _message = message; }
    >
    > public String Message {
    >
    > get { return _message; } set { _message = value; } } }
    >
    > [ ParseChildren(true) ] public class Template1 : Control,
    > INamingContainer {
    >
    > private ITemplate _messageTemplate = null; private String
    > _message = null;
    >
    > public String Message {
    >
    > get { return _message; } set { _message = value; } }
    >
    > [ PersistenceMode(PersistenceMode.InnerProperty),
    > TemplateContainer(typeof(TemplateItem)) ] public ITemplate
    > MessageTemplate { get { return _messageTemplate; } set {
    > _messageTemplate = value; } }
    >
    > public override void DataBind() { EnsureChildControls();
    > base.DataBind(); }
    >
    > protected override void CreateChildControls() {
    >
    > // If a template has been specified, use it to create children. //
    > Otherwise, create a single literalcontrol with message value
    >
    > if (MessageTemplate != null) { Controls.Clear(); TemplateItem i = new
    > TemplateItem(this.Message); MessageTemplate.InstantiateIn(i);
    >
    > Controls.Add(i); } else { this.Controls.Add(new
    > LiteralControl(this.Message)); } } } }
    >
    > --------------------------------
    > From: Luhar Powell
    > -----------------------
    > Posted by a user from .NET 247 (http://www.dotnet247.com/)
    > <Id>74yo8e5ODEqJcNxJ8aYlSQ==</Id>
    >
     
    Brock Allen, Apr 1, 2005
    #2
    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. cosine... zero
    Replies:
    0
    Views:
    675
    cosine... zero
    Jun 9, 2004
  2. Luhar Powell via .NET 247
    Replies:
    0
    Views:
    2,459
    Luhar Powell via .NET 247
    Apr 1, 2005
  3. John
    Replies:
    0
    Views:
    468
  4. Luhar Powell via .NET 247

    Newbie:Access custom Itemplate(not datagrid/repeater/datalist) control values on postback

    Luhar Powell via .NET 247, Apr 1, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    223
    Luhar Powell via .NET 247
    Apr 1, 2005
  5. Luhar Powell via .NET 247
    Replies:
    0
    Views:
    173
    Luhar Powell via .NET 247
    Apr 1, 2005
Loading...

Share This Page