Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Buttons in a loop

Status
Not open for further replies.

dave755

IS-IT--Management
May 3, 2001
69
US
This feels like it is too obvoius to ask, but I have been stymied in trying to find an answer by reading the docs...

I want to create a series of buttons in a loop, with each button passing a parameter to the handler to distinguish it from the others.

Code:
<%for i = 0 to ButtonCount%>
  <asp:button id=<%=i%> onclick="handler" runat=server ...
<%next%>

When I attempt to do this, the ASPX compiler complains that it is not legal to use <%%> in server tags.

Dave Gee
 
You can try something like this, but it is suggested you do it in the Page_Init Event of the page:
Code:
        Dim i As Integer

        For i = 1 To 10
            Dim btn As New Button
            btn.ID = "btn" + CStr(i)
            btn.Text = "Button" + CStr(i)
            Me.FindControl("Form1").Controls.Add(btn)
        Next

I just harded coded a value of 10, but you can use what ever you want of course. Also there are lots of other button properties you can specify.
Also, before you add the controls to the page you need to find the form (the default is "Form1") first. This is because the buttons need to be add to the "Form" of the page, other wise you will receive and error at run-time


Hope this helps...

Jim

Jim
 
Thanks for the tip, Jim. It isn't quite what I need, though. The application I am working on is a browser-based app and the code is running on the server. The System.Web.UI.Button control is a client side control.

I tried this:
Code:
<asp:Button ID="Temp" 
            Text="Edit" 
            OnClick="EditCmd" 
            runat=server 
            />
<%Me.FindControl("Form1").FindControl("Temp").ID = "Edit" & ndx.ToString()%>
(where 'ndx' is my iterator integer).

The compiler complained:
Object reference not set to an instance of an object.

Dave Gee
 
The example I gave, creates serverside button controls on an ASP.NET page. I have used dynamic controls in a couple of apps. Run the code. You will see that if you click a button, it causes a post back, which means it is a server control. If you view the source it is going to show a type of input type="submit" because an ASP.NET page only renders HTML, however the controls run on the server

Jim
 
The application I am working on is a browser-based app and the code is running on the server.
All ASP.NET applications are browser based apps with code running on the server so what you are doing is no different to any other ASP.NET application.

The System.Web.UI.Button control is a client side control.
No it isn't. The System.Web.UI.Button control is an ASP.NET server control which omits HTML to the browser (like all server controls do).

<%Me.FindControl("Form1").FindControl("Temp").ID = "Edit" & ndx.ToString()%>
Rather than including code inline like you would have done in classic ASP, try adding your code into your code-behind file and adding the buttons like Jim suggested (you can use the AddHandler method to wire the buttons up to a specific function).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
It's not necessary to get testy. Remember - you traversed this learning curve too...

For the benefit of those who come after me, here's what I was missing:

To programmatically add controls to an ASP.NET page, write code in the page startup routines, not in the HTML.

1) In HTML, place an <asp:table id="Table1" ...>. Don't include any rows or cells.
2) In your page startup code, create and configure "TableRow" and "TableCell" objects as needed for your business logic.
3) Use the id for the HTML table to add each row to the table.

Dave Gee
 
It's not necessary to get testy. Remember - you traversed this learning curve too...
I think you have taken my comments out of context - I wasn't being "testy", I was simply pointing out where you where going wrong and trying to help you understand that you had misunderstood what a server control was.

To programmatically add controls to an ASP.NET page, write code in the page startup routines, not in the HTML.
That is correct and is what Jim pointed out in his first post when he said "it is suggested you do it in the Page_Init Event of the page".

1) In HTML, place an <asp:table id="Table1" ...>. Don't include any rows or cells.
2) In your page startup code, create and configure "TableRow" and "TableCell" objects as needed for your business logic.
3) Use the id for the HTML table to add each row to the table.
Again you are correct in how this would be done if you wanted to create new rows for a table but this is the first time you have actually mentioned that you wanted to add new rows to a table; your original post said you wanted to create new buttons which is why Jim posted his example and why I mentioned the AddHandler event.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top