How to make asp:Button ID unique in a DataGrid?

R

Randall Parker

I have this element in an asp:DataGrid:

<asp:TemplateColumn HeaderText="Edit Record Button">
<ItemTemplate>
<asp:Button ID='<%# "Edit" + DataBinder.Eval(Container,
"DataItem.owner_serial_num") %>'
CommandName="EquipmentEdit"
CommandArgument='<%# DataBinder.Eval(Container,
"DataItem.owner_serial_num") %>'
Runat="server"
Text='<%# "Edit " + DataBinder.Eval(Container,
"DataItem.owner_serial_num") %>'
/>
</ItemTemplate>
</asp:TemplateColumn>


I get this error when I try to view the page and I do not understand why the
expression I'm using to build the ID is not accepted:

Parser Error Message: '<%# "Edit" + DataBinder.Eval(Container,
"DataItem.owner_serial_num") %>' is not a valid identifier.

Source Error:

Line 21: <asp:TemplateColumn HeaderText="Edit Record Button">
Line 22: <ItemTemplate>
Line 23: <asp:Button ID='<%# "Edit" + DataBinder.Eval(Container,
"DataItem.owner_serial_num") %>'
Line 24: CommandName="EquipmentEdit"
Line 25: CommandArgument='<%# DataBinder.Eval(Container,
"DataItem.owner_serial_num") %>'

How to do this? I just want a button in a grid cell that the user can click on.


If I do not try to make the ID unique then I get a different error when I go to click
on a button in the grid:

Multiple controls with the same ID 'hyperlink1' were found. FindControl requires that
controls have unique IDs.
 
U

Usenet Honey Pot

I get this error when I try to view the page and I do not understand
why the expression I'm using to build the ID is not accepted:

Parser Error Message: '<%# "Edit" + DataBinder.Eval(Container,
"DataItem.owner_serial_num") %>' is not a valid identifier.

I would bind it in code behind - it'll be eaiser to maintain too : )
 
R

Randall Parker

Stan,

I do not understand your suggestion. Bind what in CodeBehind?

How can I make each ID on each button on row different?
 
G

Gaurav Vaish

: Parser Error Message: '<%# "Edit" + DataBinder.Eval(Container,
: "DataItem.owner_serial_num") %>' is not a valid identifier.


Don't attempt to provide an ID yourself.

Or if at all.. give a simple plain ID.

To get the item back, work the DataGridItem - which is derived from
TableRow. Each item represents full row.
Then you can do something like (say, in ItemCommand event handler):

TableCell cell = e.Item.Cells[index];
Button b = (Button) cell.FindControl("ID-that-you-gave");


HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top