473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

RowDataBound: testing for data elements for conditional show/hide

I'm using the DataList and GridView controls, and I am trying to wrap my
head around the problem of conditionally showing or hiding cells/cell
content based on the presence or absence of DB data. I am finding this sort
of problem by far the most annoying part about working with ASP.NET
controls.

What I want is to know how to do three things:

- conditionally show or hide an ImageField +column+ based on whether or not
an the ImageField's DataImageUrlField is empty or not

-conditionally show or hide an TemplateField based on whether a DB field
associated with the row was empty or not. (In this scenario, an Image
control would be embedded in the ItemTemplate.

-conditionally show or hide an Image control based on whether a DB field
containing the URL for the image was empty or not.

I understand how to declaratively call a function attached to the
RowDataBound event, and I know how to hide columns based or the presence or
absence of string data in those fields:

protected void GridView2_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.Cells[0].Text == "")
{
e.Row.Cells[0].Visible = false;
}
}

What do I need is the syntax to test for the Url's presence or absence in my
DB.

Thanks for any help you might be able to offer.

-KF
Jul 21 '06 #1
3 7706
Hi,

Thank you for your post.

Based on my understanding, your question is how to read the binding data
record of the current row in GridView. If I've misunderstood anything,
please feel free to post here.

You can get the binding data record using following code example:

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string url = DataBinder.Eval(e.Row.DataItem, "PhotoPath") as
string;
if (url != null && url.Length 0)
{

}
}
}

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 24 '06 #2
I'm still looking for answers on the three points below. Any ideas?

-KF
<ke*****@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I'm using the DataList and GridView controls, and I am trying to wrap my
head around the problem of conditionally showing or hiding cells/cell
content based on the presence or absence of DB data. I am finding this
sort of problem by far the most annoying part about working with ASP.NET
controls.

What I want is to know how to do three things:

- conditionally show or hide an ImageField +column+ based on whether or
not an the ImageField's DataImageUrlField is empty or not

-conditionally show or hide an TemplateField based on whether a DB field
associated with the row was empty or not. (In this scenario, an Image
control would be embedded in the ItemTemplate.

-conditionally show or hide an Image control based on whether a DB field
containing the URL for the image was empty or not.

I understand how to declaratively call a function attached to the
RowDataBound event, and I know how to hide columns based or the presence
or absence of string data in those fields:

protected void GridView2_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.Cells[0].Text == "")
{
e.Row.Cells[0].Visible = false;
}
}

What do I need is the syntax to test for the Url's presence or absence in
my DB.

Thanks for any help you might be able to offer.

-KF

Jul 26 '06 #3
I suppose your first 3 questions are all about to show/hide an individual
control inside current row's cell based on whether or not the database
field value is empty or not, not the whole column. If I've misunderstood
anything, please feel free to post here.

Also, I think question 2 and 3 are basically the same question, to hide a
cell, set its Visible to false will not prevent the <tdtag from
generating, only the content in it will be empty.

<quote>
- conditionally show or hide an ImageField +column+ based on whether or not
an the ImageField's DataImageUrlField is empty or not
</quote>

Based on my test, if the field's value is empty, ASP.NET will not generate
an img tag for the ImageField, therefore I think you don't need to anything
here.

<quote>
-conditionally show or hide an TemplateField based on whether a DB field
associated with the row was empty or not. (In this scenario, an Image
control would be embedded in the ItemTemplate.

-conditionally show or hide an Image control based on whether a DB field
containing the URL for the image was empty or not.
</quote>

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control c = e.Row.Cells[4]; // 4 is the index of the
TemplateField
Image img = c.FindControl("myimg") as Image;
string url = DataBinder.Eval(e.Row.DataItem, "PhotoPath") as
string;
if (url != null && url.Length 0)
{
img.ImageUrl = url;
} else
{
img.Visible = false; // you can also call c.Visible =
false;
}
}
}

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hibernate | last post by:
I'm somewhat new to javascript/DHTML, and this problem has been plaguing me. I have made an 'array' of <div> tags within my html document like so: <div id="menu"> menu1 </div> <div id="menu">...
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
5
by: Ed Jay | last post by:
I have a switch statement that controls which of several containers is displayed or not. It currently looks like: function showHelp(n) { show('vhelp'); //makes parent container visible switch...
1
by: asilverpeach | last post by:
Hey Guys! Found some great scripts here on this topic but have to make to changes to the code that I can't seem to figure out. First, In the following code clicking on the headers shows the...
3
by: toodi4 | last post by:
I'm using a javascript that hides and unhides text based on a button click. It works great across static fields on a form. The problem I have is that I'm trying to hide and unhide various fields...
13
by: farhaaad | last post by:
Dears, I have many feilds in my tables that are left blank for some records, If I make a sub table for it and and change those feilds to a lookup feilds so only those options are chosen which...
2
by: Tim | last post by:
I'm using a GridView with its DataSource property set to a DataTable. During the RowDataBound event, the first row of the DataTable is reported as a DataControlRowType.Header instead of...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.