Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-10-15
4
Medium Priority
?
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1500 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

Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

730 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