Tutorials to .com

Tutorials to .com » Dotnet » Foundation » Multi-column sort ASP.Net2.0 GridView to show the icon to sort, paging

Multi-column sort ASP.Net2.0 GridView to show the icon to sort, paging

Print View , by: iSee ,Total views: 28 ,Word Count: 1742 ,Date: Sun, 23 Aug 2009 Time: 3:59 PM

Recently, the use of the GridView Control ASP.net 2.0, it was found that sorting and paging features of Microsoft is very simple to achieve, such as sorting, click the listing in time to trigger the full-page PostBack, and then sort out his head but did not ascending descending a display of the icon, which will give end users a very confusing, because they do not know is the ascending or descending order, so first of all to solve this problem is the first ascending descending show in the bar graph subscript, the second problem to be solved is the default GridView to sort out only in accordance with a schedule, that is not multi-column sort, and in accordance with the practical application of only one sort can not meet the business needs, and the third is sub-GridView problem page, GridView paging predefined page shows that relatively simple, and practical application, the page may not only show the home page, Previous, Next, Last page, or page numbers so simple, it should be more , Jump, the current page number, the total number of pages and so on, more detailed information.

First: GridView sorting and multi-column sort icons

First of all, we can create a new class library procedures, primarily the need to invoke System.Web.Dll and then create a new category of document, this type of inheritance and GridView control, we only need some way to re-can.
Examples of my presentation, using a single sort, if the opening of the multi-column sort, the AllowMultiColumnSorting control is set to True multi-column sort.


