Solved

Code in ASPX page for If else logic

Posted on 2004-08-24
9
3,048 Views
Last Modified: 2012-08-14
Below is the code I will display as data grid in the frontend
I would like to display if outpit  value is not eaual to empty then display the value normal otherwise display the value in Bold
Can you please help me do that

<asp:datagrid CssClass="ReportTable" id="dgIndexValue" runat="server" AutoGenerateColumns="False"
                                          OnItemDataBound="dgIndexValue_ItemDataBound" AllowSorting="True" OnPageIndexChanged="Page_IndexChanged"
                                          OnItemCreated="dgIndexValue_ItemCreated" OnSortCommand="Page_SortCommand" OnDeleteCommand="dgIndexValue_DeleteCommand"
                                          OnEditCommand="dgIndexValue_EditCommand" OnCancelCommand="dgIndexValue_CancelCommand" OnUpdateCommand="dgIndexValue_UpdateCommand"
                                          ShowHeader="True" ShowFooter="false" PagerStyle-CssClass="ReportTablePager" HeaderStyle-CssClass="ReportTableHeader"
                                          FooterStyle-CssClass="ReportTableFooter" AlternatingItemStyle-CssClass="ReportTableAlternate"
                                          SelectedItemStyle-CssClass="ReportTableSelected" OnItemCommand="dgIndexValue_ItemCommand">
                                          <Columns>
                                                <asp:BoundColumn DataField="IndexValueID" ReadOnly="True" Visible="False" HeaderText="Index Value ID" />
                                                <asp:BoundColumn DataField="IndexType" ReadOnly="True" Visible="False" HeaderText="Index Type" />
                                                <asp:BoundColumn DataField="IndexName" ReadOnly="True" HeaderText="Name"></asp:BoundColumn>
                                                <asp:TemplateColumn HeaderText="Value">
                                                      <ItemTemplate>
                                                      If <%# DataBinder.Eval(Container, "DataItem.IndexVal") %> <> "" then  // here i want to disp not bold
                                                            <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.IndexVal") %>' ID="Label2"/>
                                                      Else  // Here I want to diaply bold
                                                            <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.IndexVal") %>' ID="Label4"/>
                                                      End If
                                                      </ItemTemplate>
                                                      <FooterTemplate>
                                                            <asp:Button Text="Add" CommandName="Insert" Runat="server" ID="Button1"></asp:Button>
                                                      </FooterTemplate>
                                                      <EditItemTemplate>
                                                            <asp:TextBox runat="server" ID="EditIndexVal" Text='<%# DataBinder.Eval(Container, "DataItem.IndexVal") %>'/>
                                                            <asp:RegularExpressionValidator id="IndexValRegEx" runat="server" ControlToValidate="EditIndexVal" ValidationExpression=".*"
                                                                  ErrorMessage="Your entry is not valid." display="dynamic" />
                                                            <asp:RequiredFieldValidator runat="server" id="IndexValReq" ControlToValidate="EditIndexVal" ErrorMessage="*" />
                                                      </EditItemTemplate>
                                                </asp:TemplateColumn>
                                                <asp:BoundColumn DataField="Editable" Visible="False" HeaderText="Editable" />
                                                <asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                                          </Columns>
                                    </asp:datagrid>


