• GridView的 OnRowDataBound 事件用法


     1 <asp:GridView ID="RptUsers" runat="server" AutoGenerateColumns="False" Width="99%"
     2         CssClass="data_table" AllowPaging="false" OnPageIndexChanging="RptUsers_PageIndexChanging"
     3         OnRowCommand="RptUsers_RowCommand" onrowdatabound="RptUsers_RowDataBound" >
     4         <AlternatingRowStyle BackColor="#fafafa" />
     5         <EmptyDataRowStyle HorizontalAlign="Left" />
     6         <PagerStyle HorizontalAlign="left" />
     7         <RowStyle HorizontalAlign="Left" />
     8         <HeaderStyle HorizontalAlign="Left" />
     9         <Columns>
    10             <asp:TemplateField HeaderText="选择" Visible="false" HeaderStyle-Wrap="false">
    11                 <HeaderTemplate>
    12                     <input id="cbSelAll" onclick="SelectedAllChk(this.checked);" type="checkbox">
    13                 </HeaderTemplate>
    14                 <ItemTemplate>
    15                     <%#GetInputTagHtml(Convert.ToInt32(Eval("Id")))%>
    16                     <asp:HiddenField ID="HFId" runat="server" Value='<%# Eval("Id") %>' />
    17                 </ItemTemplate>
    18                 <ItemStyle Wrap="False" HorizontalAlign="left" />
    19             </asp:TemplateField>
    20             <asp:TemplateField HeaderText="序号" HeaderStyle-Wrap="false">
    21                 <ItemTemplate>
    22                     <%# (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize + Container.DataItemIndex + 1%>
    23                 </ItemTemplate>
    24                 <ItemStyle Wrap="False" HorizontalAlign="left" />
    25             </asp:TemplateField>
    26             <asp:TemplateField HeaderText="投票名称" HeaderStyle-Wrap="false">
    27                 <ItemTemplate>
    28                     <%#Eval("Name")%>
    29                 </ItemTemplate>
    30                 <ItemStyle Wrap="true" HorizontalAlign="left" />
    31             </asp:TemplateField>
    32             <asp:TemplateField HeaderText="主办单位" HeaderStyle-Wrap="false">
    33                 <ItemTemplate>
    34                     <%# Eval("Organizer")%>
    35                 </ItemTemplate>
    36                 <ItemStyle Wrap="true" HorizontalAlign="left" />
    37             </asp:TemplateField>
    38             <asp:TemplateField HeaderText="创建者" HeaderStyle-Wrap="false">
    39                 <ItemTemplate>
    40                     <%#GetUserName(Convert.ToInt32(Eval("Creater")))%>
    41                 </ItemTemplate>
    42             </asp:TemplateField>
    43             <asp:TemplateField HeaderText="开始时间" HeaderStyle-Wrap="false">
    44                 <ItemTemplate>
    45                     <%# GetDateStr(Eval("StartTime").ToString()) %>
    46                 </ItemTemplate>
    47             </asp:TemplateField>
    48             <asp:TemplateField HeaderText="结束时间" HeaderStyle-Wrap="false">
    49                 <ItemTemplate>
    50                     <%# GetDateStr(Eval("EndTime").ToString())%>
    51                 </ItemTemplate>
    52                 <ItemStyle Wrap="False" HorizontalAlign="left" />
    53             </asp:TemplateField>
    54             <asp:TemplateField HeaderText="创建时间" HeaderStyle-Wrap="false">
    55                 <ItemTemplate>
    56                     <%#Convert.ToDateTime(Eval("CreateTime").ToString()).ToString("yyyy-MM-dd")%>
    57                 </ItemTemplate>
    58                 <ItemStyle Wrap="False" HorizontalAlign="left" />
    59             </asp:TemplateField>
    60             <asp:TemplateField HeaderText="投票类型" HeaderStyle-Wrap="false">
    61                 <ItemTemplate>
    62                     <%#GetActiveType(Eval("ActiveType").ToString())%>
    63                 </ItemTemplate>
    64             </asp:TemplateField>
    65             <asp:TemplateField HeaderText="投票IP限制" HeaderStyle-Wrap="false">
    66                 <ItemTemplate>
    67                     <%#GetIPLimitType(Eval("IPLimitType").ToString())%>
    68                 </ItemTemplate>
    69             </asp:TemplateField>
    70             <asp:TemplateField HeaderText="游客投票" HeaderStyle-Wrap="false">
    71                 <ItemTemplate>
    72                     <%#GetUserLimitType(Eval("UserLimitType").ToString())%>
    73                 </ItemTemplate>
    74             </asp:TemplateField>
    75             <asp:TemplateField HeaderText="开启状态" HeaderStyle-Wrap="false">
    76                 <ItemTemplate>
    77                     <%# GetStateStr(Eval("States").ToString())%>
    78                 </ItemTemplate>
    79             </asp:TemplateField>
    80             <asp:TemplateField HeaderText="操作" HeaderStyle-Wrap="false">
    81                 <ItemTemplate>
    82                      <%# GetOperateStr(Eval("ActiveType").ToString(), Eval("Id").ToString())%>
    83                     <a href='/admin/ActivityInfo/ActivityInfoAdd.aspx?action=edit&id=<%#Eval("Id") %>'>编辑</a>
    84                     <asp:LinkButton ID="lkbtnOpen" CommandName="lkbtnOpen" runat="server" CommandArgument='<%# Eval("Id") %>'>开启</asp:LinkButton>
    85                     <asp:HiddenField ID="hid_states" runat="server" Value='<%# Eval("States") %>' />
    86                     <asp:LinkButton ID="lkbtnVote" CommandName="lkbtnVote" runat="server" CommandArgument='<%# Eval("Id") %>'>投票数据同步</asp:LinkButton>
    87                     <asp:LinkButton ID="lkbDelete" CommandName="lkbDelete" runat="server" CommandArgument='<%# Eval("Id") %>' OnClientClick="return confirm('您将清除此投票项目的投票数据,确认删除吗?');">删除</asp:LinkButton>
    88                     <a href="javascript:void(0)" onclick="manageWorkVoteInfo(<%#Eval("Id") %>)">投票统计</a>
    89                 </ItemTemplate>
    90                 <ItemStyle Wrap="False" HorizontalAlign="left" />
    91             </asp:TemplateField>
    92         </Columns>
    93         <EmptyDataTemplate>
    94             <img src="/images/no_data.gif" />
    95         </EmptyDataTemplate>
    96     </asp:GridView>
    View Code
      1 protected void RptUsers_RowCommand(object sender, GridViewCommandEventArgs e)
      2     {
      3         //删除
      4         if (e.CommandName.Equals("lkbDelete"))
      5         {
      6             try
      7             {  
      8                 Dictionary<string, object> searchConditions = new Dictionary<string, object>();
      9                 searchConditions.Add("States",0);
     10                 searchConditions.Add("ActivityId", Convert.ToInt32(e.CommandArgument.ToString()));
     11 
     12                 IList<WorkInfo> workInfo_list = ServiceFactory.WorkInfoService.QueryWorkInfo(searchConditions);
     13                 if (workInfo_list.Count > 0)
     14                 {
     15                     JSUtil.MessageBox("该投票下面有图文作品,请先删除图片作品!");
     16                     return;
     17                 }
     18 
     19                 ActivityInfo model_activeInfo = ServiceFactory.ActivityInfoService.GetById(Convert.ToInt32(e.CommandArgument.ToString()));
     20                 string imgpath =model_activeInfo==null?"": model_activeInfo.ImgPath;
     21                 ActivityInfo activity = model_activeInfo;
     22                 //ServiceFactory.ActivityInfoService.Delete(Convert.ToInt32(e.CommandArgument.ToString()));
     23                 IOHelper.DeleteFile(Server.MapPath(imgpath));
     24                 UserInfo user=ServiceFactory.UserInfoService.GetById(int.Parse(activity.Creater));
     25                 IDictionary<string, object> param = new Dictionary<string, object>();
     26                 param.Add("Activity", activity.Id);
     27                 param.Add("AdminId", user.Id);
     28                 //投票活动和管理员授权列表
     29                 IList<RelationAdminActivityInfo> res = ServiceFactory.RelationAdminActivityInfoService.QueryConditions(param);
     30                 if (res != null && res.Count > 0)
     31                 {
     32                     foreach(var item in res) 
     33                     {
     34                         ServiceFactory.RelationAdminActivityInfoService.Delete(item.Id);
     35                     }
     36                 }
     37                 //删除投票活动 20161216
     38                 ServiceFactory.ActivityInfoService.Delete(Convert.ToInt32(e.CommandArgument.ToString()));
     39 
     40                 JSUtil.MessageBox("删除成功!");
     41                 BindData();
     42             }
     43             catch
     44             {
     45                 JSUtil.MessageBox("删除失败,请稍后再试!");
     46             }
     47         }
     48         //投票同步
     49         else if (e.CommandName.Equals("lkbtnVote"))//作品数和投票数数据同步
     50         {
     51             try
     52             {
     53                 int votecount = 0;
     54                 ActivityInfo activity = ServiceFactory.ActivityInfoService.GetById(int.Parse(e.CommandArgument.ToString()));
     55                 //图文投票
     56                 if (activity.ActiveType == 1)
     57                 {
     58                     Dictionary<string, object> searchConditions = new Dictionary<string, object>();
     59                     searchConditions.Add("States", 1);
     60                     searchConditions.Add("ActivityId", Convert.ToInt32(e.CommandArgument.ToString()));
     61                     IList<WorkInfo> workInfo_list = ServiceFactory.WorkInfoService.QueryWorkInfo(searchConditions);
     62                     if (workInfo_list != null && workInfo_list.Count > 0)
     63                     {
     64                         for (int i = 0; i < workInfo_list.Count; i++)
     65                         {
     66                             votecount += workInfo_list[i].TotalVoteCount;
     67                         }
     68                     }
     69                     activity.VoteCount = votecount; //投票数
     70                     activity.WorkCount = workInfo_list.Count;//作品数
     71                     ServiceFactory.ActivityInfoService.Update(activity);
     72                 }
     73                 else //文字投票 ToDo
     74                 { 
     75                 }
     76                 JSUtil.MessageBox("同步成功!");
     77                 BindData();
     78             }
     79             catch
     80             {
     81                 JSUtil.MessageBox("同步失败,请稍后再试!");
     82             }
     83         }
     84 
     85         //开启
     86         else if (e.CommandName.Equals("lkbtnOpen"))//开启
     87         {
     88             try
     89             {        
     90                 ActivityInfo activity = ServiceFactory.ActivityInfoService.GetById(int.Parse(e.CommandArgument.ToString()));
     91                 if (activity.States == 0)
     92                 {
     93                     activity.States = 1;
     94                     activity.UpdateTime = DateTime.Now;
     95                 }
     96                 else
     97                 {
     98                     activity.States = 0;
     99                     activity.UpdateTime = DateTime.Now;
    100                 }
    101                 ServiceFactory.ActivityInfoService.Update(activity);
    102                 JSUtil.MessageBox("操作成功!");
    103                 BindData();
    104             }
    105             catch
    106             {
    107                 JSUtil.MessageBox("操作失败,请稍后再试!");
    108             }
    109         }
    110     }
    View Code
     1 protected void RptUsers_RowDataBound(object sender, GridViewRowEventArgs e)
     2     {
     3         if (e.Row.RowType == DataControlRowType.DataRow)
     4         {
     5             //用FindControl方法找到模板中的Label控件
     6             HiddenField HiddenField = (HiddenField)e.Row.FindControl("hid_states");// 0不开启 1 开启
     7             LinkButton lkbtnOpen = (LinkButton)e.Row.FindControl("lkbtnOpen"); //开启按钮
     8             if (HiddenField.Value == "0")
     9             {
    10                 lkbtnOpen.Text = "开启";             
    11             }
    12             else
    13             {
    14                 lkbtnOpen.Text = "关闭";
    15             }
    16         }
    17     }
    View Code
  • 相关阅读:
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业03
    C语言I博客作业02
    第一次作业
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
  • 原文地址:https://www.cnblogs.com/hgmyz/p/6186768.html
Copyright © 2020-2023  润新知