Show and Hide a div using jQuery

You may find yourself wanting to show and hide a section of your page from a button or a link. This is very easy with a little jQuery. All you need to do is have a triggering object (an a tag in my example) and a target object (a div in my example) You need to firstly ensure that jQuery is included in your project and is referenced on your page or master page. I am using a Master Page so I add the reference to that I keep my jQuery library in a scripts folder so the reference in the head area looks like:
We then add some HTML into the content page (or MVC view in this case) like:

Show/Hide using jQuery

This is the box that is hidden and shown.
You can see the trigger a tag named showhidetrigger and the target div called showhidetarget and our jQuery script at the bottom. The $(document).ready(function () { line is telling jQuery to execute when the page is ready and everything is loaded. The $('#showhidetarget').hide(); line is hiding the target div initially And the $('a#showhidetrigger').click(function () { $('#showhidetarget').toggle(400); }); lines are telling the target div to toggle visible and invisible on the click event of the trigger a tag. The jQuery Toggle method includes a nice smooth transition as well which makes it look great. You can speed up or slow down the transition by increasing or decreasing the transition parameter.

Uploading files with ASP.NET MVC

For those who want to upload files using ASP.NET MVC will find that the process and objects you used in ASP.NET WebForms has changed slightly for ASP.NET MVC. MVC uses a HttpPostedFileBase object rather than the HttpPostedFile object that you may be used to. This is the code I used recently to enable file uploading in my MVC app. In the View:


and in the Controller:
public ActionResult Import(FormCollection form)
    foreach (string file in Request.Files)
        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
        if (hpf.ContentLength != 0)
            StreamReader sr = new StreamReader(hpf.InputStream);
            //Do Something with Stream
            return View("ImportFailed");
    return View("ImportSucceeded");

ASP.NET testing shortcut

I often find when developing an ASP.NET application that I want to set a number of variables when the application loads to make testing easier. I usually have a number of variables set in the Session_Start section of the global.asax.vb or in the Page_Load of the default.aspx page. This is a quick and easy way of setting things like the currently logged in user or other settings. The problem with doing this is that if you forget to comment out or remove these entries prior to uploading the site live you can run into some rather large problems. If found an excellent function in the Request.URL class that can help avoid this issue. The function is called IsLoopback. If you wrap  “If Request.URL.IsLoopback” around your variable setting code, it will only be run when you are running your code from within Visual Studio or from IIS using the http://localhostURL. Example:
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
     ' Fires when the session is started      Session(SessionVariables.UserID) = 1      Session(SessionVariables.CustomerID) = 2 End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
     ' Fires when the session is started
     If Request.URL.IsLoopback Then
          Session(SessionVariables.UserID) = 1
          Session(SessionVariables.CustomerID) = 2
     End If
End Sub
As you can see from the code, if you forget to remove or comment out this code when you upload it to your web server, the code will skip right over setting these variables. I would only recommend this type of variable setting in the initial development/testing phase and it should be removed once your application is put live.

Resetting a DotNetNuke Host user password

A frequent question asked by customers is how to reset the DotNetNuke Host users password if you can’t remember what it is. If you have access to the database this is a fairly easy process. Steps to reset a DNN Host password
  1. If you do not know the database details of your DNN database, login to your account via FTP and view the web.config file in the root of your DNN application. You can get the Server name, db name, username and password from the connectionstring property.
  2. Open SQL Management Studio and connect to your SQL Server using the information from step 1.
  3. If you have set your registration to none, private or verified then you’ll need to edit the Portals table and change the UserRegistration field to 2. This will set the registration setting to Public.
  4. Open your site and click on the register button, then register a new user.
  5. Open the Users table and find the record of the new user you just added at the bottom. Set the IsSuperUser field to True (or 1 for SQL 2000).
  6. Login as your newly created user and you should see the Host and Admin menus. Click on the Host, Host Accounts link then click on the All link on the user list.
  7. You should now see the Host user listed. You can click on the pencil edit button on the left of the user to reset the Host users password.
  8. If you no longer need the newly created user you should logout and log back in as Host and delete it. You can then also login as Admin to your portal and set your registration setting back to what it was previously.