Repeater控件的两个问题解决
今天使用Rpt中要实现的功能~
注意:AlternatingItemTemplate,ItemTemplate都要有相应的控件不然找不到的。
ItemDataBound事件中查找控件:
<asp:HyperLink ID="HLinkModify" NavigateUrl='<%# "Admin_User.aspx?id="+Eval("id")%>' Target="_self" runat="server">点击修改</asp:HyperLink>
protected void RptUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
{
DataRowView rowv = (DataRowView)e.Item.DataItem;
if (rowv["username"].ToString() != Session["admin"].ToString())
{
HyperLink HLinkModify = (HyperLink)e.Item.FindControl("HLinkModify");
HLinkModify.Visible = false;
}
}
}
Rpt中控件的事件使用:
<asp:LinkButton ID="LBtnDel" OnClick="LBtnDel_OnClick" ToolTip='<%# Eval("username") %>' runat="server">删除</asp:LinkButton>
//Rpt中控件的事件
protected void LBtnDel_OnClick(object sender,EventArgs e)
{
LinkButton LBtnDel = sender as LinkButton;//触发事件的LinkButton
//RepeaterItem item = LBtnDel.NamingContainer as RepeaterItem; //LinkButton所在行
string username = LBtnDel.ToolTip.ToString();
if (username != Session["admin"].ToString())
{
RegJscript.AlertAndRedirect(Page, "对不起,您没有此权限!", "Admin_User.aspx");
}
else
{
AdminInfo.Where = " where username='" + username + "'";
if (Admin.DeleteAdmin(AdminInfo) > 0)
{
Session.RemoveAll();
RegJscript.AlertAndRedirect(Page, "删除成功,请重新登陆!", "Admin_Login.aspx");
}
else
{
RegJscript.AlertAndRedirect(Page, "删除失败!", "Admin_User.aspx");
}
}
}