Solved

Dynamic Control ID's

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now