Solved

Dynamic Control ID's

Posted on 2004-04-21
5
830 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
  • 3
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 250 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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