Solved

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

Posted on 2004-10-15
4
287 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now