?
Solved

add if statement in gridview

Posted on 2007-10-17
5
Medium Priority
?
946 Views
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)
else
lnkFile.visible=true  (inkfile  is a hyperlink)


0
Comment
Question by:gianitoo
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

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

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

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

Then in your
0
 
LVL 26

Expert Comment

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

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

Author Comment

by:gianitoo
ID: 20101702
can u write also the syntax for the code behind?
0
 
LVL 8

Accepted Solution

by:
Chumad earned 1000 total points
ID: 20102939
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 :)
0
 
LVL 26

Assisted Solution

by:Rejojohny
Rejojohny earned 1000 total points
ID: 20103287
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 ..
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 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