Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Browse button event - how can I update field with file selected? 1

Status
Not open for further replies.

AndyH1

Programmer
Jan 11, 2004
350
GB
I don't know if this is a asp.net or a javascript (or Ajax) problem but wonder if anyone can advise

Iv'e got a form for uploading files in which the user can enter a title and select a file (sometimes they want a more meaningful name than the filename)

<form ...>
<asp:textbox id="txtTitle" runat="server"></asp:textbox>
<input id="myFile" type="file" name="myFile" runat="server"/>
</form>

The myFile shows a browse button which allows you to go select a file (pretty straightforward so far)

What I'd like to do though is if the user uses the browse button to select a file and hasn't put in a title in the title field then the filename is put in the title field.

I tried using onChange on the myFile field
onchange="CheckTitleFilled()"

with

Private Sub CheckTitleFilled()
txtTitle.Text = myFile.Value
End Sub

but this doesn't seem to work (guess because its server side). Its easy to do this when you submit, but I'd like to do it dynamically when the user has selected a file, giving the user the choic of editing. Is there anyway of doing this - either in asp.net or javascript ?

AndyH1
 
You can't do it in ASP.NET unless you post the full form back (which in turn would remove the file name from the field input). I'd ask in the javascript forum to see how it could be done client-side.

Mark,

[URL unfurl="true"]http://lessthandot.com[/url] - Experts, Information, Ideas & Knowledge
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Website Design
[URL unfurl="true"]http://aspnetlibrary.com[/url] - An online resource for professional ASP.NET developers
 
Thanks ca8msm,

Think you are probably right. I was wondering about using the Ajax update panel, but think just javascript is probably best now
AndyH1
 
Thanks to feherke for providing the answer (under the Javascript forum)

Converting this to asp.net (as you need the Client IDs) it comes out as:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

myFile.Attributes.Add("onchange", "if(!this.form." + txtTitle.ClientID + ".value)this.form." + txtTitle.ClientID + ".value=this.value.replace(/.*\//,'')")
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top