最近客户有一需求,需要显示一些交易记录,还有分页和排序以及维护控件的状态的要求,很多时间不用纯粹的gridview,竟然有些生疏了,下面整理一下代码,做个记号,以便将来做参考之用。
这是前台,每行都有dropdownlist和textbox。
<asp:GridView ID="gvTransactions" runat="server" AutoGenerateColumns="false" Width="100%"
BorderWidth="0px" CellPadding="0" CellSpacing="1" GridLines="None" AllowSorting="true"
AllowPaging="true" ShowHeader="true" OnRowDataBound="gvTransactions_OnRowDataBound"
OnPageIndexChanging="gvTransactions_PageIndexChanging" OnSorting="gvTransactions_sort"
EnableViewState="true">
<Columns>
<asp:BoundField DataField="numcontrat" SortExpression="numcontrat" />
<asp:BoundField DataField="nomclient" SortExpression="nomclient" />
<asp:BoundField DataField="datecontrat" SortExpression="datecontrat" DataFormatString="{0:MM/dd/yyyy}" />
<asp:BoundField DataField="nomoperation" SortExpression="nomoperation" />
<asp:BoundField DataField="pointscontrat" SortExpression="pointscontrat" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblColumn6Text" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlDestinataire1" runat="server" CssClass="memberddl1">
</asp:DropDownList>
<asp:DropDownList ID="ddlDestinataire2" runat="server" CssClass="memberddl2">
</asp:DropDownList>
<asp:HiddenField runat="server" ID="hdnDestinataire1" Value='<%#Eval("destinataire1")%>' />
<asp:HiddenField runat="server" ID="hdnDestinataire2" Value='<%#Eval("destinataire2")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblColumn7Text" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox runat="server" ID="txtTransfert1" CssClass="valuetxt1" Width="50px"
Text='<%# Eval("transfert1") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="revTransfert1" runat="server" ControlToValidate="txtTransfert1"
Display="Dynamic" ErrorMessage="*" ValidationExpression="^[0-9]*$">*</asp:RegularExpressionValidator>
<asp:TextBox runat="server" ID="txtTransfert2" CssClass="valuetxt2" Width="50px"
Text='<%# Eval("transfert2") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="revTransfert2" runat="server" ControlToValidate="txtTransfert2"
Display="Dynamic" ErrorMessage="*" ValidationExpression="^[0-9]*$">*</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblColumn8Text" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblColumn8Value" CssClass="flagtxt" runat="server" Text='<%# GetStatusPicture(Container.DataItem) %>'></asp:Label>
<asp:HiddenField runat="server" ID="hdnPointscontrat" Value='<%#Eval("pointscontrat")%>' />
</ItemTemplate>
</asp:TemplateField>
<%--<asp:BoundField DataField="TransactionID" SortExpression="TransactionID" HeaderText="ID" />--%>
</Columns>
</asp:GridView>
下面的代码是后台的。
protected void Page_Load(object sender, EventArgs e)
{
// Don't display content in edit or design mode
if (!Functions.IsDesignMode())
{
if (!IsPostBack)
{
SetTranslation();
// Clear session
Session.Remove(CHECKED_ITEMS);
}
}
BindContractTransactions("TransactionID");
gvTransactions.PageIndexChanged += new EventHandler(gvTransactions_PageIndexChanged);
}
protected void gvTransactions_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
// Change page index
gvTransactions.PageIndex = e.NewPageIndex;
// Reload data
BindContractTransactions(ValidationHelper.GetString(ViewState[this.ClientID + "orderby"], null));
}
protected void gvTransactions_sort(object sender, GridViewSortEventArgs e)
{
ViewState[this.ClientID + "orderby"] = e.SortExpression;
// Reload data
BindContractTransactions(ValidationHelper.GetString(ViewState[this.ClientID + "orderby"], null));
RepopulateValues();
}
protected void gvTransactions_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
Label lblColumn6Text = e.Row.FindControl("lblColumn6Text") as Label;
if (lblColumn6Text != null)
lblColumn6Text.Text = this.Column6Label;
Label lblColumn7Text = e.Row.FindControl("lblColumn7Text") as Label;
if (lblColumn7Text != null)
lblColumn7Text.Text = this.Column7Label;
Label lblColumn8Text = e.Row.FindControl("lblColumn8Text") as Label;
if (lblColumn8Text != null)
lblColumn8Text.Text = this.Column8Label;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlDestinataire1 = e.Row.FindControl("ddlDestinataire1") as DropDownList;
if (ddlDestinataire1 != null)
{
ddlDestinataire1.DataSource = MemberDataTable;
ddlDestinataire1.DataTextField = "MemberLogin";
ddlDestinataire1.DataValueField = "MemberID";
ddlDestinataire1.DataBind();
}
DropDownList ddlDestinataire2 = e.Row.FindControl("ddlDestinataire2") as DropDownList;
if (ddlDestinataire2 != null)
{
ddlDestinataire2.DataSource = MemberDataTable;
ddlDestinataire2.DataTextField = "MemberLogin";
ddlDestinataire2.DataValueField = "MemberID";
ddlDestinataire2.DataBind();
}
ddlDestinataire1.Items.Insert(0, new ListItem("Aucun", "0"));
ddlDestinataire2.Items.Insert(0, new ListItem("Aucun", "0"));
ddlDestinataire1.Items.Insert(1, new ListItem("Raison sociale", Functions.GetMemberID().ToString()));
ddlDestinataire2.Items.Insert(1, new ListItem("Raison sociale", Functions.GetMemberID().ToString()));
ddlDestinataire1.ClearSelection();
string hdnDestinataire1 = ((HiddenField)e.Row.FindControl("hdnDestinataire1")).Value.ToString();
if (!string.IsNullOrEmpty(hdnDestinataire1))
ddlDestinataire1.Items.FindByValue(hdnDestinataire1).Selected = true;
ddlDestinataire2.ClearSelection();
string hdnDestinataire2 = ((HiddenField)e.Row.FindControl("hdnDestinataire2")).Value.ToString();
if (!string.IsNullOrEmpty(hdnDestinataire2))
ddlDestinataire2.Items.FindByValue(hdnDestinataire2).Selected = true;
}
}
void gvTransactions_PageIndexChanged(object sender, EventArgs e)
{
RepopulateValues();
}
注意PageIndexChanged和PageIndexChanging这两个事件。
PageIndexChanging就是在页码改变的时候,可以理解成在分页前要做的事件。
PageIndexChanged就是在页码改变之后,可以理解成在分页完毕后要做的事件。
下面两个方法用来维护控件的状态,比如dropdownlist和textbox之类。
private void RememberOldValues()
{
Dictionary<int, string> selectedList = new Dictionary<int, string>();
int index = -1;
DropDownList ddlDestinataire1 = null;
DropDownList ddlDestinataire2 = null;
TextBox txtTransfert1 = null;
TextBox txtTransfert2 = null;
string jointValue = string.Empty;
foreach (GridViewRow row in gvTransactions.Rows)
{
index = (int)gvTransactions.DataKeys[row.RowIndex].Value;
ddlDestinataire1 = this.gvTransactions.Rows[row.RowIndex].FindControl("ddlDestinataire1") as DropDownList;
ddlDestinataire2 = this.gvTransactions.Rows[row.RowIndex].FindControl("ddlDestinataire2") as DropDownList;
txtTransfert1 = this.gvTransactions.Rows[row.RowIndex].FindControl("txtTransfert1") as TextBox;
txtTransfert2 = this.gvTransactions.Rows[row.RowIndex].FindControl("txtTransfert2") as TextBox;
// Check in the Session
if (Session[CHECKED_ITEMS] != null)
selectedList = (Dictionary<int, string>)Session[CHECKED_ITEMS];
string destinataire1 = ddlDestinataire1.SelectedValue;
string destinataire2 = ddlDestinataire2.SelectedValue;
string transfert1 = txtTransfert1.Text;
string transfert2 = txtTransfert2.Text;
jointValue = destinataire1 + "|" + transfert1 + "|" + destinataire2 + "|" + transfert2;
if (!selectedList.ContainsKey(index))
selectedList.Add(index, jointValue);
else
selectedList[index] = jointValue;
}
if (selectedList != null && selectedList.Count > 0)
Session[CHECKED_ITEMS] = selectedList;
}
private void RepopulateValues()
{
Dictionary<int, string> selectedList = (Dictionary<int, string>)Session[CHECKED_ITEMS];
if (selectedList != null && selectedList.Count > 0)
{
foreach (GridViewRow row in gvTransactions.Rows)
{
int index = (int)gvTransactions.DataKeys[row.RowIndex].Value;
if (selectedList.ContainsKey(index))
{
TextBox txtTransfert1 = this.gvTransactions.Rows[row.RowIndex].FindControl("txtTransfert1") as TextBox;
TextBox txtTransfert2 = this.gvTransactions.Rows[row.RowIndex].FindControl("txtTransfert2") as TextBox;
DropDownList ddlDestinataire1 = this.gvTransactions.Rows[row.RowIndex].FindControl("ddlDestinataire1") as DropDownList;
DropDownList ddlDestinataire2 = this.gvTransactions.Rows[row.RowIndex].FindControl("ddlDestinataire2") as DropDownList;
string[] rememberedValue = selectedList[index].Split('|');
ddlDestinataire1.ClearSelection();
if (ddlDestinataire1.Items.FindByValue(rememberedValue[0]) != null)
ddlDestinataire1.Items.FindByValue(rememberedValue[0]).Selected = true;
txtTransfert1.Text = rememberedValue[1];
ddlDestinataire2.ClearSelection();
if (ddlDestinataire2.Items.FindByValue(rememberedValue[2]) != null)
ddlDestinataire2.Items.FindByValue(rememberedValue[2]).Selected = true;
txtTransfert2.Text = rememberedValue[3];
}
}
}
}
感谢这些帖子提供支持和参考。
http://www.cnblogs.com/szw104/archive/2007/04/27/729540.html
http://www.cnblogs.com/heekui/archive/2008/06/02/1212051.html
http://blog.csdn.net/del1214/article/details/6613292