Solved

Code in ASPX page for If else logic

Posted on 2004-08-24
9
3,031 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now