[RESOLVED]Using CssClass in my local variable

Hi all

I have list of values coming form the table and i want to use the different style for different message type

Example:

if my MessageType is Alert then i want my message be displayed in red and bold

if my MessageType is Announcement then i want my message be displayed in black and bold

Table data:

PortalMessage                            MessageType
This is a Alert Message                     Alert
This is Announcement                Announcement
This is Info1                                        Info1

HTML: 

<asp:Literal ID="Literal1" runat="server"></asp:Literal>

Code Behind

DataSet ds = DataConnections.RunSPReturnData_PortalAdmin("MY procedure", null);

foreach (DataRow dr in ds.Tables[0].Rows)

{
text = "<br />" + dr.ItemArray[1].ToString();
Literal1.Text += text;
}

Thanks in advance

You can use the below code to chagne the color of messagetext based on the message type

 foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string textval = string.Empty;
                if (dr.ItemArray[1].ToString() == "Alert")
                {
                    textval = "<br /><font style='color : red;'><b>" + dr.ItemArray[0].ToString() + "</b></font>";
                }
                else if (dr.ItemArray[1].ToString() == "Announcement")
                {
                    textval = "<br /><font style='color : black;'><b>" + dr.ItemArray[0].ToString() + "</b></font>";
                }
                else if (dr.ItemArray[1].ToString() == "Info1")
                {
                    textval = "<br />" + dr.ItemArray[0].ToString() ;
                }

                Literal1.Text += textval;
            }

Complete Sample:

DataTable table1 = new DataTable("DataTable1");
            table1.Columns.Add("PortalMessage");
            table1.Columns.Add("MessageType");
            table1.Rows.Add("This is a Alert Message", "Alert");
            table1.Rows.Add("This is Announcement", "Announcement");
            table1.Rows.Add("This is Info1 ", "Info1");
            DataSet ds = new DataSet();
            ds.Tables.Add(table1);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string textval = string.Empty;
                if (dr.ItemArray[1].ToString() == "Alert")
                {
                    textval = "<br /><font style='color : red;'><b>" + dr.ItemArray[0].ToString() + "</b></font>";
                }
                else if (dr.ItemArray[1].ToString() == "Announcement")
                {
                    textval = "<br /><font style='color : black;'><b>" + dr.ItemArray[0].ToString() + "</b></font>";
                }
                else if (dr.ItemArray[1].ToString() == "Info1")
                {
                    textval = "<br />" + dr.ItemArray[0].ToString() ;
                }

                Literal1.Text += textval;
            }

Rendered Demo

Hi A2H

Thanks for the quick reply … it worked.

I was wondering is there any way i can do it dynamically. In is example there for only 3 different style. original i have 7 style but i have to repeat  the code for 7 different condition check

Can i do it once and point to different cssclass

Thanks once again

 

I have changes the HTML to class and small change to code fixed it

foreach (DataRow dr in ds.Tables[0].Rows)
{
string textval = string.Empty;
textval = "<div class=’" + dr.ItemArray[1].ToString() + "’>" + dr.ItemArray[0].ToString() + "</div>";
Literal1.Text += textval;

}

then I can give any style in CSS Example:

.Alert
{
       color:blue;
}
.Announcement
{
        color:red;
}
.Info1
{
        color:yellow;
}

Leave a Reply