accessing file input while in edit mode of datalist
Hey All i am building an image gallery and i have used a datalist for repeating all the itmes in the gallery, when i want to update an image i have stuck a html:file input within the edit region of the datalist
How can i access the fileInput in the code behind
VB Code:
If Len(Me.FileUpload.Value) <> 0 Then
objFile.Upload(Me.FileUpload.PostedFile)
objFile.Resize()
strFileName = objFile.NewFileName
Else
strFileName = "NA.gif"
End If
When the fileInput control is in the datalist how do i access it ?
thanks in advance
Carl
Re: accessing file input while in edit mode of datalist
Well first you need button of some kind to postback the page and hence upload the file.
Best thing to do is use a simple button and set its CommandName to "Update". You can then use the UpdateCommand event to fire a method that will process the upload. You'll need to set the OnUpdateCommand property for the datalist to point to the right method.
Inside the UpdateCommand method you first need to grab the HtmlInputFile into a local variable using the DataListCommandEventArgs passed into the method and the FindControl method that will find the file upload by its control's ID. You can then check if a file was uploaded and process it accordingly.
In C# this would be roughly:
Code:
void blah_UpdateCommand(Object sender, DataListCommandEventArgs e) {
HtmlInputFile filupload = (HtmlInputFile) e.Item.FindControl("upload");
if (filupload.PostedFile.ContentLength > 0) {
// A file has been uploaded.
}
}
<datalist id="blah" OnUpdateCommand="blah_UpdateCommand">
<ItemTemplate>
Stuff in here...
</ItemTemplate>
<EditItemTemplate>
<input type="file" id="upload" />
<asp:Button id="update" CommandName="Update" Text="UPDATE!" />
</EditItemTemplate>
</datalist>
Hope that makes sense.
DJ
Re: accessing file input while in edit mode of datalist
nice one DJ
It works like a dream thank you very much
Carl
Re: accessing file input while in edit mode of datalist
DJ if i wanted to make a button visible / invisiable within the page load event
how would i set the DataListCommandEventArgs because the e is already decalred on the System.EventArgs
basically i only want administrators of the site to be able to edit content
so i was thinking when they log in i set a session variable with the users rights and when i load a page i check the session to see if its an administrator if so then i make the edit button visiable
some thing like
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'hide all the administration buttons
If Session.Item("URights") = 1 Then
Dim btnEdit As Button '= now i have to find the control
End If
If Not Page.IsPostBack Then
loadarticle()
End If
End Sub
Re: accessing file input while in edit mode of datalist
As the control is in an databound control you need to set the edit button visible/hidden during databinding not on page load.
Try:
Code:
<ItemTemplate>
<asp:Button id="btnEdit" runat="server" Text="EDIT!" CommandName="Edit" Visible=<%# Session.Item("URights") = 1 %> />
</ItemTemplate>
This will set the visible property to true or false on DataBind. Be careful with the quotes notice there aren't any around the data bound expression.
HTH
DJ
Re: accessing file input while in edit mode of datalist