就我使用到的情况先总结一下:
1、把模版列绑定的字段放在gridview的DataKeyNames里面,如DataKeyNames="id,is_used"
然后在后台的代码里面这样处理:
在行绑定的时候
protected void sgvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string strIsued = this.sgvList.DataKeys[e.Row.RowIndex][2].ToString());
}
}
2、在后台写个方法绑定到前台的html代码中
protected string ChangeIDtoName(object data)
{
string strIsUse = data.ToString();
string strUse="";
if (strID == "True")
{
strUse="使用";
}
else if (strID == "False")
{
strUse= "不使用";
}
return strName;
}
前台代码:
<%#ChangeIDtoName(DataBinder.Eval(Container, "DataItem.is_used"))%>
3、使用控件
<asp:HyperLink ID="hlkCustomer" runat="server" NavigateUrl='<%#"~/Customer/EditCustomer.aspx?id="+Eval("Customer") %>' Text='<%# Eval("Customer") %>'></asp:HyperLink>
protected void sgvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hlkCustomer = (HyperLink)e.Row.FindControl("hlkCustomer");
objEntity = getEntity(this.sgvList.DataKeys[e.Row.RowIndex][3].ToString());//一个获取名字的方法
if (objEntity != null)
{
hlkCustomer.Text = objEntity.Name;
}
else
{
hlkCustomer.Text = "";
}