?
Solved

Dynamic Control ID's

Posted on 2004-04-21
5
Medium Priority
?
845 Views
Last Modified: 2008-02-01
Is it possible (and if so how) to dynamically create ID's for .NET "buttons" when using the repeater control. I have a table which holds a list of button names / button text strings. I bind my repeater to the returned data set and then I want to loop through the returned records and create a button for each returned record with the asp:button having the ID as specified in the table. I've tried using :

<asp:button id=<%#DataBinder.Eval(Container.DataItem, "ButtonID")%> Text='<%#DataBinder.Eval(Container.DataItem, "ButtonText")%>' runat="server" cssclass="buttonStyle"></asp:button>

This doesnt work and displays an error showing :

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

If I just set the ID to a fixed name e.g.btnTest then it displays fine and each button displays the correct text.

I'm fairly new to .NET so if you could dumb down any answers it would be appreciated :-)

Cheers

Rob
0
Comment
Question by:robert_f
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 1000 total points
ID: 10886157
there are two ways of doing this
1. If you want to assign the ID not anything else you have to create the button controls dynamically - BUT you will have to recreate these controls each time you enter the page ( ispostback and not ispostback) which is boring
2. you can create the buttons with ID="btnTest" but use the CommandName to add the value from the table, like this
<asp:button ID="btnFromTable" CommandName=<%#DataBinder.Eval(Container.DataItem, "ButtonID")%> Text='<%#DataBinder.Eval(Container.DataItem, "ButtonText")%>' runat="server" cssclass="buttonStyle"></asp:button>
the create the ItemCommand event and you can use it to determine which button is pressed by e.CommandName property

HTH
B..M

0
 

Author Comment

by:robert_f
ID: 10886328
mmarinov

Thanks for that .... option 2 definately looks like a winner ... however could you provide a little more detail about the "ItemCommand" / e.CommandName part ?

Thanks

Rob
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10886416
the event ItemCommand will fired when the user submit through a control from the repeater
in it youcan determine what is the name of the command - standart it "edit", "delete" and so on
so you can check what is the name of the command and to whatever you want

here is a little example
public void dgLines_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
  Reponse.Redirect ( "yourpage.asp?type=" & e.CommandName)
}

or
public void dgLines_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
  DataTable dt_session = (DataTable)Session["datasource"];
  for( int i=0; i < dt_session.Rows.Count; i++ )
   {
     if ( dt_session.Rows[i]["ButtonID"].ToString() == e.CommandName )
      {
       // create the functionality
       }
   }
}

all depends on what you want to do and what kind of information you keep in ButtonID field in the DB and why you keep this information there

here is the link to the ItemCommand of the Repeater control
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsRepeaterClassItemCommandTopic.asp

HTH
B..M
0
 

Author Comment

by:robert_f
ID: 10886482
Excellent !! Just starting to use .NET and trying to get my head around a few of the basics .... mainly by trying to recreate a few standard ASP projects. You've saved me a couple of days of banging my head against a wall wondering why I ever decided to switch :-)

Thanks again for all your help

Rob
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10886517
Not a problem
The EE is just for that!
wish luck with .net

B..M
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question