Monday, February 25, 2008

HOW TO REDIRECT TO SPECIFIC CONTROL OF SPECIFIC TAB IN DOTNETNUKE?

As every newbie starting the DotNetNuke development has always a question like this in their lives. The basic idea behind doing this is:

  • We need TABID for redirection to specific TAB. we will pass a query string parameter along with the redirect URL to tell which control to load.
  • At the default control of that specific module, we will just read the query string and load .ASCX control accordingly into a placeholder control.

I’m trying to make sure how easily we can do this using an example.

STEP 1: Create a default control

Create a control called _default in your root directory which looks like this

_DEFAULT.ASCX

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="_Default.ascx.cs" Inherits=" _Default" %>

<table id="Table1" cellspacing="0" cellpadding="0" width="100%" align="center" border="0">

<tr>

<td valign="top" align="center">

<p align="center">

<asp:PlaceHolder ID="phMain" runat="server">asp:PlaceHolder>

p>

td>

tr>

<tr>

<td valign="top" align="center">

<asp:Label ID="lblModuleSettings" runat="server" resourcekey="lblModuleSettings"

ForeColor="Red" Visible="False">Please update module settings...Portal Admin.asp:Label>td>

tr>

table>

_DEFAULT.ASCX.CS

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using DotNetNuke;

using DotNetNuke.Common;

using DotNetNuke.Common.Utilities;

using DotNetNuke.Entities.Modules;

using DotNetNuke.Services.Exceptions;

using DotNetNuke.Services.Localization;

namespace

{

partial class _Default : PortalModuleBase

{

private string m_ModuelControl = "Contacts.ascx";//1. Default Control to Load

protected void Page_Load(object sender, EventArgs e)

{

LoadModuleControl();//2. Call Module control Loading Logic

}

private void LoadModuleControl()

{

if (Request.QueryString["mctl"] != null) //3. Read control name from querystring

{

m_ModuelControl = Request.QueryString["mctl"].ToString()) + ".ascx";

}

//4. check authorization here if you want. Otherwise comment out this switch

switch (m_ModuelControl)

{

case "Contact.ascx":

//5. Use this code to redirect to access Denied page of DotNetNuke Response.Redirect(Globals.NavigateURL("Access Denied"), true);

break;

case "ContactNotes.ascx":

break;

case "ContactPrivate.ascx":

break;

case "Contacts.ascx":

break;

case "ContactSearch.ascx":

break;

case "ContactSearchDetail.ascx":

break;

break;

case "EditContact.ascx":

break;

case "EditNote.ascx":

break;

}

//6. Load A specific control

PortalModuleBase objPortalModuleBase = (PortalModuleBase)LoadControl(m_ModuelControl);

objPortalModuleBase.ModuleConfiguration = ModuleConfiguration;

objPortalModuleBase.ID = System.IO.Path.GetFileNameWithoutExtension(m_ModuelControl);

phMain.Controls.Add(objPortalModuleBase);

}

}

}

So let’s step by step go through the details of the code.

1. If you look at the bold code in _default.ascx, , that is a place holder control of asp.net in which we will load another .ascx control at runtime.

2. Now if you look at the code in _default.aspx.cs you can find the steps in the comment at various places of the code.

3. Use a private variable to hold the control name into it. Initialize it with the default control that we will load if no query string parameter is found in the URL.

4. Initialize the variable with the query string parameter if it is specified in the URL

5. Include the switch statement for authorization if you need to authorize the user, Otherwise put that code in comment

6. Load the specific control in the phMain placeholder of the default control.

So the last thing to do is, When you redirect from any tab, just do this:

TabController objtab = new TabController();

TabInfo objtabinfo = new TabInfo();

objtabinfo = objtab.GetTabByName("Contacts", PortalId);

Response.Redirect(Globals.NavigateURL(objtabinfo.TabID, "", "mctl=" + "ContactNote")));

Remember that for this code to work:

· You need a tab called “Contacts“ in your portal

· You need a control called ContactNote.ascx in your module

· You need to include the namespace DotNetNuke.Entities.Tabs at the top of your page where you write the redirect code above.

Wish you a Happy coding with DotNetNuke.

No comments:

Post a Comment

Please add your valuable comments about this post if it helped you. Thanks