RowDataBound: testing for data elements for conditional show/hide

G

Guest

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
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 
W

Walter Wang [MSFT]

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 <td> tag 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 ([email protected], 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top