SkeetJon
SkeetJon

Reputation: 1491

How to dynamically populate the ID of a column in a bound ASP.NET GridView

I have an ASP.NET GridView as follows...

<asp:GridView ID="grdUserDecisions" runat="server" CssClass="Grid" 
              OnPageIndexChanging="grdUserDecisions_PageIndexChanging" 
              OnSorting="grdUserDecisions_Sorting">
   <HeaderStyle CssClass="GridHeader"/>
   <AlternatingRowStyle CssClass="GridAltItem"/>
   <RowStyle CssClass="GridItem"/>
   <PagerStyle CssClass="GridPager"/>
   <Columns>
       <%-- I don't want a link --%>
       <asp:HyperLinkField 
            DataNavigateUrlFields="Name" 
            DataNavigateUrlFormatString="/default.aspx?id={0}"
            DataTextField="Name" HeaderText="Name" />

       <%-- I want the id value to be set in the DOM --%>
       <asp:ButtonField ID='<%# Eval("Name") %>' DataField="Name" />
    </Columns>
</asp:GridView>

Is it possible to add a Bound column with an ID value in the DOM that is populated dynamically please?

This line doesn't work, as there is no ID property. Is there a different column type to use?

<asp:ButtonField ID='<%# Eval("Name") %>' DataField="Name" />

Thanks in advance.

Upvotes: 0

Views: 1704

Answers (1)

mshsayem
mshsayem

Reputation: 18008

Try using a TemplateField like this:

<Columns>
...
<asp:TemplateField>
    <ItemTemplate>      
        <input type="button"  ID='<%# Eval("Name") %>' value='<%# Eval("Name") %>'/>
    </ItemTemplate>
</asp:TemplateField>
...
</Columns>

Upvotes: 1

Related Questions