add if statement in gridview

Posted on 2007-10-17
Last Modified: 2012-08-14
right now i have this as my hyperlynk  in my gridview
   If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lnkFile As HyperLink = CType(e.Row.FindControl("lnkFile"), HyperLink)
            lnkFile.NavigateUrl = "livereg.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "ID").ToString() + "&contentType=" + DataBinder.Eval(e.Row.DataItem, "timeevent").ToString()+"&track="+ DataBinder.Eval(e.Row.DataItem, "track").ToString()
        End If

what i need is to create an statment somehow that if  DataBinder.Eval(e.Row.DataItem, "morning").ToString()  = true   ( bit data type)   then
image1.visible=true     (image1 is asp image)
lnkFile.visible=true  (inkfile  is a hyperlink)

Question by:gianitoo
    LVL 8

    Expert Comment

    Perhaps you could add an <asp:hiddenField> to your gridview... populate it using with your "morning" field... i.e:

          <asp:HiddenField id="hdnValue" value='<%# DataBinder.Eval(Container.DataItem, "morning") %>'

    Then inside your OnRowDataBound event, use FindControl to get the value of hdnValue and do your visible thing to your image.

    Then in your
    LVL 26

    Expert Comment

    try this ..
    <asp:hyperlink id=lnkFile runat="server" Visible='<%# Ctype(Eval("morning"), Boolean) %>' ></asp:HyperLink>

    you can do the same for the image too ..
    LVL 1

    Author Comment

    can u write also the syntax for the code behind?
    LVL 8

    Accepted Solution

    Actually, Rejojohny's solution is much more elegant than what I had proposed.  If you only need to hide an image, do this:

    <asp:image imageurl="~/yourImagehere.jpg" id=lnkFile runat="server" Visible='<%# Ctype(Eval("morning"), Boolean) %>'  </asp:HyperLink>

    If for some reason you need to do more processing other than showing/hiding an image, here's how the OnRowDataBound method would look:
    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e) {

       //make sure we are dealing with a data row and not a header/footer
       if (e.Row.RowType == DataControlRowType.DataRow) {

          //find the hidden control inside the row
          HiddenField hdnValue = (HiddenField)e.Row.FindControl("hdnValue");

          //now use hdnValue.Value to determine what this row's hidden value is
          //Set to "morning"
          if (bool.Parse(hdnValue.Value) == true) {
             //find the imageControl inside the row (make sure you've assigned an ID to it and use it below)
             Image img = (Image)e.Row.FindControl("ImageID");
             img.Visible = true;
          } else {
             //find the hyperlink control inside the row
             HyperLink hlnk = (HyperLink)e.Row.FindControl("hyperLinkID");
             hlnk.Visible = true;

    Hope it helps :)
    LVL 26

    Assisted Solution

    actuall for the image it has to be ..
    <asp:image imageurl="~/yourImagehere.jpg" id=lnkFile runat="server" Visible='<%# NOT Ctype(Eval("morning"), Boolean) %>'  </asp:HyperLink>

    because in his code he has that condition in the "else" part of the "if" condition and so i assume if value = false, lnkfile.visible = true and vice versa ..

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    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…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    755 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

    23 Experts available now in Live!

    Get 1:1 Help Now