extending Enum

Today I m faced a problem when I want to re-use Enum without major change on the based code.

Here the simple example,

I got a drop down list which bind Enum, here the sample code:

public enum Gender
{
[Description("I m a Male")] M,
[Description("I m a Female")] F
}

if you just bind the Enum, your dropdownlist will simply show you “M” and “F”, what if I want to show “I m a Male” and “I m a Female”?

OK, here the trick. (I will put the Author URL once I found, I m forgot the link)

in PageLoad or Code Behind,

using System.ComponentModel;
using System.Reflection;

public static string GetDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description :  value.ToString();
}

Here you will see gender.ToString(“d”), it will return 0 or 1, if you simply put ToString(), it will return M or F

foreach (Gender gender in Enum.GetValues(typeof(Gender)))
{
this.ddlGender.Items.Add(new ListItem(GetDescription(gender ),  gender.ToString("d")));
}

Hope this Help

Tips to control DNNTabStrip’s tab to display on page load

I just discovered that DNNTabStrip can be controlled.

I got 4 tabs on the page, which is Tab A, Tab B, Tab C and Tab D.

When I m click Tab B, it will navigate to Detail page of B, then I click Back button, it will return to Tab A but not Tab B by default, so we need to do something here to make it return to Tab B if you are from detail page of Tab B.

in ASPX,
declare a hidden field.

<input id="hTabIndex" name="hTabIndex" type="hidden" />

Code below is my DNNTabStrip, I called it as TabStrip. Please Notice TabClickFunction=”tabClick”, it will trigger the javascript to do something which I will put the sample code below.


<dnn:DNNTabStrip id="TabStrip" runat="server" Width="100%" DefaultLabel-CssClass="tablabel"  TabRenderMode="PostBack" DefaultLabel-CssClassSelected="tablabelselected" DefaultLabel-CssClassHover="tablabelhover"  TabClickFunction="tabClick">

<script language="JavaScript">

function tabClick(evt, element)
{
document.getElementById('hTabIndex').value = dnn.dom.getAttr(element, 'tid');
document.forms[0].submit();
}

in Code Behind,
declare variable,

private int m_tabIndex = 0;
private string m_tabIndexName = string.Empty;

In PageLoad,
m_tabIndexName = Request.Form["hTabIndex"];

if (m_tabIndexName.Trim().Length > 0)
{
switch (m_tabIndexName)
{
case TabA:
m_tabIndex = 0;
break;
case TabB:
m_tabIndex = 1;
break;
case TabC:
m_tabIndex = 2;
break;
case TabD:
m_tabIndex = 3;
break;
default:
m_tabIndex = 0;
break;
}
Session["tabIndexName"] = m_tabIndex;

TabStrip.SelectedIndex =
(Session["tabIndexName"] == null) ?
m_tabIndex : int.Parse(Session["tabIndexName"].ToString());

Hope this help those who wish to control the tab on page load, this might not a good practise, so i hope if someone found a better way to do that, please let me know.

Disable upload feature for DNN UrlControl

To avoid “dirty” files upload by the user of the DNN, most of the time Administrator of the System will do it, If you want to disable the DNN UrlControl which only allow user to assign file but not upload the file, you can use following code…

<dnn:url id="ctlVideoURL" runat="server" width="300" showfiles="True" ShowUpLoad="false" showUrls="False" shownewwindow="False" showtrack="False" showlog="False" UrlType="F" showtabs="False"></dnn:url>

the keyword is ShowUpload=”false”

Create DNN’s Table & SP script

Objective to create DNN table & SP scripts is ease for deployment.

Here some very simple example,

CREATE TABLE {databaseOwner}[{objectQualifier}tbl_XYZ](
[xyzId] [int] IDENTITY(1,1) NOT NULL,
[xyzName] [varchar] (100) NOT NULL,
) ON [PRIMARY]

if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}[{objectQualifier}tbl_XYZ]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}tbl_XYZ_add
GO

Please take note on those{databaseOwner}{objectQualifier}, you must included this or else you will have a bad time during deployment when you have your own databaseOnwer & objectQualifier

Configure Host Email in DNN

I m have a hard time when I m need to figure out how to configure the email in DNN.

I put the correct SMTP server and port but still not manage to send email out, then I noticed I did not put the host email in “host details”. Hope readers here do not repeat the same mistake.

DotNetNuke DatePicker

Got a chance to look at DNN’s DatePicker yesterday, seem like it quite easy to implement.

in ASCX,

<asp:TextBox ID="txtStartDate" runat="server" AutoPostBack="true" Enabled="False"></asp:TextBox>
<asp:Image ID="imgStartDate" runat="server" EnableViewState="False" ImageUrl="~/DesktopModules/BCU/images/date.gif" />);

in CS,

string attrib = "";
attrib = DotNetNuke.Common.Utilities.Calendar.InvokePopupCal(txtStartDate);

this.imgStartDate.Attributes.Add("onClick", attrib);

But bear in mind, please use image instead of image button. When on click, the date value will automatic bind into textbox. Super Simple!

DotNetNuke.Entities.Tabs

in my previous post, I blogged about NavigateURL from view page to edit or another view page in the same module.

what if i want to navigate to other module? For better understanding, From DNN Module A to DNN Module B, both module doesnt related to each other.

Ok, first we need to import the Namespace “DotNetNuke.Entities.Tabs”

Declare the object and use it shown as below,

TabInfo objTab = new TabInfo();
TabController tabCtrl = new TabController();
objTab = tabCtrl.GetTabByName("ModuleB", PortalId);
this.Response.Redirect(Globals.NavigateURL(objTab.TabID), true);

Yes, now you can navigate to other module.

output param using SqlHelper.ExecuteNonQuery

in case you develop the DNN module using SqlHelper.ExecuteNonQuery which involve parent and child table, and you wish to grab the parents identity for child table manipulation. It always return null, so please use another API which is SQLHelper.ExecuteScalar(),

when you want to return the output param in sp! remember to CAST it as integer! Or else the value return will become “NULL”.

SELECT CAST(scope_identity() as int) ;

ASP.NET RadioButtonList bind enum & display direction

If you wish to bind a simple data such as “Inactive” & “Active” using the asp.net radiobuttonlist, you can try out this way.
C# Code,

public enum Status
{
active,
inactive
}
RadioButtonList1.DataSource = Enum.GetValues(typeof(status));
RadioButtonList1.DataBind();

ASPX page,

<asp:radiobuttonlist runat="server" id="RadioButtonList1">
</asp:radiobuttonlist>

How to display the radiobutton horizontal? usually the radionbutton will display vertical, so simply add RepeatDirection=”Horizontal” inside the radionbuttonlist tag

simple example as below,

<asp:RadioButtonList ID="rbtnStatus" RepeatDirection="Horizontal" runat="server"></asp:RadioButtonList>
Related Posts with Thumbnails
Get Adobe Flash player