Create FileUpload from code behind - asp.net

I need to create a FileUpload control from the code behind page. I have this function:
Private Function CreateUpload() As FileUpload
Dim txtFileUpload As New FileUpload
txtFileUpload.ID = "element1"
Return txtFileUpload
End Function
However, even though I successfully use a similar technique for other form controls, my FormUpload doesn't get rendered to the page.
Can someone please explain what I'm doing wrong.
Edit: I'm writing the returned FileUpload to a DIV on the page outside of my function.
Update: It's sorted. Seemed to be a problem with Visual Studio. Closing it, relaunching and then rebuilding the website sorted it. Thanks to all who responded.
Thanks.

I can't see you adding this control to the WebForm. Try adding it:
Private Function CreateUpload() As FileUpload
Dim txtFileUpload As New FileUpload
txtFileUpload.ID = "element1"
Page.Controls.Add(txtFileUpload)
Return txtFileUpload
End Function
or if you don't have access to the Page inside this function add it when you call the function in the code behind of your webform:
Dim txtFileUpload As FileUpload = CreateUpload()
Page.Controls.Add(txtFileUpload)

Related

OpenFileDialog in Webform Application

I tried to use OpenFileDialog() control inside my Webform Application but it's not working.
I tried this coding but it gave me error :
System.Windows.Forms.OpenFileDialog Not defined
Private Sub FnOpenFileDialog()
Dim openfile As New System.Windows.Forms.OpenFileDialog
openfile.Filter = String.Format("Image file (*.jpg)|*.jpg")
openfile.Multiselect = True
openfile.ShowDialog()
End Sub
Private Sub btnUpload_PreRender(sender As Object, e As EventArgs) Handles btnUpload.PreRender
Dim objThread As New Thread(AddressOf FnOpenFileDialog)
objThread.IsBackground = True
objThread.SetApartmentState(ApartmentState.STA)
objThread.Start()
End Sub
Or is there any way on how to do it? I don't want to use FileUpload control because it shows textbox + button. I only want to show the Button() control.
I refer to this website How to Apply OpenFileDialog Function to a WebApplication
In a ASP.NET web application, the VB code is run on the web server. So even if you would succeed in using OpenFileDialog that would result in the dialog being showed on the server, not in the browser.
JavaScript won't help you either, as the browswer sandbox in which JavaScripts run doesn't allow access to the file system (which is good from a security perspective). You simply have to accept that you need to work with the file upload control. You might be able to customize the appearance a bit by applying styles through css.
Use HTML input file ,as follow:
<input id="File1" type="file" />

composite control renders after i need to use one of the properties (control lifecycle)

I have created a composite control for a file upload input (using an HtmlInputFile control). My problema is that everything works fine until I try to save the file using HtmlInputFile's "SaveAs" in my page's code behind. Whenever I press the button for uploading the file, I get a "object not set to an instance bla bla bla". However if I pause execution right before the SaveAs (with a break point) and then got step by step (F10), all of the composite properties have the corresponding data and I can effectively upload the file.
I am quite sure this has to do with page and control lifecycle, but everything I have read so far has led me no where. Can anyone please shed some light as to how I can force the composite control to load/render before I execute the code behind Click event?
The code behind that triggers the SaveAs is pretty straight forward:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
fup.xmsPostedFile.SaveAs(Server.MapPath(String.Format("~/memberpages/{0}", fup.xmsFileName)))
End Sub
The fup.xmsPostedFile property only references the _inputControl.PostedFile property. Same with fup.xmsFileName.
any help will be greatly appreciated. Thanks in advanced.
Finally got it to work. Turns out I was missing the EnsureChildControls(). I changed the property for this:
Public ReadOnly Property xmsPostedFile() As HttpPostedFile
Get
Return _inputFile.PostedFile
End Get
End Property
to this
Public ReadOnly Property xmsPostedFile() As HttpPostedFile
Get
EnsureChildControls()
Return _inputFile.PostedFile
End Get
End Property
and it started working as expected.

get HiddenField Value from javascript to use in code-behind WITHOUT A BUTTON NOR POSTBACK

I have an html page that has a link to an external javascript file namely external.js as follows
<script type =" text/javascript" src="external.js"></script>
The page includes an <asp:HiddenField> defined as Hidden1.
In my external .js file there is a function passData() which basically populates Hidden1 with some values. The way I do it is as follows.
On HTML
<script> passData("<%=Hidden1.ClientID%>") </script> then in external.js:
function passData(hiddenFieldID)
{
document.getElementByID(hiddenFieldID).Value = "Value";
}
What I have been trying for some while now is to get the value of Hidden1 from my code behind in vb.net by using:
Dim str = Hidden1.value
However, with no luck. I have noticed that I should perform some sort of postback so that the value can be taken. However, what I am thinking of is that how on my html page the value of Hidden1 is showing when I use alert and at the same time I am not able to retrieve it on page_load in my .aspx unless I perform a postback. In my application I am really eager to get it working without having the need to add a button to initialize postbacks. I need to get Hidden1 value in my .aspx code-behind page in order to store it in a database. Any suggestions or ideas ?

How can I programmatically modify an aspx file content before the handler handle it?

The reason I need to modify the content of an aspx (not physically, but change the content in the memory) is because there are certain custom tags I made needs to be parsed to the correct data before the entire aspx is handled by the HttpHandler.
Is there a way for that?
You can use Response Filters (HttpFilter) and modify content on the fly, basically after response is formed, before EndRequest your filter is called (it's a stream descendant) and you can modify it as you wish. In the HttpModule, Init method you got to install HttpFilter (Response.Filter) and it will be called for that request.
Here is a good article :
http://aspnetresources.com/articles/HttpFilters
UPDATE:
Maybe this is a case of XY Problem, and you can solve your problem with simple server control that will render these custom tags properly.
you can use the Render event
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim sw As New System.IO.StringWriter
Dim hw As New HtmlTextWriter(sw)
MyBase.Render(hw)
Dim html As String = sw.ToString()
' html = html.Replace() etc to change your html code in here
writer.Write(html)
End Sub
EDIT i see you want to inject mark up dynamically before asp.net handles the aspx, maybe the FileLevelPageControlBuilder Class is of use

ASP.NET: Pass value from User Control to page?

I am creating a user control in ASP.NET (using VB) that uses the autocomplete ajax control on a textbox to get a value. Then I want the page to post back and run some code according to whatever value is passed to it from this control. Problem is, I'm not exactly sure how to do this. I'm sure it's easy and I should know, but I don't.
Thanks in advance!
In your user control expose a property for the value
Public Property SomeValue() As String
Get
Return textbox1.Text
End Get
End Property
Then in your aspx page load, just reference the user control's value.
userControl1.SomeValue
Edit, I just tried changing my syntax to vb.net, I don't actually know vb, so that syntax may or may not be right.
((NameOfPage)this.Page).VariableOnPage = this.Foobar;
In the code-behind on your user-control expose a property e.g.
public TextBox UserControlTextBox
{
return this.TextBoxInstance;
}
Then from you page just call
UserControlInstance.UserControlTextBox.Text;

Resources