RSS feed icon BTB Shadow Man BiteTheBullet.co.uk logo
Feedback

Feedback

Tell me what you think about my website, I really want to know.

Your feedback is appreciated!

Page 1 2 3 4 5 6 7 8

DotNetNuke Random Image Tutorial - Part 5

PART 5
Creating the Edit control

Now in the code behind file I want to load the list with images we have stored in the database for this module.  I’ve replaced the existing Page_Load method with this one below and added a new method called DataBindList.

private void Page_Load(object sender, System.EventArgs e)
{
    try {
           if (!Page.IsPostBack) {
                   DataBindList();
                   lblImages.Text = Localization.GetString("lblImages.Text", LocalResourceFile);
           }
    }
    catch (Exception exc) {
         Exceptions.ProcessModuleLoadException(this, exc);
     }
} private void DataBindList()
{
     BTBRandomImageController objCtlBTBRandomImage = new BTBRandomImageController(); 
     ArrayList images = objCtlBTBRandomImage.GetByModules(this.ModuleId);
     lstImages.DataSource = images;
     lstImages.DataBind();
}

I’ve also set the following parameters of the lstImages control

DataTextField: ImageSrc

DataValueField: ImageID

The changes above will list the existing images stored in the database when we first load the control.

Next let’s add the function to delete items from the list, double click the image button we added to the control. This will generate a method stub for the click event. Here we need to get the id of the selected image and delete it from the database.

private void cmdDeleteImage_Click(object sender, 
                              System.Web.UI.ImageClickEventArgs e)
{
     string selectedImage = lstImages.SelectedValue;
     if(selectedImage != null && selectedImage != String.Empty){
         try
         {
              int imageId = Int32.Parse(selectedImage);
              BTBRandomImageController controller = new BTBRandomImageController();
              controller.Delete(imageId);
              DataBindList();
          }
         catch(FormatException ex)
         {
              //we've not got a valid int in the lstImage value
              Exceptions.ProcessModuleLoadException(this, ex);
          }
          catch(Exception ex)
          {
              Exceptions.ProcessModuleLoadException(this, ex);
           }
    }
}

Right, next I’ve removed the “Delete” link button and moved the other two link buttons so they are above just below the first table.

I’m going to add a panel called pnlAddImage then I’m going to move the first table and the two link buttons inside the newly created panel.
Set the panel’s visibility to false.

Next I’ve added another two link button just under the second table I’ve called the first one cmdDone with a text value of “Done”.  
The second one is called cmdAddImage with a text value of “Add Image”. Make sure both link buttons have a CssClass value of CommandButton, this will make the link style the same as the existing buttons on the user control.

Edit control in Visual Studio

Double click the Done button to create the click event stub. Then insert the code below.

Response.Redirect(Globals.NavigateURL(), true);

This will return the browser to the page we came from.

To the cmdAddImage click event we add this line of code

pnlAddImage.Visible = true;


The to the cmdCancel click event add

pnlAddImage.Visible = false;


This will display the panel containing the controls we need to show the user to select a new image.

 

Next we need to add a declaration for the ctlURL in your code behind,

protected DotNetNuke.UI.UserControls.UrlControl ctlURL;

Change the cmdUpdate click method to be like this.

private void cmdUpdate_Click(object sender, EventArgs e)
{
     try {
         if (Page.IsValid == true)
         {
              BTBRandomImageInfo objBTBRandomImage = new BTBRandomImageInfo();
              objBTBRandomImage = ((BTBRandomImageInfo)CBO.InitializeObject( objBTBRandomImage, typeof(BTBRandomImageInfo))); 
              int fileId = Int32.Parse(ctlURL.Url.Substring(7));
              FileController fileController = new FileController();
              FileInfo fi = fileController.GetFileById(fileId, this.PortalId);
              objBTBRandomImage.imageSrc = fi.Folder + fi.FileName;
              objBTBRandomImage.imageAlt = txtAlt.Text;
              objBTBRandomImage.moduleID = this.ModuleId;
              BTBRandomImageController objCtlBTBRandomImage = new BTBRandomImageController();
              objCtlBTBRandomImage.Add(objBTBRandomImage);
              DataBindList();
        }
    }
   catch (Exception exc) 
   {
         Exceptions.ProcessModuleLoadException(this, exc);
    }
}

Add a new using statement to the class shown as below

using DotNetNuke.Services.FileSystem;

 

<< Previous Page   Next Page >>

Privacy PolicyTerms and ConditionsCopyright © 2005 - 2012 BiteTheBullet.co.uk