这是 .aspx 代码:
<asp:GridView ID="gvLeads" runat="server" DataSourceID="sdsAdminLeads" AllowSorting="True"
EnableModelValidation="True" DataKeyNames="leadKey" AutoGenerateColumns="False"
OnSelectedIndexChanged="gvLeads_selected" OnRowCommand="gvLeads_RowCommand" OnDataBound="gvLeads_DataBound">
<Columns>
<asp:CommandField ShowSelectButton="true" SelectText="Edit" ButtonType="Link" />
<asp:ButtonField CommandName="Details" Text="Details" />
...(other BoundFields)
</Columns>
</asp:GridView>
这会在页面呈现时生成以下内容:
<td><a href="javascript:__doPostBack('ucLeads$gvLeads','Select$0')">Edit</a></td>
<td><a href="javascript:__doPostBack('ucLeads$gvLeads','Details$0')">Details</a></td>
...
<td><a href="javascript:__doPostBack('ucLeads$gvLeads','Select$1')">Edit</a></td>
<td><a href="javascript:__doPostBack('ucLeads$gvLeads','Details$1')">Details</a></td>
...
页面设计者希望我在 td 和锚标记之间放置一个 div 标记,以便生成的结果 html 将是:
<td><div class=editPanel id=edit0></div><a href="javascript:__doPostBack('ucLeads$gvLeads','Select$0')">Edit</a></td>
<td><div class=detailPanel id=detail0></div><a href="javascript:__doPostBack('ucLeads$gvLeads','Details$0')">Details</a></td>
...
<td><div class=editPanel id=edit1></div><a href="javascript:__doPostBack('ucLeads$gvLeads','Select$1')">Edit</a></td>
<td><div class=detailPanel id=detail1></div><a href="javascript:__doPostBack('ucLeads$gvLeads','Details$1')">Details</a></td>
...
我试过把这段代码放在 gvLeads_DataBound 函数中:
for (int i = 0; i < gvLeads.Rows.Count; i++)
{
((LinkButton)gvLeads.Rows[i].Cells[0].Controls[0]).Text = "<div class=editPanel id=edit" + i + "></div>" + ((LinkButton)gvLeads.Rows[i].Cells[0].Controls[0]).Text;
((LinkButton)gvLeads.Rows[i].Cells[1].Controls[0]).Text = "<div class=detailPanel id=detail" + i + "></div>" + ((LinkButton)gvLeads.Rows[i].Cells[1].Controls[0]).Text;
}
但这会导致标签被放置在锚标签中,如下所示:
<td><a href="javascript:__doPostBack('ucLeads$gvLeads','Select$0')"><div class=editPanel id=edit0></div>Edit</a></td>
<td><a href="javascript:__doPostBack('ucLeads$gvLeads','Details$0')"><div class=detailPanel id=detail0></div>Details</a></td>
...
<td><a href="javascript:__doPostBack('ucLeads$gvLeads','Select$1')"><div class=editPanel id=edit1></div>Edit</a></td>
<td><a href="javascript:__doPostBack('ucLeads$gvLeads','Details$1')"><div class=detailPanel id=detail1></div>Details</a></td>
所以 LinkButton 控件的 .Text 属性不是我需要的,但我不知道单元格中什么控件的什么属性用于将 div 文本放在我想要的位置。