如何根据其他列的值改变ButtonField 字段显示的文字
在GridView 中有一个 ButtonField 先设置 Text="编辑"
<asp:ButtonField CommandName="EditProject" Text="编辑">
<ItemStyle ForeColor="#0033CC" Width="40px" />
</asp:ButtonField>
如:flag=0,text ="编辑";
根据 该列的值动态改变 ButtonField的Text
如:flag=1,text="查看"
“编辑”时 点击进入编辑页面
“查看”时点击进入查看页面。
我的做法是:
在 *.aspx 中:
<asp:ButtonField CommandName="EditProject" Text="编辑">
<ItemStyle ForeColor="#0033CC" Width="40px" />
</asp:ButtonField>
在 *.aspx.cs中:
protected void grdProject_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text != "未提交")
{
e.Row.Cells[15].Text = "查看";
}
e.Row.Attributes.Add("onclick", "ItemOver(this)");
}
}
protected void grdProject_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditProject")
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
Session["PROJECT_ID"] = grdProject.Rows[rowIndex].Cells[17].Text;
string ss = grdProject.Rows[rowIndex].Cells[15].Text;
if (grdProject.Rows[rowIndex].Cells[15].Text == "编辑")
{
Response.Redirect("EditProject.aspx");
}
}
}
出现的问题:
点击编辑 grdProject.Rows[rowIndex].Cells[15].Text为空值,查看不能点击进去