|
Rank: Advanced Member Groups: Member
Joined: 3/18/2008 Posts: 41
|
Hello.
Is it possible to use the validation controls in Visual Studio to validate that the user has uploaded a file when user submits the page? Like validation of textboxes, dropdownboxes and so on, the validation controls checks for empty fields.
How is the best way to validate that a file is uploaded using the Visual Studio validation controls?
Regards, HT
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Hi, You won't be able to use standard validator with our uploader, however you can easily call the uploader's getPostedFiles function to check whether a file has been uploaded. The easiest way to do this is to call it when the form is to be submitted:
Code: C#
private void Page_Load(object sender, System.EventArgs e)
{
this.RegisterOnSubmitStatement("validate_uploader",
"if (!AJAXUploader1.getPostedFiles().length) return false;");
}
The above code would cancel submit when nothing has been uploaded. Thanks
|
|
Rank: Advanced Member Groups: Member
Joined: 3/18/2008 Posts: 41
|
Thank you. Based on this, I also found this way to solve it: In Page_Load(..):
Code: C#
Page.ClientScript.RegisterStartupScript(this.GetType(), "OnLoad", "<script language=\"javascript\" type=\"text/javascript\">funcion validate_uploader { if (AJAXUploader1.PostedFiles.Length == 0) return false; }</script>");
And then: CustomValidator with the
Code: C#
ClientValidationFunction = "validate_uploader"
Thanks.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
This is cool. Thanks for sharing!
|
|
Rank: Advanced Member Groups: Member
Joined: 3/18/2008 Posts: 41
|
Hm.. I maybe was a little to quick. Are you sure it works?
Code: C#
private void Page_Load(object sender, System.EventArgs e)
{
this.RegisterOnSubmitStatement("validate_uploader",
"if (!AJAXUploader1.getPostedFiles().length) return false;");
}
First of all, my component does not have a getPostedFiles() function, but I guess the PostedFile attribute will do the same:
Code: C#
private void Page_Load(object sender, System.EventArgs e)
{
this.RegisterOnSubmitStatement("validate_uploader", "if (AJAXUploaderIntroductionPicture.PostedFiles.Length == 0) return false;");
}
But should this be all I have to do? When I click the submit button, the page performs a postback. So the validation does not seem to work. Maybe I misunderstood something ... is the name "validate_uploader" you choose interesting here? Should I call this key from anywhere else? Regards, HT
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
No. getPostedFile is a client side function: http://www.essentialobjects.com/ViewDoc.aspx?t=JSDoc.Public.AJAXUploader.getPostedFiles.htmlIt didn't exist from in the first version though. So check your version, and if it is not the latest, try to upgrade to the latest.
|
|
Rank: Advanced Member Groups: Member
Joined: 3/18/2008 Posts: 41
|
It works:-) Thanks! This is my solution to anyone who want to valiate that a file is uploaded before a submit: Javascript:
Code: JavaScript
function ValidateAjaxUploadPicture(oSrc, args)
{
if (ctl00_ContentPlaceHolder1_AJAXUploaderIntroductionPicture.getPostedFiles().length==0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
CustomValidator control in Visual Studio: <asp:CustomValidator ID="CustomValidatorAJAXUploaderField4" runat="server" ClientValidationFunction="ValidateAjaxUploadPicture" Text="*"></asp:CustomValidator> You can replace "ctl00_ContentPlaceHolder1_AJAXUploaderIntroductionPicture" with eo_GetObject('<%=AJAXUploaderIntroductionPicture.ClientID%>') if you do not wish to hardcode the ID of the uploader control.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Perfect! Thanks for sharing!
|
|