0

这是 .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>

所以 LinkBut​​ton 控件的 .Text 属性不是我需要的,但我不知道单元格中什么控件的什么属性用于将 div 文本放在我想要的位置。

4

3 回答 3

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>"));
于 2013-01-21T19:34:52.953 回答
0

在您的列标签中,您可以使用 ItemTemplate 标签将多个项目组合成一列。我认为这应该做你需要的。

我相信它会要求您使用 runat 属性标记 div。

于 2013-01-21T19:27:02.693 回答
0

LiteralControl在单元格控件集合的开头添加一个div 应该可以做到这一点。

gvLeads.Rows[i].Cells[0].Controls.AddAt(0, 
      new LiteralControl("<div class=editPanel id=edit" + i + "></div>"));
于 2013-01-21T19:33:23.533 回答