Please help me Thanks
Pallilu
0
Comment
Question by:pallilu
[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
  • 3
  • 2
9 Comments
 

Author Comment

by:pallilu
ID: 11884350
I tried like this, But no response
<ItemTemplate>
                                                      If <%# DataBinder.Eval(Container, "DataItem.IndexVal") %> <> " " then
                                                            <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.IndexVal") %>' ID="Label2"/>
                                                      Else
                                                            <asp:Label Font--Bold="True" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.IndexVal") %>' ID="Label4"/>
                                                      End If
                                                      </ItemTemplate>
it is displaying whole text in the browser for Index value

Please help me
Pallilu.
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11884556
You can't put that kind of logic inside the datagrid.  The easiest way to make it bold would be to catch it in your SQL statment, if that's what you're using.  Can you post your SQL?
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11884573
BTW, Why do you need to make it bold only when the value is <> " "?  It doesn't have any affect on the output if you have bold when the value is nothing
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:pallilu
ID: 11884806
If I use this code
If <%# DataBinder.Eval(Container, "DataItem.IndexVal") %> <> " " then
                                                      <ItemTemplate>            
                                                            <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.IndexVal") %>' ID="Label2"/>
                                                      </ItemTemplate>
                                                Else
                                                      <ItemTemplate>            
                                                            <asp:Label Font--Bold="True" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.IndexVal") %>' ID="Label4"/>
                                                      </ItemTemplate>
                                                End If
Then i am getting message parse Error
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Literal content ('If') is not allowed within a 'System.Web.UI.WebControls.TemplateColumn'.

Source Error:


Line 117:                                                <asp:BoundColumn DataField="IndexName" ReadOnly="True" HeaderText="Name"></asp:BoundColumn>
Line 118:                                                <asp:TemplateColumn HeaderText="Value">
Line 119:                                                If <%# DataBinder.Eval(Container, "DataItem.IndexVal") %> <> " " then
Line 120:                                                      <ItemTemplate>            
Line 121:                                                            <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.IndexVal") %>' ID="Label2"/>
 

Source File: c:\inetpub\wwwroot\NotreDame\DocMaint.aspx    Line: 119


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.985; ASP.NET Version:1.1.4322.968
0
 

Author Comment

by:pallilu
ID: 11884926
Trevorhartman,
Thanks for your response,Here is my SQl strored procerdure

CREATE    PROCEDURE usp_IndexValueGetByDocID
(
       @DocID INT
) AS

--DEBUG ON
--declare @DocID INT
--set @DocID = 2
--DEBUG OFF


SET NOCOUNT ON
SELECT
      IVA.DocID,
      IVA.IndexValueID,
      IVA.IndexNameID,
      INACol.IndexName,
      IT.IndexType,
      dbo.udf_IndexValue(INACol.IndexTypeID, IVA.StringVal, IVA.IntVal, IVA.FloatVal, IVA.DateVal) AS IndexVal,
      IVA.CreateDate,
      IVA.CreatedBy,
      IVA.ModifyDate,
      IVA.ModifiedBy,
      INACol.Editable
FROM tb_IndexValue IVA
INNER JOIN tb_IndexName INACol ON INACol.IndexNameID = IVA.IndexNameID
INNER JOIN tb_IndexType IT ON INACol.IndexTypeID = IT.IndexTypeID
WHERE IVA.DocID = @DocID
AND INACol.Editable = 1

GO
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11885699
try changing your select statement to this:

SELECT
     IVA.DocID,
     CASE WHEN (IVA.IndexValueID='') THEN '' ELSE '<b>' + IVA.IndexValueID + '</b>' END AS IndexValueID,
     IVA.IndexNameID,
     INACol.IndexName,
     IT.IndexType,
     dbo.udf_IndexValue(INACol.IndexTypeID, IVA.StringVal, IVA.IntVal, IVA.FloatVal, IVA.DateVal) AS IndexVal,
     IVA.CreateDate,
     IVA.CreatedBy,
     IVA.ModifyDate,
     IVA.ModifiedBy,
     INACol.Editable
FROM tb_IndexValue IVA
INNER JOIN tb_IndexName INACol ON INACol.IndexNameID = IVA.IndexNameID
INNER JOIN tb_IndexType IT ON INACol.IndexTypeID = IT.IndexTypeID
WHERE IVA.DocID = @DocID
AND INACol.Editable = 1

this will work when the value in the data table is '', not when its NULL.  do you need checking for null as well?

Trevor
0
 
LVL 3

Expert Comment

by:gspronych
ID: 11887304
I think you are getting way too complicated.
I have a page that when a row in a datagrid has a value = 'Y', then it bolds the line.

Go to the code behind, add this code to the ItemDataBound sub I included.
I commented the sub for any values you will need to change.

I also use the FormatHeading sub to format all datagrids in my project, also copy to your project.
or you can assign the formatting right in the if statement, but I chose to make it a global function.

    Private Sub dtlTasks_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dtlTasks.ItemDataBound

        Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
        Dim strHeading As String

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            strHeading = drv.Row("TTHeading").ToString
'change the value in quotes to you column value you want to test

            If strHeading = "Y" Then
'change the if to what value you would like to test for
                FormatHeading(e.Item)
            End If
        End If

    End Sub

This sub formats the row

    Shared Function FormatHeading(ByVal e As System.Web.UI.WebControls.DataListItem)

        'e.Font.Bold = True 'bold
        'e.BackColor = Color.Beige 'change back color
        e.CssClass = "TaskHeader" 'or assign a cssclass
    End Function

I hope this works for you
0
 

Author Comment

by:pallilu
ID: 11899550
Gspronych,
Thanks for your response
Can you please help me where to put your code in the below function.
I am attaching the function and page load function where I am calling the CreateIndexValueDataSource() and also ASPX code.
CreateDatasource function
-----------------------------------------------------------------------------------------------
      public ICollection CreateIndexValueDataSource()
            {
                  DataView dv = null;
                  if (Request.QueryString["DocID"] != null)
                  {
                        DataSet ds = null;
                        try
                        {
                              SqlParameter [] param=new SqlParameter[1];
                              param[0] = new SqlParameter("@DocID",Convert.ToInt64(Request.QueryString["DocID"]));
                              ds = provider.FillDataSet("usp_IndexValueGetByDocID",param);
                              dv =  ds.Tables[0].DefaultView;
                        }                        
                        catch(Exception ex)
                        {
                              lblError.Text = ex.Message;
                              lblError.Visible = true;
                        }
                        finally
                        {
                              if (ds != null)
                              {
                                    ds.Dispose();
                              }
                              ds = null;
                        }
                  }
                  return dv;//only null if errors occur
            }
-----------------------------------------------------------------------------------------------------------
For loading page function I Include that Datasource to display in ASPX Data grid.
First it displays the Document values and then the Index values
I am concerned about index values.
So I want to change the CreateIndexValueDataSource() method.

Page Load
----------------------------------------------------------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
            {
      

                  SecurityAccess sa = new SecurityAccess();
                  if ( sa.AllowedAccess( Convert.ToInt32( this.User.Identity.Name), "DocMaint", "Read", "", ""))
                  {
                        if (Request["DocID"]!=null && Convert.ToInt32(Request["DocID"])>0)
                        {
                              dgDoc.Visible = true;
                              dgIndexValue.Visible = true;
                        }
                        else
                        {
                              dgDoc.Visible = false;
                              dgIndexValue.Visible = false;
                        }


                        if (!IsPostBack)
                        {
                              dgDoc.DataSource = CreateDocumentDataSource();
                              dgDoc.DataBind();

                              dgIndexValue.DataSource = CreateIndexValueDataSource();
                              dgIndexValue.DataBind();
                        }


                        // Check security access
                        if ( sa.AllowedAccess( Convert.ToInt32( this.User.Identity.Name), "DocMaint", "Write", "", ""))
                        {
                              dgDoc.Columns[10].Visible = true;
                              dgIndexValue.Columns[5].Visible = true;
                        }
                        else
                        {
                              dgDoc.Columns[10].Visible = false;
                              dgIndexValue.Columns[5].Visible = false;
                        }
                  }
                  else
                  {
                        Response.Redirect("Login.aspx");
                  }
            }
-------------------------------------------------------------------------------------------------------------
ASPX page Code to display Indexvalues
Then I am also attaching data grid code  in aspx page
--------------------------------------------------------------------------------------------------------
<ItemTemplate>
      <asp”Label runat=”server” Text=’<%# DataBinder..Eval(Container, DataItem.IndexVal”) %>’ ID =”Label14”/>
</ItemTemplate>
--------------------------------------------------------------------------------------------------------
Please help me
I'll appreciate it
thanks
Pallilu
0
 
LVL 3

Accepted Solution

by:
gspronych earned 90 total points
ID: 11904304
My code is in VB, sorry I don't know much C so you'll have to convert it

The code does not need to be inserted into your code, rather it is triggered by the ItemDataBound method of the datagrid.
To insert the code into your project, follow these steps

Double click your datagrid in design view (dgIndexValue), then it'll take you into the code behind.
You'll be in this sub dgIndexValue_SelectedIndexChanged
On the top toolbar, change the method from 'SelectedIndexChanged' to 'ItemDataBound'

Insert the below code into the ItemDataBound method
Also change TTHeading to the value you want to test for, then change the IF
I don't think DataItem.IndexVal will work, it caused an error in my VB code, put the column name in quotes, as in my sample, my sql column is TTHeading

start code insert in dgIndexValue_SelectedIndexChanged method
--------------
        Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
        Dim strHeading As String

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            strHeading = drv.Row("TTHeading").ToString
'change the value in quotes to you column value you want to test

            If strHeading <> " " Then
'change the if to what value you would like to test for
            e.Item.BackColor = Color.Beige
            End If
        End If

------------------
end of code insert

Also note
e.item formats the entire row, mine turns the back color beige

e.item.Font.Bold does not format textboxes or labels
simply write your values to the screen if you want to be able to bold them instead of assigning them to a label

I hope this helps
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

627 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