Results 1 to 6 of 6

Thread: accessing file input while in edit mode of datalist

  1. #1

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    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:
    1. If Len(Me.FileUpload.Value) <> 0 Then
    2.             objFile.Upload(Me.FileUpload.PostedFile)
    3.             objFile.Resize()
    4.             strFileName = objFile.NewFileName
    5.         Else
    6.             strFileName = "NA.gif"
    7.         End If
    When the fileInput control is in the datalist how do i access it ?

    thanks in advance

    Carl
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

  3. #3

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    Re: accessing file input while in edit mode of datalist

    nice one DJ

    It works like a dream thank you very much

    Carl
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  4. #4

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    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:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'hide all the administration buttons
    3.         If Session.Item("URights") = 1 Then
    4.             Dim btnEdit As Button '= now i have to find the control
    5.         End If
    6.  
    7.  
    8.         If Not Page.IsPostBack Then
    9.             loadarticle()
    10.         End If
    11.  
    12.  
    13.     End Sub
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  5. #5
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

  6. #6

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    Re: accessing file input while in edit mode of datalist

    thanks DJ your a star
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width