For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > July 2004 > DataBinding









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author DataBinding
Jim Heavey

2004-06-28, 8:59 am

I have 2 Databinding questions and perhaps the answer is the same.

I have a dataset in which I DO NOT have a field which identifies the record number but I want to show the specific item number of the row within a DataList control. I was thinking that I would set up a method which would have a counter in it and each tim
e it is executed it would incriment the counter by 1 and then return the string value of the count. So on the HTML document I would have something like the following:
<%# GetRowNumber() %>

An then the method would look like this:
Public string GetRowNumber()
{
_ModuleCtr ++;
return _ModuleCtr.ToString();
}

Is there a better way to do this?

Second question - I may or may not want to display the contents of a data column based upon some condition. I am guessing that I could place script in the HTML to test the variable and either bind the Datacolumn to the field on the HTML or bind the field
to a space, so that nothing would show. If I created a method to do this test, how would I pass the Datarow which is being bound at that time (within the DataList). So right now, without the test, the code would look like the following:
<%# DataBinder.Eval(Container.DataItem,"PERIOD_END", "{0:MM/dd/yyyy}") %>

But if I created a method which did the test (the above does not do the test), how do I pass the "context" which will allow me to pass the appropriate value. So for instance...
Public string GetTheRightValue( ? ? ? ?)
{
if (OKToPrintContents)
Return dataTable[rowNumber]["MyField"].ToString();
else
return " ";
}

So in the above, what is being passed to me which will allow me to get a reference to the row which is being databound for the specific row? I realize I could probably us the OnItemDataBound event or a half a dozen other ways, but can I do it this way?
Bharat Biyani

2004-07-31, 8:56 am

Hi,

Answer to 2nd question.

<%# DataBinder.Eval(Container.DataItem,"PERIOD_END", "{0:MM/dd/yyyy}") %>
>
> But if I created a method which did the test (the above does not do the test), how do I pass the "context" which will allow me to pass the appropriate value. So for instance...
> Public string GetTheRightValue( ? ? ? ?)
> {
> if (OKToPrintContents)
> Return dataTable[rowNumber]["MyField"].ToString();
> else
> return " ";
> }


Pass the function as well as the parameters in the aspx page, like this:

<%# GetTheRightValue(DataBinder.Eval(Container.DataItem,"PERIOD_END" )) %>

as u mentioned in the function u will get the value for each record and depending on ur test u can decide whether to hide show the data.
--
Bharat Biyani (bsb@orcim.com)
http://www.orcim.com




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com