asp.net webpart access to data bound to control using ITemplate cl

Discussion in 'ASP .Net' started by cindy, Dec 9, 2009.

  1. cindy

    cindy Guest

    I have a c# 2008 asp web part with a Datalist for which I have defined my own
    template. I have done this by creating a class that implements ITemplate.
    After I get some data I use the ITemplate class to create the control so I
    can add it back in the web part.
    This is the problem, for a label (lblHH) in the Datalist I need to compare
    the label’s text to a string held in ViewState["SearchHighlight"]. I use a
    procedure declared in the web part to compare and highlight matching words.
    I use the ITemplate class to create the Datalist
    class DatalistColumns : ITemplate
    {
    public DatalistColumns()
    {
    }
    public void InstantiateIn(Control container)
    {

    Label lblHH = new Label();
    lblHH.DataBinding += new EventHandler(lblHH_DataBinding);
    container.Controls.Add(lblHH);
    ///////more controls

    }

    void lblHH_DataBinding(object sender, EventArgs e)
    {
    Label lbl = (Label)sender;
    DataListItem container = (DataListItem)lbl.NamingContainer;
    if (DataBinder.Eval(((DataListItem)container).DataItem,
    "HitHighlightedSummary") != System.DBNull.Value)
    lbl.Text =
    Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem,
    "HitHighlightedSummary"));
    else
    lbl.Text = "";
    }
    }

    In the web part I have a dataset, I create a Datalist with ITemplate class,
    bind it and add the ItemDataBound event.
    private void fillResultsList(DataSet grdDs)
    {
    dlResults = new DataList();
    dlResults.DataSource = grdDs;
    dlResults.ItemDataBound += new
    DataListItemEventHandler(dlResults_ItemDataBound);
    dlResults.ItemTemplate = new DatalistColumns();
    dlResults.DataBind();

    Controls.Add(dlResults);
    }

    I enter into the ItemDataBound event of the Datalist in the webpart. This
    event fires after the Datalist has had data bound to the label in the
    ITemplate class so I know that lblHH is not null but when code enter the
    event ItemDataBound, FindControl comes back null. How do I reach that label
    instantiated in the ITemplate class in the ItemDataBound event on the web
    part.
    void dlResults_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    if (ViewState["SearchHighlight"].ToString().Length > 0)
    {
    Label lblhigh = (Label)e.Item.FindControl("lblAbstract");
    string eHH = lblhigh.Text;
    lblhigh.Text =
    Highlight(ViewState["SearchHighlight"].ToString(), eHH);
    }
    }

    I would rather use the string in ViewState["SearchHighlight"] to process the
    label in the DataBinding event in the ITemplate class but I cannot access a
    ViewState["SearchHighlight"] object in the ITemplate class, I don’t know how.
    If I could get the string out of the ViewState["SearchHighlight"] from
    inside ITemplate class I could do the compare in
    void lblHH_DataBinding(object sender, EventArgs e)
    {
    Label lbl = (Label)sender;
    DataListItem container = (DataListItem)lbl.NamingContainer;
    if (DataBinder.Eval(((DataListItem)container).DataItem,
    "HitHighlightedSummary") != System.DBNull.Value)
    lbl.Text =
    Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem,
    "HitHighlightedSummary"));
    else
    lbl.Text = "";
    }
    cindy, Dec 9, 2009
    #1
    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:
    658
    cosine... zero
    Jun 9, 2004
  2. Mario Krsnic
    Replies:
    0
    Views:
    376
    Mario Krsnic
    Jun 23, 2006
  3. anars
    Replies:
    0
    Views:
    361
    anars
    Jan 7, 2007
  4. Brook

    Custom Control, ITemplate and nested bound controls

    Brook, Mar 17, 2008, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    436
    Brook
    Mar 25, 2008
  5. cindy
    Replies:
    0
    Views:
    765
    cindy
    Dec 9, 2009
Loading...

Share This Page