Solved

Dynamic Control ID's

Posted on 2004-04-21
5
820 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
performance issue in asp web 5 37
tutorial for ebay api 3 55
Expression Evaluater 3 38
Easy filter aspnet 2 27
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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