473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Handle Event of button at runtime

26
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.
Apr 9 '09 #1
8 7711
kunal pawar
297 100+
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.createElement("input");
btn.setAttribute("type","Button");
btn.className ="css class name";

For server control

Button btn = new Button();
Apr 9 '09 #2
Ria12
26
hi....I want to generate server button controls in Vb.net and also want to handle the click Event of that button.any help is appricated....
Apr 9 '09 #3
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. Private WithEvents myButton As Button
  2.  
  3. Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  4.     myButton = new Button
  5.     myButton.Height = 60
  6.     myButton.Width = 100
  7.     myButton.Style.Add("color","blue")
  8. End Sub
  9.  
  10. Private Sub myButton _Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles myButton .Click
  11.  
  12. End Sub
  13.  

-Frinny
Apr 9 '09 #4
Ria12
26
I used your code...but actually i want generate button on click of other button....
Apr 10 '09 #5
Frinavale
9,735 Expert Mod 8TB
Well what do you have so far?
The example in the article explains how to show more TextBoxes depending on a DropDownList. This same concept would be applied to your button click...
Apr 10 '09 #6
Ria12
26
hi.......I 'm able to generate buttons on click of another botton in Vb.net but not able to handle button click event that is generated at runtime...
Apr 13 '09 #7
Frinavale
9,735 Expert Mod 8TB
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...
Apr 13 '09 #8
Ria12
26
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
Expand|Select|Wrap|Line Numbers
  1.    Select Case hidden
  2.  
  3.                     Case "Top"
  4.                         id= 1
  5.                         For i As Integer = 0 To c - 1
  6.  
  7.                             Dim btnTop As Button = New Button()
  8.                             btnTop.ID = id
  9.                             btnTop.Text = "Top"
  10.                             'btnTop.Style("Top") = "200px"
  11.                             btnTop.CssClass = "btnTop"
  12.                             'btnTop.Style("Left") = "5px"
  13.                             btnTop.Style("Height") = h & "px"
  14.                             btnTop.Style("Width") = w & "px"
  15.                             btnTop.Style("Border") = "solid"
  16.                             Panel1.Controls.Add(btnTop)
  17.                             AddHandler btnTop.Click, AddressOf btnTop_Click
  18.                             id= id + 1
  19.  
  20.  
  21.                         Next i
  22.       End Select
Event handler of this button is
Expand|Select|Wrap|Line Numbers
  1.  Private Sub btnTop_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  2.         l = Integer.Parse(DirectCast(sender, Button).ID)
  3.         text= CType(sender, Button).Text
  4.         height=CType(sender,Button).Height
  5.         Session("ID") = l
  6.         Session("loc") = text
  7.         Session("height1")=height
  8.         Response.Redirect("Default2.aspx")
Plz help me, I urgently need help....
Apr 17 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
4
by: KvS | last post by:
Hi all, I'm pretty new to (wx)Python so plz. don't shoot me if I've missed something obvious ;). I have a panel inside a frame, on which a Button and a StaticText is placed: self.panel =...
1
by: Lalit | last post by:
Hi, My application has two methods which creates buttons at runtime. In one method I am able to handle the events of the buttons created at run time, but in other I am not. The code structure...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
3
by: Jerry Wei | last post by:
Dear all: I have a problem about how to handle the events. For example, there are two forms, Form1 and Form2. Form2 is owned by Form1 and there is a button,Button1, in Form2. It is no problem for...
13
by: Abhishek | last post by:
Hi, how do I pass the handle of a control to the win32 api mouse_event. so that it will create the click event on that application only even if there is any other window in front of it. I dont...
2
by: =?Utf-8?B?VG9u?= | last post by:
Hello, I want to understand teh benefits of ajax technology. Does anyone has a good website where AJAX EXTENSIONS is worked out so I really understand it. There a 2 main questions: 1) How about...
10
by: menashay | last post by:
Hello, I am absolute beginner in C# so pardon me if the following question is too easy. I have a form with a one button. When I click the button I want to display a message that reads...
5
ram09
by: ram09 | last post by:
I had made a tree view of folders in a rad tree view... I have added a text box with a button in one of my nodes, using this code node.controls.add(txtbox) node.controls.add(btn) where, txtbox...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.