How to programmatically access ObjectDataSource values (instead of binding to data controls)

G

Guest

I've defined an ObjectDataSource against a dataset, and I can bind the
ObjectDataSource's members to data controls.

I'm wondering how to take the values in an ObjectDataSource and
programmatically manipulate them before they're bound to any data control.
I'd be using them almost like you'd use recordset fields from Classic ADO.

In other words, I would like to grab the value corresponding to a
publication date from my database, use the date information to build a path
as part of some string manipulation, and then use my manipulated string to
help build the web page. The anologue for what I'm trying to do in Classic
ADO/Classic ASP would be something like:

yearFolder = ("http://whatever.com/site/images/newsreleases/" &
year(rsArticleText.Fields.Item("Con_PubDate").Value) & "/")
monthFolder = yearFolder &
monthname(month(rsArticleText.Fields.Item("Con_PubDate").Value)) & "/"
Any help out there? Thanks very much.

-KF
 
G

Guest

It took awhile to work this out on my own, but there was lots of great
learning along the way.

Here's a very helpful article about the finer points of ASP.NET databinding:
http://www.codeproject.com/aspnet/Mastering_DataBinding.asp

I realized that the DataList raises events as it binds each item, and I was
thinking I could call a formatting function on one of these events. However,
the article suggested a much more direct way:

"....While binding, it's possible to do simple formatting directly in the
databinding expression or by calling functions which reside in
code-behind..."

It offered this example:
<%# FormatDate(DataBinder.Eval(Container.DataItem, "Ordered"))%>
^---------------------- my custom function

My codebehind looks like this:

public string GetYearFolder (object specifieddate)
{
DateTime cspecifieddate = Convert.ToDateTime(specifieddate);
string yearFolder = ("http://whatever.com/news/images/newsreleases/" +
((cspecifieddate.ToString("yyyy")) + "/" +
((cspecifieddate.ToString("MMMM")) + "/")));
return yearFolder;
}

If you embed the output as the ImageURL of an asp:image control, another
question will be just around the bend: "how do I hide the stupid red 'x'
icons for list items which have no image?"

Understanding the stuff above points the way: you can test for the presence
of an image string, and show or hide visibility based on whether there's
anything there.

Codebehind:

public bool IsImageAvailable(object iconstring)
{
string teststring = Convert.ToString(iconstring);
if (teststring == "")
{
bool imagepresent = false;
return imagepresent;
}
else
{
bool imagepresent = true;
return imagepresent;
}

}

ASPX:

<asp:Image runat=server Visible='<%#IsImageAvailable(Eval("Con_Icon"))%>'
ImageUrl ='<%# GetYearFolder(Eval("Con_PubDate1"))+ Eval("Con_Icon")+
".jpg"%>' BorderColor="1" BorderWidth="1" />

-KF
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top