Ugh, that was annoying. I needed to modify this line to also look for parens:
var re = new RegExp("^[a-zA-Z\.\'\,\-]*$", "g");
So I added it to the end, like so:
var re = new RegExp("^[a-zA-Z\.\'\,\-\(\)]*$", "g");
Let me add that I had already modified the server sie validation written in C# which looked almost the same, with no problem. But when I ran the page, the js choked and I got this error in the Error Console of my FF web dev toolbar (because I haven't tried Firebug yet...I know, what is wrong with me?)
"invalid range in character class"
What was this odd error? As far as I can tell, js does not like it if the last character before the ] in my RegExp expression is either ( or ). I moved them elsewhere, several elsewheres, just to see, and the error went away.
Ugh.
Tuesday, July 28, 2009
Monday, July 20, 2009
programatically change gridview row background color
//change bgcolor of gridview row based on data
//in this case the data I am testing is in a templatefield that is hidden
//if the data had been added in the last hour, then I apply a certain css class
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime entryDate = Convert.ToDateTime(((DataRowView)e.Row.DataItem)["DtEnter"]);
DateTime nowDate = DateTime.Now;
TimeSpan ts = nowDate.Subtract(entryDate);
if (ts.TotalMinutes <= 60)
{
e.Row.CssClass = "updatedRow";
}
}
}
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.)
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
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
Subscribe to:
Posts (Atom)