GridView的 OnRowDataBound 事件用法

时间:2022-12-18 15:16:58
 <asp:GridView ID="RptUsers" runat="server" AutoGenerateColumns="False" Width="99%"
CssClass="data_table" AllowPaging="false" OnPageIndexChanging="RptUsers_PageIndexChanging"
OnRowCommand="RptUsers_RowCommand" onrowdatabound="RptUsers_RowDataBound" >
<AlternatingRowStyle BackColor="#fafafa" />
<EmptyDataRowStyle HorizontalAlign="Left" />
<PagerStyle HorizontalAlign="left" />
<RowStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<Columns>
<asp:TemplateField HeaderText="选择" Visible="false" HeaderStyle-Wrap="false">
<HeaderTemplate>
<input id="cbSelAll" onclick="SelectedAllChk(this.checked);" type="checkbox">
</HeaderTemplate>
<ItemTemplate>
<%#GetInputTagHtml(Convert.ToInt32(Eval("Id")))%>
<asp:HiddenField ID="HFId" runat="server" Value='<%# Eval("Id") %>' />
</ItemTemplate>
<ItemStyle Wrap="False" HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="序号" HeaderStyle-Wrap="false">
<ItemTemplate>
<%# (this.AspNetPager1.CurrentPageIndex - ) * this.AspNetPager1.PageSize + Container.DataItemIndex + %>
</ItemTemplate>
<ItemStyle Wrap="False" HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="投票名称" HeaderStyle-Wrap="false">
<ItemTemplate>
<%#Eval("Name")%>
</ItemTemplate>
<ItemStyle Wrap="true" HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="主办单位" HeaderStyle-Wrap="false">
<ItemTemplate>
<%# Eval("Organizer")%>
</ItemTemplate>
<ItemStyle Wrap="true" HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="创建者" HeaderStyle-Wrap="false">
<ItemTemplate>
<%#GetUserName(Convert.ToInt32(Eval("Creater")))%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="开始时间" HeaderStyle-Wrap="false">
<ItemTemplate>
<%# GetDateStr(Eval("StartTime").ToString()) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="结束时间" HeaderStyle-Wrap="false">
<ItemTemplate>
<%# GetDateStr(Eval("EndTime").ToString())%>
</ItemTemplate>
<ItemStyle Wrap="False" HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="创建时间" HeaderStyle-Wrap="false">
<ItemTemplate>
<%#Convert.ToDateTime(Eval("CreateTime").ToString()).ToString("yyyy-MM-dd")%>
</ItemTemplate>
<ItemStyle Wrap="False" HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="投票类型" HeaderStyle-Wrap="false">
<ItemTemplate>
<%#GetActiveType(Eval("ActiveType").ToString())%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="投票IP限制" HeaderStyle-Wrap="false">
<ItemTemplate>
<%#GetIPLimitType(Eval("IPLimitType").ToString())%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="游客投票" HeaderStyle-Wrap="false">
<ItemTemplate>
<%#GetUserLimitType(Eval("UserLimitType").ToString())%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="开启状态" HeaderStyle-Wrap="false">
<ItemTemplate>
<%# GetStateStr(Eval("States").ToString())%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作" HeaderStyle-Wrap="false">
<ItemTemplate>
<%# GetOperateStr(Eval("ActiveType").ToString(), Eval("Id").ToString())%>
<a href='/admin/ActivityInfo/ActivityInfoAdd.aspx?action=edit&id=<%#Eval("Id") %>'>编辑</a>
<asp:LinkButton ID="lkbtnOpen" CommandName="lkbtnOpen" runat="server" CommandArgument='<%# Eval("Id") %>'>开启</asp:LinkButton>
<asp:HiddenField ID="hid_states" runat="server" Value='<%# Eval("States") %>' />
<asp:LinkButton ID="lkbtnVote" CommandName="lkbtnVote" runat="server" CommandArgument='<%# Eval("Id") %>'>投票数据同步</asp:LinkButton>
<asp:LinkButton ID="lkbDelete" CommandName="lkbDelete" runat="server" CommandArgument='<%# Eval("Id") %>' OnClientClick="return confirm('您将清除此投票项目的投票数据,确认删除吗?');">删除</asp:LinkButton>
<a href="javascript:void(0)" onclick="manageWorkVoteInfo(<%#Eval("Id") %>)">投票统计</a>
</ItemTemplate>
<ItemStyle Wrap="False" HorizontalAlign="left" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<img src="/images/no_data.gif" />
</EmptyDataTemplate>
</asp:GridView>
 protected void RptUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
