Showing posts with label repeater. Show all posts
Showing posts with label repeater. Show all posts

Tuesday, November 30, 2010

validating dynamically created radiobuttonlists

I recently was working on writing a client validation function to go hand in hand with my customvalidator. My form had some regular ASP controls on it, and it also had a section of questions inside a repeater, since this section was designed to ask you to rate instructors, and the name and quantity of said instructors depended on which class it was. I don't remember trying to write javascript to check radiobuttonlists inside a repeater before but eventually read a helpful snippet here.

In the example at that link, the entire form is iterated through. In my case however the form had radipbuttonlists both in and out of the repeated, and I only wanted to iterate through the repeater for this check. I already had simpler functions for the rest of them. Additionally I did not want a message to pop up for every unanswered radiobuttonlist in this section, but merely one generic "please rate all instructors" message. So I wrapped the repeater in a div, and it is this div that the variable called container below references.


// Grabs all inputs - radio, checkbox, text, buttons and lists -sticks them in an array
var last = "NameUnlikelyToBeUsedAsAnElementName";

var container = document.getElementById("divRpt");
var allInputs = container.getElementsByTagName("input");

// walk through the array
for (i = 0; i< allInputs.length; i++)
{
var input = allInputs[i];
if (input.name == last) continue; // if this object name is the same as the last checked radio, go to next iteration

// checks to see if any one of similarly named radiobuttons is checked
else if (input.type == "radio" )
{
last = input.name;
var radios = document.getElementsByName(input.name);
var radioSelected=false;

//iterate over question options
for (j=0; j < radios.length; j++)
{
if(radios[j].checked)
{
radioSelected=true;
break; // Found it, proceed to next question
}
}
if (!radioSelected) // no option selected
{
bOK = false;
}
}
if (!bOK){alert("Please rate the overall teaching effectiveness for your instructor(s)");}

Monday, August 17, 2009

accessing a control in a repeater's headertemplate or footertemplate

If you are trying to FindControl on the repeater's ItemDataBound or ItemCreated event it's pretty easy. However I needed to access a control from the repeater's PreRender event. This is when I discovered that "Only items bound to the data source are contained in the Items collection. The header, footer, and separator are not included in the collection." (from MSDN).

Therefore I had to use a line like this which apparently works because "all child controls must exist inside the Controls collection" ("In Search of ASP.NET Controls", from odetocode.com)


HtmlTableRow tr = (HtmlTableRow)rptIncomplForms.Controls[0].Controls[0].FindControl("trFoot");

Thursday, July 9, 2009

dynamically hiding rows in a repeater

I recently needed to hide a row in a repeater if it had no data. I feel like I had done this before but I could not remember how. After a lot of reading and experimenting, I figured out that it was really pretty easy to do. (Note that I read a couple of ways that I think were not the way to go, so I'll mention them here: one person ad made a hidden footer row, and used that to test for missing data and determine whether to hide a row. Another person used FindControl to see if the field was empty, and then set the CSS dynamically to hide the row if needed.)


1 protected void rptGrants_ItemDataBound(object sender, RepeaterItemEventArgs e)
2 {
3 DataRowView drv = (DataRowView)e.Item.DataItem;
4 if (drv["fieldName"].ToString() == "")
5 {
6 HtmlTableRow tRow = (HtmlTableRow)e.Item.FindControl("tableRowID");
7 tRow.Visible = false;
8 }
9 }


Again, I was using a repeater, and called a class method to bind the data to a DataTable. In the first line, we're accessing the data that is currently being bound to the repeater control - note that this event (ItemDataBound) is fired as the repeater iterates and binds to each row of your data. The item the repeater binds to is a DataRowView - your row of data has been turned into this DataRowView to give it more properties which enable the Repeater to do things like sort and filter, etc.

So, in line 2, I examine the specific column I'm interested in to see if there's anything there to display. Since this column is of varchar type, I check if it's empty.

Next I need to use the FindControl method to call the table row I want to hide, if the field I'm looking at is indeed empty.

That's all there was to it! Note that if my repeater had anything other than ItemTemplates or AlternateItemTemplates in it, I would have tested for the template type before the rest of my code.

References:
http://idunno.org/archive/2004/10/30/145.aspx
http://www.velocityreviews.com/forums/showpost.php?p=526696&postcount=4

More on DataRowView:
http://books.google.com/books?id=tKytuTyw3boC&pg=PA461&dq=datarowview&ei=d9pVStW2OIWQNvL5zPsJ