Solved

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

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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