DataBinding with user control as a template

Posted by: Jotekes Blog, on 06 Jul 2006 | View original | Bookmarked: 0 time(s)

This one was originally asked on Forums. You can provide a user control into ItemTemplate property of DataList in code, and use it as a reusable template while the uc still participates in databinding, accerssing the data source. Here is a sample:

ucTemplatesample.aspx
=====================

 <asp:DataList ID="DataList1" runat="server">
        </asp:DataList>

ucTemplatesample.aspx.aspx.cs
=============================
...
protected void Page_Load(object sender, EventArgs e)
    {
        DataList1.ItemTemplate = Page.LoadTemplate("ucTemplate.ascx");
        if(!IsPostBack)
            BindData();
    }

    private void BindData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("something", typeof(String));

        DataRow dr = null;
        dr = dt.NewRow();
        dr["something"] = "text1";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["something"] = "text2";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["something"] = "text3";
        dt.Rows.Add(dr);

        DataList1.DataSource = dt;
        DataList1.DataBind();
    }
...

ucTemplate.ascx
===============

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucTemplate.ascx.cs" Inherits="ucTemplate" %>
<asp:TextBox ID="TextBoxinUC" runat="server" />

ucTemplate.ascx.cs
==================
...
protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);


        //here is more generic version, knows very little about the data source
        object obj = DataBinder.GetPropertyValue(this.NamingContainer, "DataItem");
        TextBoxinUC.Text = DataBinder.Eval(obj,"something").ToString();
       
        //Following assumes that the containing control's item type is known at compile time
        //You need it in case you need to access DataKeys etc
        //DataListItem cont = this.NamingContainer as DataListItem;

        //if (cont != null)
        //{
        //    //If you don't want to know about the data source you can use this
        //    //TextBoxinUC.Text = (string)DataBinder.Eval(cont.DataItem, "something");

        //    //Typed way, tie yo to a dataTable/dataView as data source
        //    DataRowView drw = cont.DataItem as DataRowView;
        //    if (drw != null)
        //        TextBoxinUC.Text = drw["something"].ToString();
        //}
    }
...

 

 

 

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: DataGrid | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 8414 | Hits: 257

Similar Posts

  • Introducing Versatile DataSources more
  • Visual Studio Extensions for RadControls for Silverlight Coming with Q3 2009 Release more
  • MVC or Web Forms? A Dying Question more
  • Choose your preferred data layout with RadListView for ASP.NET AJAX more
  • Announcing Visual Studio 2010 and .NET FX 4 Beta 2 more
  • Adding users to a TFS project when youre not on the domain more
  • Creating a Filtering User Interface With jQuery In a Web Forms Application: Part 1 more
  • Adding additional power to RadGridView for Silverlight with attached behaviors more
  • Sneak Peek: ASP.NET Time Edit Control more
  • Telerik Releases New Controls for Silverlight 3 and WPF more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD