By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,274 Members | 2,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,274 IT Pros & Developers. It's quick & easy.

RowDataBound: testing for data elements for conditional show/hide

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.