Friday, February 27, 2009

Reporting Service Custom Code Add Assembly

For adding assembly you have to copy the .dll assembly file to the following location "C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies" folder.

Friday, February 20, 2009

Paging in Datalist and Repeater

Datalist and Repeater doesn't provide any paging by default like gridview or datagrid. So we avoid using datagrid/repeater as much as possible. But there comes situation where using datalist or repeater is easier to maintain the design for the page. Like some product is displayed like row by row with 4 producs in a single w. There its tough to achieve (but not impossible) this design output wit datagrid/gridview. So we choose datalist making its RepeatColumns="4" RepeatDirection="Horizontal".
But paging???
So i created a control named as COUSPaging (Cous is my project name). Which contains two link buttons as "Next" and "Previous" and a textbox for goto page as below



In my control i used a public event "PageIndexChanging" which is of type GridViewPageEventHandler, so that it sends the same parameter as GridView. The event is handled from the Parent page containg the DataList.
The Paging.aspx Page Code:
<table>
<tr>
<td>
<asp:LinkButton ID="linkPrevious" CssClass="button" runat="server" Text="Previous" CommandName="Previous" OnClick="linkPrevious_Click"></asp:LinkButton>
</td>
<td>
<asp:Literal ID="litPageCount" runat="server"></asp:Literal>
<span id="spanGotpPage" runat="server">Goto Page:
<asp:TextBox Width="15" ID="txtPageNo" CssClass="inputfld" runat="server" MaxLength="3"></asp:TextBox>
<asp:Button ID="btnGo" runat="server" Text="Go" CssClass="button" OnClick="btnGo_Click" />
</span>
</td>
<td>
<asp:LinkButton ID="linkNext" CssClass="button" runat="server" Text="Next" CommandName="Next" OnClick="linkNext_Click"></asp:LinkButton>
</td>
</tr>
</table>

.Cs Page Coding:

void Controls_Paging_PreRender(object sender, EventArgs e)
{
litPageCount.Text = string.Format("{0} of {1} Pages ", SelectedPageIndex + 1, TotalNoOfPages);
if (SelectedPageIndex == TotalNoOfPages - 1)
{
linkNext.Enabled = false;
}
else
{
linkNext.Enabled = true;
}
if (SelectedPageIndex == 0)
{
linkPrevious.Enabled = false;
}
else
{
linkPrevious.Enabled = true;
}
txtPageNo.Text = (SelectedPageIndex + 1).ToString();
}
protected void btnGo_Click(object sender, EventArgs e)
{
int newPageIndex = 0;
try
{
newPageIndex = Convert.ToInt32(txtPageNo.Text) - 1;
if (newPageIndex > TotalNoOfPages - 1)
{
newPageIndex = TotalNoOfPages - 1;
}
if (newPageIndex < 0)
{
newPageIndex = 0;
}
SelectedPageIndex = newPageIndex;
PageIndexChanging(btnGo, new GridViewPageEventArgs(newPageIndex));
}
catch { }
}
public event GridViewPageEventHandler PageIndexChanging;
public int PageSize
{
set
{
ViewState["PageSize"] = value;
}
get
{
if (ViewState["PageSize"] is int)
return (int)ViewState["PageSize"];
else return 0;
}
}

public int CurrentPageIndex
{
set
{
ViewState["CurrentPageIndex"] = value;
}
private get
{
if (ViewState["CurrentPageIndex"] is int)
return (int)ViewState["CurrentPageIndex"];
else return 0;
}
}
public int StartIndex
{
get
{
return SelectedPageIndex * PageSize;
}
}

public int TotalNoOfPages
{
get
{
return (int)Math.Ceiling(TotalNoOfRows / (float)PageSize);
}
}
public long TotalNoOfRows
{
set
{
ViewState["TotalNoOfRows"] = value;
}
get
{
if (ViewState["TotalNoOfRows"] is long)
return (long)ViewState["TotalNoOfRows"];
else return 0;
}
}
public int SelectedPageIndex
{
set
{
ViewState["SelectedPageIndex"] = value;
}
get
{
if (ViewState["SelectedPageIndex"] is int)
return (int)ViewState["SelectedPageIndex"];
else return 0;
}
}
protected void linkNext_Click(object sender, EventArgs e)
{
if (SelectedPageIndex < TotalNoOfPages)
{
SelectedPageIndex = SelectedPageIndex + 1;
PageIndexChanging(sender, new GridViewPageEventArgs(SelectedPageIndex));
}
}
protected void linkPrevious_Click(object sender, EventArgs e)
{
if (SelectedPageIndex > 0)
{
SelectedPageIndex = SelectedPageIndex - 1;
PageIndexChanging(sender, new GridViewPageEventArgs(SelectedPageIndex));
}
}

The page where the control is used

Here the page size is initialized. From the cs page call the bind datalist function as

void bindDataList()
{
long totalNoOfRows;
ImageTblMasterTable dtImage = ImageTblMaster.SelectAll(-1, 1, pagingDL.StartIndex, pagingDL.PageSize, out totalNoOfRows);
pagingDL.TotalNoOfRows = totalNoOfRows;
dlImage.DataSource = dtImage;
dlImage.DataBind();
}

Here i am using custom paging. So i am sending the page start index and page size which is converted to ennIndex before the call to SP. Here is code from SP

Select @TotalNoOfRows = Count(*) FROM [dbo].[Image_tbl_Master]

Select * from
(
SELECT ROW_NUMBER() OVER (ORDER BY ImageId) as RowNumber, m.* FROM [dbo].[Image_tbl_Master] as m
)t Where
RowNumber >= @StartIndex
AND
RowNumber <= @EndIndex As i am using SQL Server 2005 i get the facility of ROW_Number() but in SQL Server 2000 or before, there is no row number. So you have to change the query like Select * from ( Select (Select Count(*) from Image_tbl_Master where ImageId<=img.ImageId )as RowNumber, img.* from Image_tbl_Master as img )t Where RowNumber >= @StartIndex
AND
RowNumber <= @EndIndex

Here row number is calculated on the fly through a subquery. See the where clause "ImageId<=img.ImageId ", it is responsible for the sorting. As i was sorting with ImageId i have used it like that.