------------------------------------------------------------------------
Oct 2, 2009
Creating a Custom Toolpart for webpart
------------------------------------------------------------------------
Sep 19, 2009
Limiting length of Multiline textbox(inside webpart) through javascript
So we fix this problem using java script
Add this javascript in the CreateChildControl method.
Here is javascript for this :
----------------------------------------------------------
StringBuilder javascript = new StringBuilder();
javascript.AppendLine("<script type='text/javascript'>");
javascript.AppendLine("function Validation(evt,maxLength)");
javascript.AppendLine("{");
javascript.AppendLine("if(evt.value.length > maxLength)");
javascript.AppendLine("{");
javascript.AppendLine("alert('Maximum 255 characters allowed');");
javascript.AppendLine("evt.value = evt.value.substring(0,maxLength);");
javascript.AppendLine("}");
javascript.AppendLine("}");
javascript.AppendLine("</script>");
if (javascript != null)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "javascriptcode", javascript.ToString());
}
-------------------------------------------------------------
In above code we have written the java script and registered the client script in the page.Now we need to add this javascript event in the multiline textbox
-------------------------------------------------------------txtMultilineTextBox.Attributes.Add("onKeyDown", "Validation(this, 255)");
----------------------------------------------------------------
Programmatically Adding List viewer Web part(for document Library) on the page
Objective is to add the listviewer web part on the page programmatically. I have a document library for which i want to add the list viewer web part on any page.
Create a console application and paste the code
Code snippet
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite(”Site Url”);
SPWeb oSubWeb = site.OpenWeb();
oSubWeb.AllowUnsafeUpdates = true;
//Get the object of the page as SPFile on which we need to add the listviewer webpart
SPFile oFile = oSubWeb.GetFile(”default.aspx”);
//Create the object SPLimitedWebPart Manager
SPLimitedWebPartManager oWebPM = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
//Get the object of the list of which we are creatin the list viewer webpart
SPList oSubSiteList = oSubWeb.Lists["Shared Documents"];
//Create the object of ListViewer webpart
ListViewWebPart oListViewWP = new ListViewWebPart();
//Set the properties of the webpart
oListViewWP.ChromeType = PartChromeType.TitleOnly;
oListViewWP.Title = “Shared Documents”;
oListViewWP.ListName = oSubSiteList.ID.ToString(”B”).ToUpper();
//Define the zone in which webparts need to be added
oWebPM.AddWebPart(oListViewWP, “Zone 1″, 1);
}
}
Your job is done….
Enjoy!!!
If you find this post useful then please post your comments
Using SP Grid View inside Webpart
In this post I am using SPGrid view inside a webpart.
First we need to initialize the SPGrid view control in the Create Child control mehod and specify its properties
Use this code inside CreateChildControl method
—————————————————————
//Initialize the control
SPGrid CustomGrid;
CustomGrid = new SPGridView();
CustomGrid.Width = Unit.Pixel(700);
this.CustomGrid.ID = “Customgrid”;
//Specifying the Eventhandler for Paging
this.CustomGrid.PageIndexChanging += new GridViewPageEventHandler(CustomGrid_PageIndexChanging);
this.CustomGrid.AutoGenerateColumns = false;
//Specifying the grid properties
this.CustomGrid.PageSize = 10;
this.CustomGrid.BackColor = System.Drawing.Color.White;
this.CustomGrid.GridLines = GridLines.Horizontal;
//Allowing Paging on the Grid
this.CustomGrid.AllowPaging = true;
this.CustomGrid.EnableViewState = true;
this.CustomGrid.TemplateControl = null;
//Creating Boundfields in the Grid view
BoundField DealName = new BoundField();
DealName.DataField = “Name”;
DealName.ItemStyle.Width = Unit.Percentage(30);
DealName.ItemStyle.Wrap = true;
this.Controls.Add(CustomGrid);
CustomGrid.PagerTemplate = null;
—————————————————————–
Once the declaration and initialization of control is done we need to define the pagination of the SPGrid
This is the method for the pagination event
—————————————————–
// Method for Page indexing
void CustomGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
CustomGrid.PageIndex = e.NewPageIndex;
CustomGrid.DataBind();
}
catch (Exception ex)
{
}
}
Your job is done ..
Enjoy!!!