前端:
1、 给GridView中添加一模板列,将数据(ID)直接绑定到按钮的CommandArgument中,在RowCommand事件中的e.CommandArguemnt就能读出绑定的数据了。这样只能绑定一个数据。 要实现多数据绑定,通过CommandArgument=<’Container.DataItemIndex’>绑定行的索引号。
2、要实现不能重复提交,要使用LinkButton 的Enabled属性使用后台方法进行格式化,返回True或者False。
<asp:TemplateField HeaderText="基本操作>
<ItemTemplate>
<asp:LinkButton ID="lbtn_Submmit" runat="server" Text='提交' CommandName="Submmit" CommandArgument='<%#Eval("MajorID")%>' OnClientClick="return confirm('确定提交吗?提交后将不可更改。)" Enabled='<%# FormatBool(Eval("ReviewSubmmitState").ToString()) %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
后台:通过CommandName参数选中触发的按钮,通过CommandArgument获得绑定的行数据。如果绑定的数据是GUID数据,要使用 new Guid(e.CommandArgument.ToString())来转换。
protected void gv_ReviewIndex_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Submmit")
{
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("update tbReview set ReviewSubmmitState ='已提交' where ReviewMajorID='" + new Guid(e.CommandArgument.ToString()) + "' and ReviewUserID='" + Session["UserID"].ToString() + "'", conn))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("<script>alert('{0}条记录提交成功。”);location.href=location.href</script>", cmd.ExecuteNonQuery()));
}
}
}
SetBind();
}
实现提交后按钮不能再次提交,后台方法格式化。
public bool FormatBool(string strSubmmitState)
{
if (strSubmmitState.Trim() == "已提交?")
return false;
else
return true;
}