Friday, December 19, 2008

Dot Net Tips n Tricks

1) To set the textbox's Text property having TextBoxMode property set to Password:
In case of normal textbox, we change its Text property by writing following code :

TextBox txtSample = new TextBox();
txtSample.Text = "Sample";

But in case if we are using textbox for Password purpose, then we have to set its TextBoxMode to Password. So to change its Text property we have to use following code:

TextBox txtPassword = new TextBox();
if (txtPassword.TextMode == TextBoxMode.Password)
//newValue is the new value that we want to set.
txtPassword.Attributes.Add("value", newValue);

2) Code to remove HTML contents from a string :

public string RemoveHTML(string strHTML)
return Server.HtmlDecode(Regex.Replace(strHTML, "<(.|\n)*?>", ""));

Tuesday, December 9, 2008

ASP.Net Calendar control

Following are the steps to use ASP.Net calendar control in a web page:

1) Add <asp:Calendar ... > control tag in your web page.

HTML code:

<asp:TextBox ID="txtDate" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="btnDate" runat="server"
Text="..." OnClick="btnDate_Click"></asp:Button>
<asp:Calendar ID="calendar" runat="server" OnDayRender="OnDayRender"
BackColor="#ffffff" Width="300px" Height="300px" Font-Size="14px"
NextPrevFormat="shortmonth" DayNameFormat="firsttwoletters"
Visible="False" OnSelectionChanged="calendar_SelectionChanged">

<TodayDayStyle ForeColor="White" BackColor="Black"></TodayDayStyle>
<NextPrevStyle Font-Size="14px" Font-Bold="True"
<DayHeaderStyle Font-Size="14px" Font-Bold="True"></DayHeaderStyle>
<TitleStyle Font-Size="16px" Font-Bold="True"
BorderWidth="2px" ForeColor="#000055"></TitleStyle>
<OtherMonthDayStyle ForeColor="#CCCCCC"></OtherMonthDayStyle>


2) As visible property of calendar control is set to false, so on button click event set calendar's visible property to true and also set the selected date of calendar from the textbox.

CSharp (C#) code:

protected void btnDate_Click(object sender, EventArgs e)
if (txtDate.Text.Trim() != "")
calendar.SelectedDate = Convert.ToDateTime(txtDate.Text);
{ }

calendar.Visible = true; //showing the calendar.

3) Add code to handle the SelectionChanged event of calendar control.

CSharp (C#) code:

protected void calendar_SelectionChanged(object sender, EventArgs e)
//Displaying the selected date in TextBox
txtDate.Text = calendar.SelectedDate.ToString("d");
calendar.Visible = false; //hiding the calendar.

4) Now to disable the earlier/previous date selection in caledar control, handle OnDayRender event of Calendar control.

CSharp (C#) code:

protected void OnDayRender(Object sender, DayRenderEventArgs e)
e.Day.IsSelectable = (e.Day.Date >= DateTime.Today);
//e.Day.IsSelectable = (e.Day.Date >= DateTime.Now);

Following is the snapshot of Calendar control in a web page :

Wednesday, December 3, 2008

SharePoint's ListViewWebPart

To use SharePoint's ListViewWebPart directly in your ASPX page, follow these steps:

1) Register Microsoft.SharePoint.WebPartPages namespace in ASPX page :

<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

2) Add following ListViewWebPart's html code :

<WebPartPages:ListViewWebPart ID="oListViewWP" runat="server" __WebPartId="{GUID}"

Note :
Replace GUID in above code either by a newly generated GUID or if you are using SharePoint designer, then SharePoint designer will automatically insert a new GUID in your HTML code when you try to save the ASPX page.

3) Add following server side code to populate data in ListViewWebPart :

SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["Announcements"];

oListViewWP.ListName = oList.ID.ToString("B").ToUpper();
oListViewWP.ViewGuid = oList.Views["MyView"].ID.ToString("B").ToUpper();


Note :
Don't forget to write oListViewWP.GetDesignTimeHtml();, otherwise ListViewWebPart will not display any data in it.

Here MyView is just a dummy SPView name, you can use either default SPView or can create a new SPView with only desired columns and settings.


Now if you want to hide/remove the ToolBar from ListViewWebPart, then as per me following are the two ways :
1) Set SuppressWebPartChrome property of WebPart

oListViewWP.SuppressWebPartChrome = true;

2) One of the problem with above code is that it hide the toolBar in ListViewWebPart as well as in the SPList/SPView from which it is displaying the data
( I face this problem, not sure about others :) ).

So, the other alternative is to add following CSS in our code:

<style type="text/css">
.ms-menutoolbar { display: none; }

Be careful while using the above CSS, because it hide all the ToolBars which uses the .ms-menutoolbar CSS class.

Reference :
MSDN Forum


If you want to apply Filter on ListViewWebPart's column or in other words, display data in ListViewWebPart with only desired column value, then write following code :

string columnValue = "SP";
oListViewWP.FilterString = "FilterField1=Title&FilterValue1=" + columnValue;

The above code results in the display only those records whose column name Title has value SP.


Another major challenge that i faced while using ListView WebPart (or in any normal WebPart) is to change/modify the Empty message or no record message.

The exact message is : There are no items to show in this view of the "ViewName" list. To create a new item, click "New" above.

One of the way to change this message is to modify the ViewEmpty XML node of ListViewXml.

Following code snippet describes the way to change the empty message of ListView WebPart :

//oListViewWP is ListView WebPart instance
XmlDocument xmlDoc = new XmlDocument();
XmlNode node = xmlDoc.SelectSingleNode("//ViewEmpty");
if (node != null)
StringBuilder strB = new StringBuilder();
strB.Append("<HTML><![CDATA[<SPAN Class='ms-vb' style='width:200px;'>]]></HTML>");
strB.Append("<HTML>There is no item to show.</HTML>");

node.InnerText = strB.ToString();

oListViewWP.ListViewXml = xmlDoc.OuterXml;

Note : It is required to set the ListViewXml before GetDesignTimeHtml() function call.

Otherwise, if you are using SharePoint Designer and using/populating ListView WebPart in design time. Then simply search following text :
There are no items to show in this view of the and replace it with your required/desired message.

Another way is to change/modify the text directly in core.resx file that reside at following location :
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\Resources\core.resx.

Note : If we change the core.resx file, it will be gets reflected in all sites that are using the core.resx resource file.