I am trying to prevent a person from editing a form once it has already been approved by checking the approved date within a formview with the IsNullOrEmpty method. However, it is registering as NOT null/empty regardless of what is in the SQL Data Source. Here is the code:
if (!String.IsNullOrEmpty(frmChgRequest.FindControl("ApprovedDateLabel").ToString()))
{
lblErrorMessage.Text = "This Request has already been approved and cannot be edited.";
e.Cancel = true;
}
Since it is checking the Label in the Formview do I need to do this differently?
Thanks for your help!
Loading
Anna HawksPosted Jan 31, 2008, 9:13 AM
I was missing the set of parentheses including "(Label)" with the portion that used ".Text"
Thanks so much for your patience and assistance!
Guest UserPosted Jan 30, 2008, 11:06 PM
Anna- FindControl() doesn't return the text of the label, which is what you're after. It returns the Label control itself.
That's why you need to cast it as follows in order to get access to the properties of the Label control:
string s = ((System.Web.UI.WebControls.Label)(frmBlah.FindControl("blahblah"))).Text;
DateTime dt;
if (DateTime.TryParse(s, out dt)) { // date is present }
Anna HawksPosted Jan 30, 2008, 4:37 PM
It still gave me a false reading. It seems that the string assignment isn't working. Based on what I change, it gives me several errors:
1) 'System.Web.UI.Control' does not contain a definition for 'Text' - so I tried .ToString() and it went away
2) Cannot convert type 'string' to 'System.Web.UI.WebControls.TextBox' (or Label) so when I remove the (Label) or (TextBox) from the assignment, THAT goes away, too.
Once I got rid of those errors, it ran, but still called EVERYTHING a valid date. On debug, I get the value of s as 'System.Web.UI.WebControls.Label' instead of a string value. It should work because ApprovedDateLabel is bound to a DateTime Field in the Database.
dt has the value of MinValue when it is assigned and since s is not initializing as a valid string for it to evaluate, it's saying it is not a valid date regardless of whether or not it is. So it's allowing me to edit even when it has a valid date. Here's what I have now:
string s = (frmChgRequest.FindControl("ApprovedDateLabel")).ToString();
DateTime dt;
DateTime.TryParse(s, out dt);
if (dt != DateTime.MinValue)
{ // date is not present - OK to allow edit }
else
{ // date is present - Do not allow edit
lblErrorMessage.Text = "This Request has already been approved and cannot be edited.";
e.Cancel = true; }
I appreciate your patience, John!
Guest UserPosted Jan 30, 2008, 4:03 PM
This should work:
string s = (TextBox)(frmBlah.FindControl("blahblah")).Text;
DateTime dt;
if (DateTime.TryParse(s, out dt))
{
// date is present
}
If 's' contains a valid date string, the corresponding date value will be stored in dt. If not, then dt will be set to the constant DateTime.MinValue.
Anna HawksPosted Jan 30, 2008, 3:30 PM
I am not able to get the logic on the TryParse to work. Here's what I have, and I can't seem to make any of the valid overload methods fit:
string s = (frmChgRequest.FindControl("ApprovedDateLabel")).ToString();
if (DateTime.TryParse(s))
{
// date is present
}
Error: No overload for method 'TryParse' takes '1' arguments
Thanks again! I think you fixed it, but I just don't know how to use TryParse!
Guest UserPosted Jan 30, 2008, 2:58 PM
Also, FindControl() won't return the specific type of control, so you'll need to cast the value returned by it to TextBox or Label or whichever type of control you're using here.
What you should end up with is something like this:
string s = (TextBox)(frmBlah.FindControl("blahblah")).Text;
if (DateTime.TryParse(s))
{
// date is present
}