Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HTMLInputFile control and validation.

Reply
Thread Tools

HTMLInputFile control and validation.

 
 
Al Smith
Guest
Posts: n/a
 
      01-20-2004
Newbee to aspx needs direction.

We are using an <INPUT type="file" tag to upload a file. We also have a
text field for the user to enter a description for the file which the user
must enter. Server side validation was being done for the description field
having length and if not an appropriate message was written back to the
client. However, in the process, the filename the user enters is not
persisted between server calls (HTMLInputFile control). Also after some
research I found where the "value" property for the <INPUT type="file" tag
is read only for security purposes. Soo, I thought I would just do client
side validation to make sure the description field was filled in. Now my
problem is how to raise the appropriate event back on the server. The code
below shows my issue.

The original code. When clicked, the btnUpload_Click event on the server is
raised and the description field being filled in was validated. If not
filled in, the user supplied value of the <INPUT type="file" is lost back on
the client.


<INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
runat="server">
<INPUT class="txt" type="text" size="35" id="DocName" name="DocName"
maxlength="50" runat="server">
<input type="button" id="btnUpload" class="btn" value="Upload"
OnServerClick="btnUpload_Click" NAME="btnUpload" runat="server">


Soo, I changed the btnUpload to call JS on the client as shown next.

<INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
runat="server">
<INPUT class="txt" type="text" size="35" id="DocName" name="DocName"
maxlength="50" runat="server">
<input type="button" id="btnUpload" class="btn" value="Upload"
OnClick="btnUpload_Click()" NAME="btnUpload">

and added the JS:
<script LANGUAGE="javascript" >
<!--
function btnUpload_Click(){
if ( Form1.DocName.value.length == 0 ){
alert("Fill in a description.");
}
else{
Form1.submit();
}
}
//
-->
</script>

What I do not know how to do is in the else. I don't want to just do a
"Form1.submit()", I would like to simulate the aspx way and effectively
raise the btnUpload_Click event on the server or some other server sub.


 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      01-20-2004
Form1.Submit() will work fine.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Al Smith" <> wrote in message
news:#...
> Newbee to aspx needs direction.
>
> We are using an <INPUT type="file" tag to upload a file. We also have a
> text field for the user to enter a description for the file which the user
> must enter. Server side validation was being done for the description

field
> having length and if not an appropriate message was written back to the
> client. However, in the process, the filename the user enters is not
> persisted between server calls (HTMLInputFile control). Also after some
> research I found where the "value" property for the <INPUT type="file"

tag
> is read only for security purposes. Soo, I thought I would just do client
> side validation to make sure the description field was filled in. Now my
> problem is how to raise the appropriate event back on the server. The

code
> below shows my issue.
>
> The original code. When clicked, the btnUpload_Click event on the server

is
> raised and the description field being filled in was validated. If not
> filled in, the user supplied value of the <INPUT type="file" is lost back

on
> the client.
>
>
> <INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
> runat="server">
> <INPUT class="txt" type="text" size="35" id="DocName" name="DocName"
> maxlength="50" runat="server">
> <input type="button" id="btnUpload" class="btn" value="Upload"
> OnServerClick="btnUpload_Click" NAME="btnUpload" runat="server">
>
>
> Soo, I changed the btnUpload to call JS on the client as shown next.
>
> <INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
> runat="server">
> <INPUT class="txt" type="text" size="35" id="DocName" name="DocName"
> maxlength="50" runat="server">
> <input type="button" id="btnUpload" class="btn" value="Upload"
> OnClick="btnUpload_Click()" NAME="btnUpload">
>
> and added the JS:
> <script LANGUAGE="javascript" >
> <!--
> function btnUpload_Click(){
> if ( Form1.DocName.value.length == 0 ){
> alert("Fill in a description.");
> }
> else{
> Form1.submit();
> }
> }
> //
> -->
> </script>
>
> What I do not know how to do is in the else. I don't want to just do a
> "Form1.submit()", I would like to simulate the aspx way and effectively
> raise the btnUpload_Click event on the server or some other server sub.
>
>



 
Reply With Quote
 
 
 
 
Al Smith
Guest
Posts: n/a
 
      01-20-2004
