0

Here is the .aspx code:

<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>

This generates the following when the page renders:

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

The page designer would like me to put a div tag between the td and anchor tag so that the resulting html that gets generated will be:

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

I've tried putting this code in the gvLeads_DataBound function:

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;
}

But this results in the tags being put inside the anchor tag as such:

<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>

So the .Text property of the LinkButton control is not what I need, but I don't know what property of what control in the cell to use to put the div text where I want it.

3 Answers3

0

In your columns tag, you can use ItemTemplate tag to combine multiple items into one column. I think that should do what you need.

I believe it will require you to mark the divs with the runat attribute.

Limey
  • 2,642
  • 6
  • 37
  • 62
0

Adding a LiteralControl of a div at the start of the cells control collection should do it.

gvLeads.Rows[i].Cells[0].Controls.AddAt(0, 
      new LiteralControl("<div class=editPanel id=edit" + i + "></div>"));
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • Yes, this worked. Thank you so much. One thing... there needs to be one more closing parenthesis before the semicolon. – user1769025 Jan 21 '13 at 19:57
0

In your Databound

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>"));
Vincent James
  • 1,120
  • 3
  • 16
  • 27