在ASP.NET WEBFORM 中后台实现gridview全选功能

上面是效果图。

 

 

------

前台代码:

<asp:GridView ID="CardID" runat="server" AllowPaging="True" PageSize="20" AutoGenerateColumns="False"
OnPageIndexChanging="CardID_PageIndexChanging" Width="1805px" OnRowDataBound="CardID_RowDataBound"
DataKeyNames="tid" OnRowCommand="CardID_RowCommand">
<HeaderStyle HorizontalAlign="Center" BackColor="SkyBlue"></HeaderStyle>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True" OnCheckedChanged="chkAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkItem" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%#Container.DataItemIndex+1%>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:LinkButton ID="lbtnQianRu" runat="server" CommandName="tid" CommandArgument='<%# Eval("tid") %>'>详情</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>
</asp:TemplateField>
<%-- <asp:CommandField HeaderText="编辑" ItemStyle-HorizontalAlign="Center" ShowEditButton="True"
EditText="详情">
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>--%>
<asp:TemplateField HeaderText="是否上传身份证" SortExpression="a.isload Desc">
<ItemTemplate>
<asp:Label ID="lblIsload" Text='<%# DataBinder.Eval(Container.DataItem, "isload")%>'
runat="server"></asp:Label>
<asp:Label ID="lblisloadID" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "isload")%>'
runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="receiver_name" HeaderText="收货人" SortExpression="receiver_name"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="buyer_nick" HeaderText="买家账号" SortExpression="buyer_nick"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="seller_nick" HeaderText="网店店名" SortExpression="seller_nick"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="tid" HeaderText="订单号" SortExpression="tid" ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="pay_time" HeaderText="付款时间" SortExpression="pay_time"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="modified" HeaderText="下单时间" SortExpression="modified"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" Height=""></ItemStyle>
</asp:BoundField>
</Columns>
<%-- <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />--%>
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<PagerTemplate>
<br />
<asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
<asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'
CommandName="Page" CommandArgument="First"></asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'
CommandName="Page" CommandArgument="Prev"></asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
CommandName="Page" CommandArgument="Next"></asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
CommandName="Page" CommandArgument="Last"></asp:LinkButton>
</PagerTemplate>
</asp:GridView>

---

后台事件:

//全选checkbox事件
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < this.CardID.Rows.Count; i++)
{
((CheckBox)CardID.Rows[i].FindControl("chkItem")).Checked =
((CheckBox)this.CardID.HeaderRow.FindControl("chkAll")).Checked;
}
}

//是否上传
protected void Button2_Click(object sender, EventArgs e)
{
#region 选择checkbox验证身份证是否上传的状态
for (int i = 0; i < this.CardID.Rows.Count; i++)
{
CheckBox cb = ((CheckBox)CardID.Rows[i].FindControl("chkItem")) as CheckBox;
string tid = this.CardID.DataKeys[i].Value.ToString();//在datakeyname属性里面设置了tid
if (cb != null && cb.Checked)
{
DataTable dt = DbHelperSQL.QueryDT("select isLoad,receiver_name from tbtrade where tid='" + tid + "'");
if (dt.Rows.Count > 0)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://qingguanid.com/api/search.php?n=" + dt.Rows[0]["receiver_name"].ToString());
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding);
string htmlstr = reader.ReadToEnd();
reader.Close();
response.Close();
int isload = 0;
if (htmlstr.Contains("not found"))
{
isload = 0;
}
else
{
if (htmlstr.IndexOf(": confirmed") > -1)
{
isload = 1;
}
else
{
isload = 0;
}
}
string sql = @"update tbtrade set isload=" + isload + " where tid='" + tid + "'";
DbHelperSQL.ExecuteSql(sql);
}
catch (Exception error)
{
Response.Write(error.ToString());
}
}
}
}
bind();
#endregion
}

posted @ 2016-01-28 11:46  何波  阅读(786)  评论(0编辑  收藏  举报