Hello.....I want to generate a button at runtime..and set the style like Top,Left,Right, Height,Width of a button also have to handle click event and when button is clicked it should redirect to another page.Plz help me.
Handle Event of button at runtime
Collapse
X
-
which type of button you want to generate at runtime, "server" side or HTML control.
If you want to generate html button then using
var btn = document.create Element("input" );
btn.setAttribut e("type","Butto n");
btn.className ="css class name";
For server control
Button btn = new Button(); -
Please take a look at the article about how to use dynamic controls in ASP.NET.
You need to declare your Button at the page level (so that it is accessible to all of the page's methods). You need to declare this Button "WithEvents " in VB.NET
You need to instantiate the Button in the Page Init event (otherwise you will not be able to handle the click event).
For example:
Code:Private WithEvents myButton As Button Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init myButton = new Button myButton.Height = 60 myButton.Width = 100 myButton.Style.Add("color","blue") End Sub Private Sub myButton _Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles myButton .Click End Sub
-FrinnyComment
-
You have most likely instantiated the dynamic button during the OnClick event of the non-dynamic button. In this case the OnClick event for they dynamic button will not fire.
The reason is pretty simple, ASP.NET uses the ViewState to determine what event(s) caused the page to post back to the server (and to determine what state your Objects are in...like selected indexes of DropDownList etc). If your Object does not exist (is not instantiated) at the time the ViewState is loaded, then your event will not be registered and all the state information for that Object will be lost.
Therefore you need to instantiate your button during the Page Init event.
So, you need to be able to indicate when to create the button during the Page Init event. In order to do this you need to store a Boolean value somewhere that is available during the Page Init event. Because the Page Init event occurs before the ViewState is loaded you are going to have to use something that is available through the Request Object...
You could use a hidden field, or a cookie, or the URL...Comment
-
I understand your point with that i solve my problem of Click Event of runtime generated button not fired ....now i want to retrieve the height of rutime generated button pass to another page but it create an error
can not implicitly convert type unit to integer
my code is
Event handler of this button isCode:Select Case hidden Case "Top" id= 1 For i As Integer = 0 To c - 1 Dim btnTop As Button = New Button() btnTop.ID = id btnTop.Text = "Top" 'btnTop.Style("Top") = "200px" btnTop.CssClass = "btnTop" 'btnTop.Style("Left") = "5px" btnTop.Style("Height") = h & "px" btnTop.Style("Width") = w & "px" btnTop.Style("Border") = "solid" Panel1.Controls.Add(btnTop) AddHandler btnTop.Click, AddressOf btnTop_Click id= id + 1 Next i End Select
Plz help me, I urgently need help....Code:Private Sub btnTop_Click(ByVal sender As Object, ByVal e As System.EventArgs) l = Integer.Parse(DirectCast(sender, Button).ID) text= CType(sender, Button).Text height=CType(sender,Button).Height Session("ID") = l Session("loc") = text Session("height1")=height Response.Redirect("Default2.aspx")Last edited by Frinavale; Apr 17 '09, 01:23 PM. Reason: Added code tags. Please post code snippets in [code] [/code] tags.Comment
Comment