Solved

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

Posted on 2004-10-15
4
290 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:l_agmon
4 Comments
 
LVL 7

Expert Comment

by:gsiric
ID: 12326276
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;
}

0
 

Author Comment

by:l_agmon
ID: 12326799
I am afraid this will not work - as each datalist can contain many records, and setting a single viewstate variable is not enough.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 12399274
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;
0
 
LVL 16

Accepted Solution

by:
MikeMCSD earned 500 total points
ID: 12450609
Set the visible property to "False" for MessageLabel label on the Web form.
Use the ItemDataBound event for the DataList.
Here my DataList is called "list" :

Private Sub list_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles list.ItemDataBound

        ' need this line first:
        If Not (e.Item.ItemType = ListItemType.Header) And Not (e.Item.ItemType = ListItemType.Footer) Then

           ' need to locate the label:
            Dim lblMsg As Label
            lblMsg = e.Item.FindControl("MessageLabel")

           ' here you can put your condition to check for; here I'm just checking the
           ' text property for "True":

            If lblMsg .Text = "True" Then
                lblMsg .Text = "Engraving Available"
                lblMsg .Visible = True
            End If

        End If
    End Sub


Here I'm just checking the field from the database for true or false but you could
modify the logic to your situation.

You can set the text property of the Label when you do your update and then just check
for the value with the above code and display the Label.

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question