2013-01-21 32 views
0

這裏是一個LinkBut​​ton生成的代碼是在.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> 

所以LinkBut​​ton的的。文本屬性控制不是我所需要的,但我不知道單元格中的什麼控件用於將div文本放在需要的位置。

回答

0

在您的列標記中,您可以使用ItemTemplate標記將多個項目合併到一個列中。我認爲這應該做你需要的。

我相信它會要求你用runat屬性標記div。

0

在單元格控件集合的開頭添加一個div的LiteralControl應該這樣做。

gvLeads.Rows[i].Cells[0].Controls.AddAt(0, 
     new LiteralControl("<div class=editPanel id=edit" + i + "></div>")); 
+0

是的,這個工作。非常感謝。有一件事......在分號之前需要再加一個右括號。 – user1769025

+0

@ user1769025對! – Magnus

0

在您的數據綁定

e.Row.Cells[0].Controls.AddAt(0, new LiteralControl("<div class=editPanel id=edit" + e.Row.RowIndex.ToString() + "><div>")); 
e.Row.Cells[1].Controls.AddAt(0, new LiteralControl("<div class=detailPanel id=detail" + e.Row.RowIndex.ToString() + "><div>")); 
+0

是的,就是這樣。 – user1769025