Hi Kevin,

Although Form1.Submit() works, can I call a specific routine on the server?

Thanks

Al


"Kevin Spencer" <> wrote in message
news:#...
> Form1.Submit() will work fine.
>
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
>
> "Al Smith" <> wrote in message
> news:#...
> > Newbee to aspx needs direction.
> >
> > We are using an <INPUT type="file" tag to upload a file. We also have

a
> > text field for the user to enter a description for the file which the

user
> > must enter. Server side validation was being done for the description

> field
> > having length and if not an appropriate message was written back to the
> > client. However, in the process, the filename the user enters is not
> > persisted between server calls (HTMLInputFile control). Also after some
> > research I found where the "value" property for the <INPUT type="file"

> tag
> > is read only for security purposes. Soo, I thought I would just do

client
> > side validation to make sure the description field was filled in. Now

my
> > problem is how to raise the appropriate event back on the server. The

> code
> > below shows my issue.
> >
> > The original code. When clicked, the btnUpload_Click event on the

server
> is
> > raised and the description field being filled in was validated. If not
> > filled in, the user supplied value of the <INPUT type="file" is lost

back
> on
> > the client.
> >
> >
> > <INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
> > runat="server">
> > <INPUT class="txt" type="text" size="35" id="DocName" name="DocName"
> > maxlength="50" runat="server">
> > <input type="button" id="btnUpload" class="btn" value="Upload"
> > OnServerClick="btnUpload_Click" NAME="btnUpload" runat="server">
> >
> >
> > Soo, I changed the btnUpload to call JS on the client as shown next.
> >
> > <INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
> > runat="server">
> > <INPUT class="txt" type="text" size="35" id="DocName" name="DocName"
> > maxlength="50" runat="server">
> > <input type="button" id="btnUpload" class="btn" value="Upload"
> > OnClick="btnUpload_Click()" NAME="btnUpload">
> >
> > and added the JS:
> > <script LANGUAGE="javascript" >
> > <!--
> > function btnUpload_Click(){
> > if ( Form1.DocName.value.length == 0 ){
> > alert("Fill in a description.");
> > }
> > else{
> > Form1.submit();
> > }
> > }
> > //
> > -->
> > </script>
> >
> > What I do not know how to do is in the else. I don't want to just do a
> > "Form1.submit()", I would like to simulate the aspx way and effectively
> > raise the btnUpload_Click event on the server or some other server sub.
> >
> >

>
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      01-20-2004
> Although Form1.Submit() works, can I call a specific routine on the
server?

Sure, in the Page_Load Sub. Just check to see whether a file was uploaded.

I think it's great that you want to do this "the aspx way" (as you put it).
However, in this case, you're not violating any Best Practices. Event
Handlers are good for handling events, and you could programmatically click
a button to invoke a server-side event handler, but in this case, it would
add unnecessary processing to do so.



--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Al Smith" <> wrote in message
news:#...
> Hi Kevin,
>
> Although Form1.Submit() works, can I call a specific routine on the

server?
>
> Thanks
>
> Al
>
>
> "Kevin Spencer" <> wrote in message
> news:#...
> > Form1.Submit() will work fine.
> >
> > --
> > HTH,
> > Kevin Spencer
> > .Net Developer
> > Microsoft MVP
> > Big things are made up
> > of lots of little things.
> >
> > "Al Smith" <> wrote in message
> > news:#...
> > > Newbee to aspx needs direction.
> > >
> > > We are using an <INPUT type="file" tag to upload a file. We also

have
> a
> > > text field for the user to enter a description for the file which the

> user
> > > must enter. Server side validation was being done for the description

> > field
> > > having length and if not an appropriate message was written back to

the
> > > client. However, in the process, the filename the user enters is not
> > > persisted between server calls (HTMLInputFile control). Also after

some
> > > research I found where the "value" property for the <INPUT type="file"

