這裏是一個LinkButton生成的代碼是在.aspx代碼:C#我需要幫助插入文本在GridView
<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的的。文本屬性控制不是我所需要的,但我不知道單元格中的什麼控件用於將div文本放在需要的位置。
是的,這個工作。非常感謝。有一件事......在分號之前需要再加一個右括號。 – user1769025
@ user1769025對! – Magnus