Solved

Code in ASPX page for If else logic

Posted on 2004-08-24
9
3,037 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
  • 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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