> > tag
> > > is read only for security purposes. Soo, I thought I would just do

> client
> > > side validation to make sure the description field was filled in. Now

> my
> > > problem is how to raise the appropriate event back on the server. The

> > code
> > > below shows my issue.
> > >
> > > The original code. When clicked, the btnUpload_Click event on the

> server
> > is
> > > raised and the description field being filled in was validated. If

not
> > > filled in, the user supplied value of the <INPUT type="file" is lost

> back
> > on
> > > the client.
> > >
> > >
> > > <INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
> > > runat="server">
> > > <INPUT class="txt" type="text" size="35" id="DocName" name="DocName"
> > > maxlength="50" runat="server">
> > > <input type="button" id="btnUpload" class="btn" value="Upload"
> > > OnServerClick="btnUpload_Click" NAME="btnUpload" runat="server">
> > >
> > >
> > > Soo, I changed the btnUpload to call JS on the client as shown next.
> > >
> > > <INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
> > > runat="server">
> > > <INPUT class="txt" type="text" size="35" id="DocName" name="DocName"
> > > maxlength="50" runat="server">
> > > <input type="button" id="btnUpload" class="btn" value="Upload"
> > > OnClick="btnUpload_Click()" NAME="btnUpload">
> > >
> > > and added the JS:
> > > <script LANGUAGE="javascript" >
> > > <!--
> > > function btnUpload_Click(){
> > > if ( Form1.DocName.value.length == 0 ){
> > > alert("Fill in a description.");
> > > }
> > > else{
> > > Form1.submit();
> > > }
> > > }
> > > //
> > > -->
> > > </script>
> > >
> > > What I do not know how to do is in the else. I don't want to just do

a
> > > "Form1.submit()", I would like to simulate the aspx way and

effectively
> > > raise the btnUpload_Click event on the server or some other server

sub.
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Al Smith
Guest
Posts: n/a
 
      01-20-2004
Thanks Kevin!

"Kevin Spencer" <> wrote in message
news:...
> > Although Form1.Submit() works, can I call a specific routine on the

> server?
>
> Sure, in the Page_Load Sub. Just check to see whether a file was uploaded.
>
> I think it's great that you want to do this "the aspx way" (as you put

it).
> However, in this case, you're not violating any Best Practices. Event
> Handlers are good for handling events, and you could programmatically

click
> a button to invoke a server-side event handler, but in this case, it would
> add unnecessary processing to do so.
>
>
>
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
>
> "Al Smith" <> wrote in message
> news:#...
> > Hi Kevin,
> >
> > Although Form1.Submit() works, can I call a specific routine on the

> server?
> >
> > Thanks
> >
> > Al
> >
> >
> > "Kevin Spencer" <> wrote in message
> > news:#...
> > > Form1.Submit() will work fine.
> > >
> > > --
> > > HTH,
> > > Kevin Spencer
> > > .Net Developer
> > > Microsoft MVP
> > > Big things are made up
> > > of lots of little things.
> > >
> > > "Al Smith" <> wrote in message
> > > news:#...
> > > > Newbee to aspx needs direction.
> > > >
> > > > We are using an <INPUT type="file" tag to upload a file. We also

> have
> > a
> > > > text field for the user to enter a description for the file which

the
> > user
> > > > must enter. Server side validation was being done for the

description
> > > field
> > > > having length and if not an appropriate message was written back to

> the
> > > > client. However, in the process, the filename the user enters is

not
> > > > persisted between server calls (HTMLInputFile control). Also after

> some
> > > > research I found where the "value" property for the <INPUT

type="file"
> > > tag
> > > > is read only for security purposes. Soo, I thought I would just do

> > client
> > > > side validation to make sure the description field was filled in.

Now
> > my
> > > > problem is how to raise the appropriate event back on the server.

The
> > > code
> > > > below shows my issue.
> > > >
> > > > The original code. When clicked, the btnUpload_Click event on the

> > server
> > > is
> > > > raised and the description field being filled in was validated. If

