私はasp.netを初めて使用し、いくつかの手がかりがありますが、これを行う方法がわからないため、助けていただければ幸いです。
ページング付きのデータリストがあります。ドロップダウンリストを介してユーザー入力からデータリストを更新すると、データリストとページングがバインドされます。
私の私有財産
private int CurrentPage
{
get
{
object objPage = ViewState["_CurrentPage"];
int _CurrentPage = 0;
if (objPage == null)
{
_CurrentPage = 0;
}
else
{
_CurrentPage = (int)objPage;
}
return _CurrentPage;
}
set { ViewState["_CurrentPage"] = value; }
}
private int fistIndex
{
get
{
int _FirstIndex = 0;
if (ViewState["_FirstIndex"] == null)
{
_FirstIndex = 0;
}
else
{
_FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
}
return _FirstIndex;
}
set { ViewState["_FirstIndex"] = value; }
}
private int lastIndex
{
get
{
int _LastIndex = 0;
if (ViewState["_LastIndex"] == null)
{
_LastIndex = 0;
}
else
{
_LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
}
return _LastIndex;
}
set { ViewState["_LastIndex"] = value; }
}
プライベートメソッド
private void BindItemsList()
{
DataTable dataTable = dm.GetDataTable().Tables["product"];// middle tier which pulls data from a database
_PageDataSource.DataSource = dataTable.DefaultView;
_PageDataSource.AllowPaging = true;
_PageDataSource.PageSize = 1;
_PageDataSource.CurrentPageIndex = CurrentPage;
ViewState["TotalPages"] = _PageDataSource.PageCount;
this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;
this.dlPhones.DataSource = _PageDataSource;
this.dlPhones.DataBind();
this.doPaging();
}
/// <summary>
/// Binding Paging List
/// </summary>
private void doPaging()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
fistIndex = CurrentPage - 5;
if (CurrentPage > 5)
{
lastIndex = CurrentPage + 5;
}
else
{
lastIndex = 10;
}
if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
{
lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
fistIndex = lastIndex - 10;
}
if (fistIndex < 0)
{
fistIndex = 0;
}
for (int i = fistIndex; i < lastIndex; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
this.dlPaging.DataSource = dt;
this.dlPaging.DataBind();
}
ページコードの背後にあるコード
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindItemsList();
}
}//end of page load
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
this.BindItemsList();
}
protected void lbtnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
this.BindItemsList();
}
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Paging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
this.BindItemsList();
}
}
protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Style.Add("fone-size", "14px");
lnkbtnPage.Font.Bold = true;
}
}
protected void lbtnLast_Click(object sender, EventArgs e)
{
CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
this.BindItemsList();
}
protected void lbtnFirst_Click(object sender, EventArgs e)
{
CurrentPage = 0;
this.BindItemsList();
}
asp.netフロントページ
<asp:Label ID="lblSearch" runat="server" Text="Search By Phone"></asp:Label>
<asp:DropDownList ID="ddlCat" runat="server">
<asp:ListItem>Nokia</asp:ListItem>
<asp:ListItem>Samsung</asp:ListItem>
<asp:ListItem>Motorola</asp:ListItem>
<asp:ListItem>Sony</asp:ListItem>
<asp:ListItem>Ericsson</asp:ListItem>
<asp:ListItem>Sony</asp:ListItem>
<asp:ListItem>LG</asp:ListItem>
<asp:ListItem>Apple</asp:ListItem>
<asp:ListItem>HTC</asp:ListItem>
<asp:ListItem>BlackBerry</asp:ListItem>
<asp:ListItem>HP</asp:ListItem>
<asp:ListItem>Huawei</asp:ListItem>
<asp:ListItem>Acer</asp:ListItem>
<asp:ListItem>Asus</asp:ListItem>
<asp:ListItem>Dell</asp:ListItem>
<asp:ListItem>Alcatel</asp:ListItem>
<asp:ListItem>Vodafone</asp:ListItem>
<asp:ListItem>T-Mobile</asp:ListItem>
<asp:ListItem>Toshiba</asp:ListItem>
<asp:ListItem>Gigabyte</asp:ListItem>
<asp:ListItem>Pantech</asp:ListItem>
<asp:ListItem>ZTE</asp:ListItem>
<asp:ListItem>Micromax</asp:ListItem>
<asp:ListItem>BLU</asp:ListItem>
<asp:ListItem>Spice</asp:ListItem>
<asp:ListItem>Icemobile</asp:ListItem>
<asp:ListItem>verykool</asp:ListItem>
<asp:ListItem>Vertu</asp:ListItem>
<asp:ListItem>Celkon</asp:ListItem>
<asp:ListItem>NIU</asp:ListItem>
<asp:ListItem>Yezz</asp:ListItem>
<asp:ListItem>Parla</asp:ListItem>
<asp:ListItem>Plum</asp:ListItem>
<asp:ListItem>Sim Card</asp:ListItem>
</asp:DropDownList>
<asp:DataList ID="dlPhones" runat="server" DataKeyField="PID" RepeatDirection="Horizontal" RepeatColumns="1" CellPadding="10" CellSpacing="10" >
<ItemTemplate>
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' />
<br />Price:
<asp:Label ID="PriceLabel" runat="server" Text='<%# "$" + Eval("Price") %>' />
<br />
<asp:Image ID="Image1" runat="server" CssClass="ImageStyles" ImageUrl='<%# "GetImage.ashx?Id=" + Eval("PID") %>' BackColor="White" BorderStyle="Ridge" BorderColor="WhiteSmoke" />
<br />
</ItemTemplate>
</asp:DataList>
<table cellpadding="0" border="0">
<tr>
<td align="right">
<asp:LinkButton ID="lbtnFirst" runat="server" CausesValidation="false" OnClick="lbtnFirst_Click"><img src="images/First.png" height="50px" width="100px" onmouseover="this.src='images/FirstMouseOver.png';" onmouseout="this.src='images/First.png';" /></asp:LinkButton>
</td>
<td align="right">
<asp:LinkButton ID="lbtnPrevious" runat="server" CausesValidation="false" OnClick="lbtnPrevious_Click"><img src="images/Previouse.png" onmouseover="this.src='images/PreviouseMouseOver.png';" onmouseout="this.src='images/Previouse.png';" /></asp:LinkButton> </td>
<td align="center" valign="middle">
<asp:DataList ID="dlPaging" runat="server" RepeatDirection="Horizontal" OnItemCommand="dlPaging_ItemCommand"
OnItemDataBound="dlPaging_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnPaging" Font-Size="X-Large" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
CommandName="Paging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</td>
<td align="left">
<asp:LinkButton ID="lbtnNext" runat="server" CausesValidation="false"
OnClick="lbtnNext_Click"><img src="images/Next.png" onmouseover="this.src='images/NextMouseOver.png';" onmouseout="this.src='images/Next.png';" /></asp:LinkButton></td>
<td align="left">
<asp:LinkButton ID="lbtnLast" runat="server" CausesValidation="false" OnClick="lbtnLast_Click"><img src="images/Last.png" onmouseover="this.src='images/LastMouseOver.png';" onmouseout="this.src='images/Last.png';" /></asp:LinkButton></td>
</tr>
<tr>
<td colspan="5" align="center" style="height: 30px" valign="middle">
<asp:Label ID="lblPageInfo" runat="server"></asp:Label></td>
</tr>
</table>