GridView中使用LinkButton按钮(工作心得)
前台代码:
<asp:TemplateField HeaderText="审核">
<ItemTemplate>
<asp:LinkButton ID="lbCharge" runat="server" CommandName="charge" OnClientClick="if(!confirm('确认通过审核吗?')) return false"><font color=red>通过</font></asp:LinkButton>
<asp:LinkButton ID="lbDelete" runat="server" CommandName="del"
OnClientClick="if(!confirm('确认取消审核吗?')) return false">取消</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="news_IsLock" HeaderText="审核" />
后台代码:
protected void GridView_NewsList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//**************鼠标放上去显高亮
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#CFE4EF'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E0EEF5'");
//**************结束
LinkButton lbDelete = (LinkButton)e.Row.FindControl("lbDelete");
lbDelete.CommandArgument = e.Row.RowIndex.ToString();
LinkButton lbCharge = (LinkButton)e.Row.FindControl("lbCharge");
lbCharge.CommandArgument = e.Row.RowIndex.ToString();
if (e.Row.Cells[6].Text == "True")
{
//e.Row.Cells[6].Text = "已审核";
//e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;
lbDelete.Visible = true;
lbCharge.Visible = false;
e.Row.Cells[6].Visible = false;
}
else
{
//e.Row.Cells[6].Text = "未审核";
lbDelete.Visible = false;
lbCharge.Visible = true;
e.Row.Cells[6].Visible = false;
}
}
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[6].Visible = false;
}
}
protected void GridView_NewsList_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = int.Parse(e.CommandArgument.ToString());
DataKey key = this.GridView_NewsList.DataKeys[index];
Int64 payid = Int64.Parse(key.Value.ToString());
switch (e.CommandName)
{
case "charge":
{
//string mysql = "update News set fd_IsLock=1 where fd_Id=" + payid;
//Conn.ExecuteSql(mysql);
FdNews.CloseLockNews(payid);
BindGrid();
break;
}
case "del":
{
//string mysql = "update News set fd_IsLock=0 where fd_Id=" + payid;
//Conn.ExecuteSql(mysql);
FdNews.OpenLockNews(payid);
BindGrid();
break;
}
}
}
|