> not
> > > > filled in, the user supplied value of the <INPUT type="file" is lost

> > back
> > > on
> > > > the client.
> > > >
> > > >
> > > > <INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
> > > > runat="server">
> > > > <INPUT class="txt" type="text" size="35" id="DocName"

name="DocName"
> > > > maxlength="50" runat="server">
> > > > <input type="button" id="btnUpload" class="btn" value="Upload"
> > > > OnServerClick="btnUpload_Click" NAME="btnUpload" runat="server">
> > > >
> > > >
> > > > Soo, I changed the btnUpload to call JS on the client as shown next.
> > > >
> > > > <INPUT type="file" id="File1" NAME="File1" size="35" class="txt"
> > > > runat="server">
> > > > <INPUT class="txt" type="text" size="35" id="DocName"

name="DocName"
> > > > maxlength="50" runat="server">
> > > > <input type="button" id="btnUpload" class="btn" value="Upload"
> > > > OnClick="btnUpload_Click()" NAME="btnUpload">
> > > >
> > > > and added the JS:
> > > > <script LANGUAGE="javascript" >
> > > > <!--
> > > > function btnUpload_Click(){
> > > > if ( Form1.DocName.value.length == 0 ){
> > > > alert("Fill in a description.");
> > > > }
> > > > else{
> > > > Form1.submit();
> > > > }
> > > > }
> > > > //
> > > > -->
> > > > </script>
> > > >
> > > > What I do not know how to do is in the else. I don't want to just

do
> a
> > > > "Form1.submit()", I would like to simulate the aspx way and

> effectively
> > > > raise the btnUpload_Click event on the server or some other server

> sub.
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-21-2004
Hi Al,


Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you're looking for some information on how to post
back a page via client script(such as javasciprt) and also use some other
ways besides "form.submit()", such as call a server control's postback
event?
If there is anything I misunderstood, please feel free to let me know.

As for this problem, firstly I should confirm that Kevin has provided the
very good explanation on submit a form and the mechanism when doing work on
the page's post back events. Here I'll provide you some additional
suggestion:

In addition, we could also use javascript to call a servercontrol's
postback event. In fact, all the server controls such as an ASP.NET Button,
when it is output in the response page to client, it is rendered as a
normal html element, normally it'll be rendered as a <input type=submit />
or <input type=button /> , yes , just a html button. And as we know, in
javascript, we can manually fire a html element's DHTML event . For example
if there is a html button as:
<input type=button id="btnTest" value="click me to post back" />

Then, we can use the below javascript code to fire its click event:
btnTest.click();

And as for the problem in our issue, you can also use the above code to
fire a server controls's post back event. Here is a simple sample to show
this way:

