Solved

label in itemtemplate of listview

Posted on 2010-08-12
10
1,059 Views
Last Modified: 2013-12-26
Hi,

I have a listview bound to a datacontrol.
In the ItemTemplate I want to show a label and database value for every record where the database value is not null.  so I have
<asp:Label
      ID="work_records_results1_StatusChange1"  
     runat="server"
     Visible='<%# Not IsDBNull(Eval("status_last"))%>'
     Text="<br />Record status was changed from "
     Enabled = "true">
</asp:Label>

Now... what is supposed to happen is that IF the database field named status_last is not null THEN the label should become visible and the text should read:
 Record status was changed from XX (where XX should be the value in the database field named old_status).

As it is, the above code will work.... but I can't get the text property to include both literal text AND a database value... I can manage one or the other, but not both.  
I have tried every possible combination of single and double quotes etc.  

Does anyone know the proper format here?
                                                         
0
Comment
Question by:santaspores1
[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
  • 6
  • 3
10 Comments
 

Author Comment

by:santaspores1
ID: 33421300
by the way... I can use two labels - one for the literal and one for the database field.... but I don't want to ... my actual code required me to string together several literals and several database fields... and I would end up with 8 or so labels to print one sentence to the screen.
0
 
LVL 22

Accepted Solution

by:
Mrunal earned 250 total points
ID: 33427951
try this:

set lable's following properties:

Text='<%# Convert.ToString(DataBinder.Eval(Container.DataItem,"status_last"))==""?"":Convert.ToString(DataBinder.Eval(Container.DataItem,"status_last")) %>'

Visible='<%# Convert.ToString(DataBinder.Eval(Container.DataItem,"status_last"))==""?"Convert.ToBoolean(false)":Convert.ToBoolean(true)) %>'

hope this helps you.
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 33427963
here it is:

http://www.dotnetspider.com/resources/21897-If-condition-with-inline-code.aspx

Another way is you can call code behind function and get visibility and text value from those functions by passing same parameters.

here are references:

http://csharpdotnetfreak.blogspot.com/2009/08/handle-gridview-eval-null-itemtemplate.html

and

search for - 'DisplayDaysOnJob' in

http://msdn.microsoft.com/en-us/library/bb288032.aspx
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Assisted Solution

by:Ramkisan Jagtap
Ramkisan Jagtap earned 250 total points
ID: 33428020
You can do it as  below :

<asp:Label ID="work_records_results1_StatusChange1" runat="server"
  Visible='<%# Not IsDBNull(Eval("status_last"))%>' Text="<br />Record status was changed from '<%#Eval("old_status")%>'" Enabled = "true"> </asp:Label>
0
 

Author Comment

by:santaspores1
ID: 33428947
Ramkisan:  You do understand what I was trying to do, but it doesn't work.  Here is the output from that code:
Record status was changed from 'unprocessed'" Enabled = "true">  
It incorrectly displays single quotes around my database value and then write the rest of the label properties to the screen.  What I want to show up on the screen is:
Record status was changed from unprocessed.

The output will actually be much longer with several literals and database values.

mroonal:I don't see, from your response, how I can combine literal text and a database value in the text property of my label.  Am I missing it?

Grumble:  .net is really turning out to be a drag in my opinion.  If your goals are simple and standard it is quick and easy.  But if you start by designing your solution (as you should) and try to use .net to get there....  the code becomes very messy very quickly.
0
 

Author Comment

by:santaspores1
ID: 33429080
mroonal:

The code you listed
Text='<%# Convert.ToString(DataBinder.Eval(Container.DataItem,"status_last"))==""?"":Convert.ToString(DataBinder.Eval(Container.DataItem,"status_last")) %>'
Throws an error...Expression expected.
What is that code supposed to display exactly?
0
 

Author Comment

by:santaspores1
ID: 33429083
P.S.  I am VERY grateful for the help.  Thanks to both of you.
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 33429588
hi santaspores1,

It was very simple. I think you can manage it easily with removing minor syntax bugs.
Never mind. Here is what i want to say:

step 1. here is my gridview design in ASPX file:

<asp:GridView ID="gvEscalationClaims" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField HeaderText="City" ItemStyle-Width="100px">
    <ItemTemplate>
    <asp:Label ID="lbltest1" runat="server"
     Text='<%# Convert.ToString(Custom_IIF(Eval("city"))) %>'
      Visible='<%# Convert.ToBoolean(Convert.ToString(DataBinder.Eval(Container.DataItem, "city")) == "" ? "false" : "true") %>'
     ></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Status" ItemStyle-Width="150px">
    <ItemTemplate>
    <asp:Label ID="lbltest2" runat="server"
     Text='<%# Convert.ToString(DataBinder.Eval(Container.DataItem, "status_last")) == "" ? "blank" : "not blank: " + Eval("status_last") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>


step 2:  in code behind i just created a dummy data table and bind it to grid.

protected void Page_Load(object sender, EventArgs e)
        {          
            this.BindGrid();
        }

        private void BindGrid()
        {
            DataTable dt = CreateDataTable();

            for (int i = 0; i < 10; i++)
            {
                if (i == 3)
                    AddDataToTable("", "pune", dt);
                else
                    AddDataToTable("pune-" + i.ToString(), null, dt);
            }

            gvEscalationClaims.DataSource = dt;
            gvEscalationClaims.DataBind();
        }

        private DataTable CreateDataTable()
        {
            DataTable myDataTable = new DataTable();

            DataColumn myDataColumn;
                       
            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "status_last";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            //myDataColumn.DataType = Type.GetType();
            myDataColumn.ColumnName = "city";
            myDataTable.Columns.Add(myDataColumn);

            return myDataTable;
        }

        private void AddDataToTable(string status_last, string city, DataTable myTable)
        {
            DataRow row;
            row = myTable.NewRow();            
           
            row["status_last"] = status_last;
            row["city"] = city;

            myTable.Rows.Add(row);
        }



step 3: If you read carefully my second reply, i also had given you another way..
here it is:

add following function in code behind...

public string Custom_IIF(object city)
        {
            if (city.ToString() == "")
                return "NULL object";
            else
                return city.ToString();
        }




try this...
and let me know if this makes sense ?
0
 

Author Comment

by:santaspores1
ID: 33429651
I finally stumbled upon the solution:

Text='<%# "<br /> The status was changed from " & Eval("status_last")%>'

just a matter of placing the single and double quotes properly.
0
 

Author Closing Comment

by:santaspores1
ID: 33429666
Thank you very much for your time.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

724 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