1 public class WebGridView: GridView
2 (
3 Properties # region Properties
4 /**//// <summary>
5 / / / whether or to prohibit the opening of the multi-column sort
6 / / / </ summary>
7 [
8 Description ( "sort out whether the opening of the multi-function"),
9 Category ( "Sort"),
10 DefaultValue ( "false"),
11]
12 public bool AllowMultiColumnSorting
13 (
14 get
15 (
16 object o = ViewState [ "EnableMultiColumnSorting"];
17 return (o! = Null? (Bool) o: false);
18)
19 set
20 (
21 AllowSorting = true;
22 ViewState [ "EnableMultiColumnSorting"] = value;
23)
24)
25 /**//// <summary>
26 / / / Show icon when ascending
27 / / / </ summary>
28 [
29 Description ( "Show icon when ascending"),
30 Category ( "Sort"),
31 Editor ( "System.Web.UI.Design.UrlEditor", typeof (System.Drawing.Design.UITypeEditor)),
32 DefaultValue (""),
33
34]
35 public string SortAscImageUrl
36 (
37 get
38 (
39 object o = ViewState [ "SortImageAsc"];
40 return (o! = Null? O.ToString (): "");
41)
42 set
43 (
44 ViewState [ "SortImageAsc"] = value;
45)
46)
47 /**//// <summary>
48 / / / Show icon when descending
49 / / / </ summary>
50 [
51 Description ( "Show icon when descending"),
52 Category ( "Sort"),
53 Editor ( "System.Web.UI.Design.UrlEditor", typeof (System.Drawing.Design.UITypeEditor)),
54 DefaultValue (""),
55]
56 public string SortDescImageUrl
57 (
58 get
59 (
60 object o = ViewState [ "SortImageDesc"];
61 return (o! = Null? O.ToString (): "");
62)
63 set
64 (
65 ViewState [ "SortImageDesc"] = value;
66)
67)
68 # endregion
69 rewrite the rewrite method # region Methods
70 protected override void OnSorting (GridViewSortEventArgs e)
71 (
72 if (AllowMultiColumnSorting)
73 (
74 e.SortExpression = GetSortExpression (e);
75)
76
77 base.OnSorting (e);
78)
79 protected override void OnRowCreated (GridViewRowEventArgs e)
80 (
81 if (e.Row.RowType == DataControlRowType.Header)
82 (
83 if (SortExpression! = String.Empty)
84 (
85 DisplaySortOrderImages (SortExpression, e.Row);
86 this.CreateRow (0, 0, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
87)
88)
89 base.OnRowCreated (e);
90)
91 # endregion
92 protected methods # region methods protected
93 /**//// <summary>
94 / / / obtain the sort expression
95 / / / </ summary>
96 protected string GetSortExpression (GridViewSortEventArgs e)
97 (
98 string [] sortColumns = null;
99 string sortAttribute = SortExpression;
100
101 if (sortAttribute! = String.Empty)
102 (
103 sortColumns = sortAttribute.Split (",". ToCharArray ());
104)
105 if (sortAttribute.IndexOf (e.SortExpression)> 0 | | sortAttribute.StartsWith (e.SortExpression))
106 (
107 sortAttribute = ModifySortExpression (sortColumns, e.SortExpression);
108)
109 else
110 (
111 sortAttribute + = String.Concat (",", e.SortExpression, "ASC");
112)
113 return sortAttribute.TrimStart (",". ToCharArray ()). TrimEnd (",". ToCharArray ());
114
115)
116 /**//// <summary>
117 / / / modify the sort order
118 / / / </ summary>
119 protected string ModifySortExpression (string [] sortColumns, string sortExpression)
120 (
121 string ascSortExpression = String.Concat (sortExpression, "ASC");
122 string descSortExpression = String.Concat (sortExpression, "DESC");
123
124 for (int i = 0; i <sortColumns.Length; i + +)
125 (
126
127 if (ascSortExpression.Equals (sortColumns [i]))
128 (
129 sortColumns [i] = descSortExpression;
130)
131
132 else if (descSortExpression.Equals (sortColumns [i]))
133 (
134 Array.Clear (sortColumns, i, 1);
135)
136)
137
138 return String.Join (",", sortColumns). Replace (",,", ","). TrimStart (",". ToCharArray ());
139
140)
141 /**//// <summary>
142 / / / obtain the current expressions of the selected sort out
143 / / / </ summary>
144 protected void SearchSortExpression (string [] sortColumns, string sortColumn, out string sortOrder, out int sortOrderNo)
145 (
146 sortOrder = "";
147 sortOrderNo = -1;
148 for (int i = 0; i <sortColumns.Length; i + +)
149 (
150 if (sortColumns [i]. StartsWith (sortColumn))
151 (
152 sortOrderNo = i + 1;
153 if (AllowMultiColumnSorting)
154 (
155 sortOrder = sortColumns [i]. Substring (sortColumn.Length). Trim ();
156)
157 else
158 (
159 sortOrder = ((SortDirection == SortDirection.Ascending)? "ASC": "DESC");
160)
161)
162)
163)
164 /**//// <summary>
165 / / / draw the picture ascending descending
166 / / / </ summary>
167 protected void DisplaySortOrderImages (string sortExpression, GridViewRow dgItem)
168 (
169 string [] sortColumns = sortExpression.Split (",". ToCharArray ());
170
171 for (int i = 0; i <dgItem.Cells.Count; i + +)
172 (
173 if (dgItem.Cells [i]. Controls.Count> 0 & & dgItem.Cells [i]. Controls [0] is LinkButton)
174 (
175 string sortOrder;
176 int sortOrderNo;
177 string column = ((LinkButton) dgItem.Cells [i]. Controls [0]). CommandArgument;
178 SearchSortExpression (sortColumns, column, out sortOrder, out sortOrderNo);
179 if (sortOrderNo> 0)
180 (
181 string sortImgLoc = (sortOrder.Equals ( "ASC")? SortAscImageUrl: SortDescImageUrl);
182
183 if (sortImgLoc! = String.Empty)
184 (
185 Image imgSortDirection = new Image ();
186 imgSortDirection.ImageUrl = sortImgLoc;
187 dgItem.Cells [i]. Controls.Add (imgSortDirection);
188
189)
190 else
191 (
192
193 if (AllowMultiColumnSorting)
194 (
195 Literal litSortSeq = new Literal ();
196 litSortSeq.Text = sortOrderNo.ToString ();
197 dgItem.Cells [i]. Controls.Add (litSortSeq);
198
199)
200)
201)
202)
203)
204
205)
206 # endregion
207)
Second: a detailed page of information shows that this feature does not control package into form, directly in the event of GridView_DataBound Last operation.
The following is a multi-column sorting and paging code demo show


<script runat="server">
void PageDropDownList_SelectedIndexChanged (Object sender, EventArgs e)
(
GridViewRow pagerRow = CustomersGridView.BottomPagerRow;
DropDownList pageList = (DropDownList) pagerRow.Cells [0]. FindControl ( "PageDropDownList");
CustomersGridView.PageIndex = pageList.SelectedIndex;
)
void CustomersGridView_DataBound (Object sender, EventArgs e)
(
GridViewRow pagerRow = CustomersGridView.BottomPagerRow;

LinkButton linkBtnFirst = (LinkButton) pagerRow.Cells [0]. FindControl ( "linkBtnFirst");
LinkButton linkBtnPrev = (LinkButton) pagerRow.Cells [0]. FindControl ( "linkBtnPrev");
LinkButton linkBtnNext = (LinkButton) pagerRow.Cells [0]. FindControl ( "linkBtnNext");
LinkButton linkBtnLast = (LinkButton) pagerRow.Cells [0]. FindControl ( "linkBtnLast");

if (CustomersGridView.PageIndex == 0)
(
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
)
else if (CustomersGridView.PageIndex == CustomersGridView.PageCount-1)
(
linkBtnLast.Enabled = false;
linkBtnNext.Enabled = false;
)
else if (CustomersGridView.PageCount <= 0)
(
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
linkBtnNext.Enabled = false;
linkBtnLast.Enabled = false;
)
DropDownList pageList = (DropDownList) pagerRow.Cells [0]. FindControl ( "PageDropDownList");
Label pageLabel = (Label) pagerRow.Cells [0]. FindControl ( "CurrentPageLabel");

if (pageList! = null)
(
for (int i = 0; i <CustomersGridView.PageCount; i + +)
(
int pageNumber = i + 1;
ListItem item = new ListItem (pageNumber.ToString () + "/" + CustomersGridView.PageCount.ToString (), pageNumber.ToString ());
if (i == CustomersGridView.PageIndex)
(
item.Selected = true;
)
pageList.Items.Add (item);

)

)
if (pageLabel! = null)
(
int currentPage = CustomersGridView.PageIndex + 1;
pageLabel.Text = "Current Page:" + currentPage.ToString () +
"/" + CustomersGridView.PageCount.ToString ();
)
)

</ script>

<html>
<body>
<form id="Form1" runat="server">
<h3>
GridView PagerTemplate Example </ h3>
<asp: WebGridView ID = "CustomersGridView" DataSourceID = "CustomersSqlDataSource" AutoGenerateColumns = "true"
AllowPaging = "true" OnDataBound = "CustomersGridView_DataBound" SortAscImageUrl = "~ \ images \ arrow-up.gif" SortDescImageUrl = "~ \ images \ arrow-down.gif" runat = "server" AllowSorting = "True" Width = "723px ">
<PagerStyle ForeColor="Blue" BackColor="LightBlue" />
<PagerTemplate>
<table width="100%">
<tr>
<td width="70%">
<asp:Label ID="MessageLabel" ForeColor="Blue" Text=":" runat="server" />
<asp: DropDownList ID = "PageDropDownList" AutoPostBack = "true" OnSelectedIndexChanged = "PageDropDownList_SelectedIndexChanged"
runat = "server" />
<asp:LinkButton CommandName="Page" CommandArgument="First" ID="linkBtnFirst" runat="server"> Home </ asp: LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="linkBtnPrev" runat="server"> Previous </ asp: LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Next" ID="linkBtnNext" runat="server"> Next </ asp: LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Last" ID="linkBtnLast" runat="server"> at the end of page </ asp: LinkButton>
</ td>
<td width="70%" align="right">
<asp:Label ID="CurrentPageLabel" ForeColor="Blue" runat="server" />
</ td>
</ tr>
</ table>
</ PagerTemplate>
</ asp: WebGridView>
<asp: SqlDataSource ID = "CustomersSqlDataSource" SelectCommand = "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
ConnectionString ="<%$ ConnectionStrings: NorthWindConnectionString%> "runat =" server ">
</ asp: SqlDataSource>

</ form>
</ body>
</ html>


.NET foundation Articles


Can't Find What You're Looking For?


Rating: 0.0

Comments

No comments posted.