//删除
if (e.CommandName.Equals("lkbDelete"))
{
try
{
Dictionary<string, object> searchConditions = new Dictionary<string, object>();
searchConditions.Add("States",);
searchConditions.Add("ActivityId", Convert.ToInt32(e.CommandArgument.ToString())); IList<WorkInfo> workInfo_list = ServiceFactory.WorkInfoService.QueryWorkInfo(searchConditions);
if (workInfo_list.Count > )
{
JSUtil.MessageBox("该投票下面有图文作品,请先删除图片作品!");
return;
} ActivityInfo model_activeInfo = ServiceFactory.ActivityInfoService.GetById(Convert.ToInt32(e.CommandArgument.ToString()));
string imgpath =model_activeInfo==null?"": model_activeInfo.ImgPath;
ActivityInfo activity = model_activeInfo;
//ServiceFactory.ActivityInfoService.Delete(Convert.ToInt32(e.CommandArgument.ToString()));
IOHelper.DeleteFile(Server.MapPath(imgpath));
UserInfo user=ServiceFactory.UserInfoService.GetById(int.Parse(activity.Creater));
IDictionary<string, object> param = new Dictionary<string, object>();
param.Add("Activity", activity.Id);
param.Add("AdminId", user.Id);
//投票活动和管理员授权列表
IList<RelationAdminActivityInfo> res = ServiceFactory.RelationAdminActivityInfoService.QueryConditions(param);
if (res != null && res.Count > )
{
foreach(var item in res)
{
ServiceFactory.RelationAdminActivityInfoService.Delete(item.Id);
}
}
//删除投票活动 20161216
ServiceFactory.ActivityInfoService.Delete(Convert.ToInt32(e.CommandArgument.ToString())); JSUtil.MessageBox("删除成功!");
BindData();
}
catch
{
JSUtil.MessageBox("删除失败,请稍后再试!");
}
}
//投票同步
else if (e.CommandName.Equals("lkbtnVote"))//作品数和投票数数据同步
{
try
{
int votecount = ;
ActivityInfo activity = ServiceFactory.ActivityInfoService.GetById(int.Parse(e.CommandArgument.ToString()));
//图文投票
if (activity.ActiveType == )
{
Dictionary<string, object> searchConditions = new Dictionary<string, object>();
searchConditions.Add("States", );
searchConditions.Add("ActivityId", Convert.ToInt32(e.CommandArgument.ToString()));
IList<WorkInfo> workInfo_list = ServiceFactory.WorkInfoService.QueryWorkInfo(searchConditions);
if (workInfo_list != null && workInfo_list.Count > )
{
for (int i = ; i < workInfo_list.Count; i++)
{
votecount += workInfo_list[i].TotalVoteCount;
}
}
activity.VoteCount = votecount; //投票数
activity.WorkCount = workInfo_list.Count;//作品数
ServiceFactory.ActivityInfoService.Update(activity);
}
else //文字投票 ToDo
{
}
JSUtil.MessageBox("同步成功!");
BindData();
}
catch
{
JSUtil.MessageBox("同步失败,请稍后再试!");
}
} //开启
else if (e.CommandName.Equals("lkbtnOpen"))//开启
{
try
{
ActivityInfo activity = ServiceFactory.ActivityInfoService.GetById(int.Parse(e.CommandArgument.ToString()));
if (activity.States == )
{
activity.States = ;
activity.UpdateTime = DateTime.Now;
}
else
{
activity.States = ;
activity.UpdateTime = DateTime.Now;
}
ServiceFactory.ActivityInfoService.Update(activity);
JSUtil.MessageBox("操作成功!");
BindData();
}
catch
{
JSUtil.MessageBox("操作失败,请稍后再试!");
}
}
}
 protected void RptUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//用FindControl方法找到模板中的Label控件
HiddenField HiddenField = (HiddenField)e.Row.FindControl("hid_states");// 0不开启 1 开启
LinkButton lkbtnOpen = (LinkButton)e.Row.FindControl("lkbtnOpen"); //开启按钮
if (HiddenField.Value == "")
{
lkbtnOpen.Text = "开启";
}
else
{
lkbtnOpen.Text = "关闭";
}
}
}