Link to home
Start Free TrialLog in
Avatar of l_agmon
l_agmon

asked on

DataList - Updateing a Label in ItemTemplate from an "Update" command

Hi,

Can I somehow solve the following situation? :

I a datalist, the user presses Edit. The <EditItemTemplate> become visible. The user then presses 'Save' - and the UpdateCommand is being called.

After I update the relevant data, I return to the list (i.e., items are presented as <ItemTemplate>).

What I want to do is to make a label in <ItemTemplate> become visible, upon the use pressing 'Save' in the <EditItemTemplate>. Is that possible?

I tried to create a Label called MessageLabel in the <ItemTemplate>, and in the UpdateCommand have the following line:
((Label) e.Item.FindControl("MessageLabel")).Visible=true;

It doesn't work - I suspect because the "e" variable contains only the controls of <EditItemTemplate>.

Thanks !

Agmon.
Avatar of gsiric
gsiric

I suggest to use help variable stored in ViewState:

bool bLabelVisible
{
  set {ViewState["LabelVisible"] = value;}
 get {return (bool)ViewState["LabelVisible"];}
}

In OnPageLoad when there is not postback (first call to page) set bLabelVisible to false;

if (!IsPostBack)
{
...
bLabelVisible = false;
}

In UpdateCommand set bLabelVisible to true:
bLabelVisible = true;

in OnItemDataBound event add:

if (e.Item.ItemType == ListItemType.ItemTemplate || e.Item.ItemType == ListItemType.AlternatingItemTemplate)
{
   ((Label) e.Item.FindControl("MessageLabel")).Visible=bLabelVisible;
}

Avatar of l_agmon

ASKER

I am afraid this will not work - as each datalist can contain many records, and setting a single viewstate variable is not enough.
There are a couple of things going on. But the main problem is as follows

The message label is within the ItemTemplate, but the UpdateCommand fires in the EditItemTemplate. What you need to do:
1) databind the updated datasource to the datalist
2) find the row you were in
3) show the message label

Pseudocode:

Dim iMyIndex Int32 = YourList.EditItemIndex
YourList.EditItemIndex = -1
DataBind()
Dim CurrentItem as DataListItem = YourList.Items.Item(iMyIndex)
CurrentItem.FindControl("MessageLabel").Visible=true;
ASKER CERTIFIED SOLUTION
Avatar of MikeMCSD
MikeMCSD
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial