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


C

cindy

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 = "";
}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top