绑定GridView
hdList.aspx 文件
<asp:GridView ID="gvwHdList" runat="server" CssClass="tbGvw cb f12" Width="100%" AutoGenerateColumns="False" onrowcommand="gvwHdList_RowCommand"> <Columns> <asp:TemplateField HeaderText="序号" ItemStyle-Width="40px"> <ItemTemplate> <%# gvwHdList.Rows.Count + 1%> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="hd_name" HeaderText="活动名称"> <ItemStyle CssClass="tbTdLeft" /> </asp:BoundField> <asp:BoundField DataField="hdrq_ks" HeaderText="活动开始日期" DataFormatString="{0:yyyy-MM-dd HH:mm}" /> <asp:BoundField DataField="hdrq_js" HeaderText="活动结束日期" DataFormatString="{0:yyyy-MM-dd HH:mm}" /> <asp:BoundField DataField="yyFlgName" HeaderText="是否需要预约"/> <asp:BoundField DataField="zt_name" HeaderText="活动状态" /> <asp:TemplateField HeaderText="操作" HeaderStyle-Width="80px"> <ItemTemplate> <asp:Image ID="imgView" runat="server" ImageUrl="~/images/table_view.gif" ToolTip="查看活动信息" /> <asp:ImageButton ID="ibtnEdit" CommandName="CMDUpdate" CommandArgument='<%# Eval("hd_key") %>' ToolTip="修改活动信息" runat="server" ImageUrl="~/images/table_edit.gif" /> <asp:ImageButton ID="ibtnSend" CommandName="CMDSend" CommandArgument='<%# Eval("hd_key") %>' ToolTip="发布活动信息" runat="server" ImageUrl="~/images/iconButton/icon_virement.gif" OnClientClick="javascript:return confirm('确定要发布该活动吗?');"></asp:ImageButton> <asp:ImageButton ID="ibtnDelete" CommandName="CMDDelete" CommandArgument='<%# Eval("hd_key") %>' ToolTip="取消活动信息" runat="server" ImageUrl="~/images/delete.gif" OnClientClick="javascript:return confirm('确定要取消该活动吗?');"></asp:ImageButton> </ItemTemplate> <HeaderStyle Width="90px"></HeaderStyle> </asp:TemplateField> </Columns> </asp:GridView>
hdList.aspx.cs 文件
protected void BindHdList(int hdzKey) { //绑定GridView gvwHdList.DataSource = hdDal.Get_ActivityByHDZKey(hdzKey); gvwHdList.DataKeyNames = new string[] { "hd_key" }; //设置主键字段 gvwHdList.DataBind(); for (int i = 0; i < gvwHdList.Rows.Count; i++) { Image imgView = (Image)gvwHdList.Rows[i].FindControl("imgView"); ImageButton ibtnEdit = (ImageButton)gvwHdList.Rows[i].FindControl("ibtnEdit"); ImageButton ibtnSend = (ImageButton)gvwHdList.Rows[i].FindControl("ibtnSend"); ImageButton ibtnDelete = (ImageButton)gvwHdList.Rows[i].FindControl("ibtnDelete"); string url = "hdxxView.aspx?hdKey=" + gvwHdList.DataKeys[i]["hd_key"]; imgView.Attributes.Add("OnClick", "window.showModalDialog('" + url + "','','dialogWidth:800px;dialogHeight:700px;help:no;resizable:no;status:no;scroll:auto')"); if (gvwHdList.Rows[i].Cells[5].Text.Trim() == "取消" || gvwHdList.Rows[i].Cells[4].Text.Trim() == "结束") { ibtnEdit.Visible = false; ibtnSend.Visible = false; ibtnDelete.Visible = false; } else if (gvwHdList.Rows[i].Cells[5].Text.Trim() == "已发布") { ibtnSend.Visible = false; } } } protected void gvwHdList_RowCommand(object sender, GridViewCommandEventArgs e) { int i = int.Parse(e.CommandArgument.ToString()); //获取主键字段的值 if (e.CommandName == "CMDUpdate") { //修改活动 Response.Redirect("hdAdd.aspx?hdzKey=" + Convert.ToInt32(ViewState["hdzKey"])+"&hdKey="+i); } else if (e.CommandName == "CMDSend") { //发布活动 if (hdDal.Maint_ActivityStatus(i, 1)) { UI.Alert(this, "该活动发布成功"); BindHdList(Convert.ToInt32(ViewState["hdzKey"])); } else { UI.Alert(this, "该活动发布失败"); } } else if (e.CommandName == "CMDDelete") { //取消活动 if (hdDal.Maint_ActivityStatus(i, 2)) { UI.Alert(this, "该活动已被取消"); BindHdList(Convert.ToInt32(ViewState["hdzKey"])); } else { UI.Alert(this, "该活动取消失败"); } } }