Tuesday, March 8, 2005

How to reference a UserControl from a DataGrid

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/how_to_reference_a_usercontrol_from_a_datagrid.htm]

Sometimes you need to have a UserControl in a DataGrid, and sometime you also need to reference that control to either get/set its values. In ASP.Net, this is straight forward.

First let's create a UserControl and host Aspx page. The important sections are below, but you can download it here.

UserControl

Html:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="UCSimple.ascx.cs" Inherits="UIDemo.UC.UCSimple" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:TextBox id="TextBox1" runat="server">asp:TextBox>
<
asp:Button id="Button1" runat="server" Text="Button">asp:Button>
<
asp:Label id="Label1" runat="server">Labelasp:Label>

CodeBehind:

private void Page_Load(object sender, System.EventArgs e)
{
   
// Put user code to initialize the page here
}

private void Button1_Click(object sender, System.EventArgs e)
{
    this.Label1.Text = this.TextBox1.Text;
}

public string TextValue
{
    get
    {
        return this.TextBox1.Text;
    }
    set
    {
        this.TextBox1.Text = value;
    }
}

Host Aspx Page

CodeBehind

private void Page_Load(object sender, System.EventArgs e)
{
   
// Put user code to initialize the page here
    if (!Page.IsPostBack)
    {
        string[] astr = new string[] {"aaa","bbb","ccc"};
        this.DataGrid1.DataSource = astr;
        this.DataGrid1.DataBind();
    }
}

private const int _UCColumn = 0;
private const int _UCControlPosition = 1;

private void Button1_Click(object sender, System.EventArgs e)
{
    int intRow = Convert.ToInt32(this.TxtRow.Text);
    UCSimple uc = (UCSimple)this.DataGrid1.Items[intRow].Cells[_UCColumn].Controls[_UCControlPosition];
    uc.TextValue = this.TxtSet.Text;

}

private void BtnGet_Click(object sender, System.EventArgs e)
{
    int intRow = Convert.ToInt32(this.TxtRow.Text);
    UCSimple uc = (UCSimple)this.DataGrid1.Items[intRow].Cells[_UCColumn].Controls[_UCControlPosition];
    this.LblGet.Text = uc.TextValue;
}

private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   
//Then this fires too.
    e = e;
}

private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
   
//First this fires
    e = e;
    UCSimple uc = (UCSimple)e.Item.Cells[_UCColumn].Controls[_UCControlPosition];
    this.LblGet.Text = uc.TextValue;

}

Get the UserControl

We can reference the control absolutely:

UCSimple uc = (UCSimple)this.DataGrid1.Items[intRow].Cells[_UCColumn].Controls[_UCControlPosition];

or from the argument in an individual row.

UCSimple uc = (UCSimple)e.Item.Cells[_UCColumn].Controls[_UCControlPosition];

Note what's going on:

  1. Get the row (via .Items[intRow] or just e.Item)
  2. Then get the column (via .Cells[_UCColumn] )
  3. Then get the control within that cell - a single cell can have multiple controls.
  4. Then cast that as the UserControl type that you need.
  5. Then reference the get/set of any public property.

This will allow you to reference user controls from either outside the grid, or with respect to any row.

No comments:

Post a Comment