HTMLInputFile control and validation.

A

Al Smith

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.
 
K

Kevin Spencer

Form1.Submit() will work fine.

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

Al Smith

Hi Kevin,

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

Thanks

Al
 
K

Kevin Spencer

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.
 
A

Al Smith

Thanks Kevin!

Kevin Spencer said:
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.

do
 
S

Steven Cheng[MSFT]

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.)
 
A

Al Smith

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
 
S

Steven Cheng[MSFT]

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top