Is it possible ot sub class the HtmlInputFile control?

B

Buddy Ackerman

I created this class

Public Class HTMLFileInput : Inherits System.Web.UI.HtmlControls.HtmlInputFile

Public Property Data As String
Get
Return ViewState("HTMLFileInput.Data")
End Get

Set (ByVal Value As String)
ViewState("HTMLFileInput.Data") = Value
End Set

End Property

End Class


Then I added this to my web page :
<%@ Register TagPrefix="custctrl" Namespace="MyNamespace" Assembly="MyNameSpace" %>

and this:

<custctrl:HTMLFileInput type="file" size="40" id="mycontrol"
data="<%# cType(Container.DataItem, XmlNode).Attributes.itemOf("mydata").innerText %>">



It rendered as this:

<custctrl:HTMLFileInput type="file" size="40" id="mycontrol" data="14">



What's the problem?




--Buddy
 
B

Bruce Barker

you need to add a runat=server, for the control to do the rendering

-- bruce (sqlwork.com)
 
D

Dave Fancher

Yes, you can. HtmlInputFile is not marked as NotInheritable (sealed in C#)
and has protected properties and methods. I must say though that I can't
see much reason for deriving a class from HtmlInputFile. You'd need to set
runat="server" for the control to render.

HtmlInputFile isn't really intended for data binding. What are you trying
to accomplish?

HTH
 
B

Buddy Ackerman

I am dynamically creating the file input boxes based on the number of records in the database for given parent record
there are two dropdown list boxes n the page that allow the selection of an artist and an album then the songs are
presented so that the user can upload the songs and samples for the a;blm. So, I need to know which file input goes
with what song so I store the songid in the new data property that I created by subclassing the HtmlInputFile control.
If there's a better way to do it I'd like to hear it.

BTW, I put the runat="server" in the tag (and added and end tag to it) and I keep getting a "The server tag is not well
formed." parse error.



--Buddy
 
G

Guest

As for the malformed server tag, you'll need to use either
<custctrl:HTMLFileInput ... /> or <custctrl:HTMLFileInput
....></custctrl:HTMLFileInput>

since ASP.NET pages are actually XHTML. Server tags need to be well-formed
but the runtime is pretty forgiving for client tags.

As for the rest of your issue, since it looks like you're dynamically
creating the controls in a repeater or datagrid, you should be able to just
reference the individual DataItem as you iterate through the Items
collection. Since DataItem is an instance of Object, you'll need to cast the
instance to the appropriate type depending on how you're doing the
databinding.

HTH
 
B

Buddy Ackerman

Having problems accessing the file input controls. File input controls are different from other controls in that they
have normal form control behavior and they also have posted file (which are accessed through separate interface i.e. the
Files collection). I have the songID in the data attribute that I created but when I access the Items collection of the
Repeater control the items aren't the input controls they are apparently the ItemTemplates (and there are two file input
controls be ItemTemplate). Anyway I need to not only access the input control to get the songid I also need to get the
actual PostedFile.


Here's what my ItemTemplate looks like:

<ItemTemplate>
<tr><td>Song Title:</td><td><b><%# cType(Container.DataItem, XmlNode).Attributes.itemOf("songTitle").innerText
%></b></td></tr>
<tr><td>Song File:</td><td><indie:HTMLFileInput type="file" size="40" id="songFile" Data=<%#
cType(Container.DataItem, XmlNode).Attributes.itemOf("songID").innerText %> runat="server"></indie:input></td></tr>
<tr><td>Song Sample:</td><td><indie:HTMLFileInput type="file" size="40" id="songSample" Data=<%#
cType(Container.DataItem, XmlNode).Attributes.itemOf("songID").innerText %> runat="server"></indie:input></td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
</ItemTemplate>

I need to get the file and ID of each file input control so I can post this info to my database and save the file to the
proper location.



--Buddy
 
G

Guest

Well, HtmlInputFile has the PostedFile property which is conceptually no
different than having a Value property or a Checked property.

Each "Item" in the Items collection of a Repeater is an instance of
RepeaterItem
[http://msdn.microsoft.com/library/d...mwebuiwebcontrolsrepeateritemmemberstopic.asp].
Since it is likely that you're iterating through the Items collection on
postback, as you iterate, you can use FindControl()
[http://msdn.microsoft.com/library/d...rfsystemwebuicontrolclassfindcontroltopic.asp]
to get a reference to your HtmlInputFile instance(s) based on the control's
ID (as specified in the template) (Yoou'll have to cast the control reference
returned by FindControl to HtmlInputFile) and use the RepeaterItems DataItem
property to get back to the songID in your data source.

HTH
 

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

Top