DataBinding

G

Guest

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 time 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?
 
G

Guest

Hi,

Answer to 2nd question.

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top