0

Creating a dynamic button in code behind as:

Dim b As Button = New Button()
b.Text = "Some Dynamic Text Here"
b.PostBackUrl = "some_page.asx"

I want to be able to render that in a specific location on an aspx page.

In aspx page, I want to do:

<td>
<%
If (specific condition) Then
    'render that specific button here <---------
Else
    'render something else
End If 
%>
</td>

1 - I know of being able to do a

Page.Form.Controls.Add(dynamic button here)

but I can not add it into a specific place on the page (not even with CSS because the button will go into a table -- the table has a column of numbers where if non zero, a button will show that crosspage postback to show drilldowned items)

4
  • In general, working with dynamic controls gets icky-sticky and Viewstate can become a large problem. I'd highly recommend hiding/showing controls with the Visible property, even if you group them into another control, such as an asp:Panel. Commented Jul 14, 2011 at 16:34
  • But how do you dynamically set the text for the static button you are creating? Commented Jul 14, 2011 at 16:35
  • using an asp:Button or asp:LinkButton, myButton.Text = "blahblah"; , just like you did above. Commented Jul 14, 2011 at 16:37
  • Right, but how do I reference the specific button being created? In aspx page, done <asp:Button runat="server" ID="<%= some_dynamic_value_here %>"> is not allowed. Commented Jul 14, 2011 at 16:42

1 Answer 1

1

You can put a placeholder where you want the button to be

<asp:PlaceHolder ID="placeHolder" runat="server"></asp:PlaceHolder>

And add the button in the code

If (specific condition) Then   
    placeHolder.Controls.Add(b) 
Sign up to request clarification or add additional context in comments.

7 Comments

I've seen the method/approach, but how, in the code behind, do I reference the placeholder? I need to be able to create placeholders with a dynamic id so in the codebehind, i can add it to the correct placeholder
So you want dynamic placeholders with dynamic buttons? I think you need to find a simpler solution
Would you be able to suggest one? For looking at a report table, for a number <> 0, to see that specific data (ie. 7/1/2011, 100 widgets; 7/2/2011, 0 widgets) 100 widgets would be clickable. I could create a URL with query string to pass the values to retrieve those 100 widgets but cross-page postback seems like the better solution (since I won't have to parse a string for values etc)
Bind to an asp:Repeater control and put the button/data inside its item template
After reviewing the repeater control, it seems the issue still occurs where I will not be able to create a button that has a dynamic ID when putting it into the item template... I think the solution I am going to do (attempt) is (just came up with it now) create an html button, set the id dynamically, add javascript so when it is clicked it changes the form location and submits the form so it posts it to the page I want it cross-page postbacked to... "Winning!"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.