Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamic Control ID's

Posted on 2004-04-21
5
Medium Priority
?
853 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 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