Solved

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

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

622 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