--------------------------------------------aspx
file---------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>PostBack</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function doCheck()
{
if(document.all("txtPost").value.length > 4)
{
document.all("btnPost").click();
}
else
{
alert("input text is not enought!");
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><INPUT id="txtPost" type="text" name="txtPost" runat="server"
onchange="doCheck()"></td>
<td><INPUT id="btnPost" type="button" value="Button" name="btnPost"
runat="server" style="display:none"></td>
</tr>
<tr>
<td><FONT face="ËÎÌå"></FONT></td>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
--------------------------------------------------code-behind page
class------------------------------------------------------------
public class PostBack : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton btnPost;
protected System.Web.UI.HtmlControls.HtmlInputText txtPost;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnPost.ServerClick += new
System.EventHandler(this.btnPost_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnPost_ServerClick(object sender, System.EventArgs e)
{
Response.Write("<br>Page is post back at: "+
System.DateTime.Now.ToLongTimeString());
}
}
---------------------------------------------------------------------------

Please check out my suggestion to see whether it helps. If you have any
questions or need any assistance, please feel free to post here.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


 
Reply With Quote
 
Al Smith
Guest
Posts: n/a
 
      01-21-2004
Hi Steven,

The reason for the original post was to figure out if my research was valid
and if so I was looking for a work around. The research I found was the
"value" property for the <INPUT type="file"...> tag is read only and it is
read only for security purposes. I assume that is a true statement and it
seems logical.

Both your suggestions and Kevin's have helped in my understanding of aspnet
and ideas for a work around.

Thanks very much!

Al

"Steven Cheng[MSFT]" <v-> wrote in message
news:...
> Hi Al,
>
>
> Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
> assisting you on this issue.
> From your description, you're looking for some information on how to post
> back a page via client script(such as javasciprt) and also use some other
> ways besides "form.submit()", such as call a server control's postback
> event?
> If there is anything I misunderstood, please feel free to let me know.
>
> As for this problem, firstly I should confirm that Kevin has provided the
> very good explanation on submit a form and the mechanism when doing work

on
> the page's post back events. Here I'll provide you some additional
> suggestion:
>
> In addition, we could also use javascript to call a servercontrol's
> postback event. In fact, all the server controls such as an ASP.NET

Button,
> when it is output in the response page to client, it is rendered as a
> normal html element, normally it'll be rendered as a <input type=submit />
> or <input type=button /> , yes , just a html button. And as we know, in
> javascript, we can manually fire a html element's DHTML event . For

example
> if there is a html button as:
> <input type=button id="btnTest" value="click me to post back" />
>
> Then, we can use the below javascript code to fire its click event:
> btnTest.click();
>
> And as for the problem in our issue, you can also use the above code to
> fire a server controls's post back event. Here is a simple sample to show
> this way:
>
> --------------------------------------------aspx
> file---------------------------------------------
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
> <HTML>
> <HEAD>
> <title>PostBack</title>
> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" Content="C#">
> <meta name="vs_defaultClientScript" content="JavaScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> <script language="javascript">
> function doCheck()
> {
> if(document.all("txtPost").value.length > 4)
> {
> document.all("btnPost").click();
> }
> else
> {
> alert("input text is not enought!");
> }
> }
> </script>
> </HEAD>
> <body>
> <form id="Form1" method="post" runat="server">
> <table width="500" align="center">
> <tr>
> <td><INPUT id="txtPost" type="text" name="txtPost" runat="server"
> onchange="doCheck()"></td>
> <td><INPUT id="btnPost" type="button" value="Button" name="btnPost"
> runat="server" style="display:none"></td>
> </tr>
> <tr>
> <td><FONT face="ËÎÌå"></FONT></td>
> <td></td>
> </tr>
> </table>
> </form>
> </body>
> </HTML>
> --------------------------------------------------code-behind page
> class------------------------------------------------------------
> public class PostBack : System.Web.UI.Page
> {
> protected System.Web.UI.HtmlControls.HtmlInputButton btnPost;
> protected System.Web.UI.HtmlControls.HtmlInputText txtPost;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
>
> }
>
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
>
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.btnPost.ServerClick += new
> System.EventHandler(this.btnPost_ServerClick);
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
> #endregion
>
> private void btnPost_ServerClick(object sender, System.EventArgs e)
> {
> Response.Write("<br>Page is post back at: "+
> System.DateTime.Now.ToLongTimeString());
> }
> }
> --------------------------------------------------------------------------

-
>
> Please check out my suggestion to see whether it helps. If you have any
> questions or need any assistance, please feel free to post here.
>
>
>
> Regards,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-23-2004
Hi Al,

Thanks for your response. I'm glad that my suggestion has been helpful to
you. And if you need any further assistance on this issue. Please feel free
to let me know. I'll be willing to help you.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HtmlInputFile control looses information after post back. shimonsim@talamus.com ASP .Net 4 01-19-2004 03:05 AM
HtmlInputFile control is not responsive!! Frank ASP .Net 0 11-21-2003 10:28 PM
Cant set value of HTMLInputFile control Mark ASP .Net 2 11-09-2003 07:37 PM
HtmlInputFile control and Postback Jeff Richardson ASP .Net Web Controls 0 10-01-2003 04:12 PM
Re: HELP - HtmlInputFile Server Control bruce barker ASP .Net 0 07-17-2003 04:44 PM



Advertisments