Runtime Creation Of Button+Adding Events+Sourabh
-
Hi All, I have created a button at runtime on form load:
Dim btnSave As New Button btnSave.BackColor = Drawing.Color.Maroon btnSave.ForeColor = Drawing.Color.White btnSave.Text = "Save" ButtonHolder.Controls.Add(btnSave)
I Have also added a code to it as :Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnhandler.Click Response.Write("clicked") End Sub
but this second event code does not execute when i click on the button. Reason???Thanks and Regards,
-
Hi All, I have created a button at runtime on form load:
Dim btnSave As New Button btnSave.BackColor = Drawing.Color.Maroon btnSave.ForeColor = Drawing.Color.White btnSave.Text = "Save" ButtonHolder.Controls.Add(btnSave)
I Have also added a code to it as :Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnhandler.Click Response.Write("clicked") End Sub
but this second event code does not execute when i click on the button. Reason???Thanks and Regards,
-
You haven't wired up the eventhandler to the Click event of your dynamically-created control. Use the AddHandler[^] statement to do this.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
How do we do that? Like this?
AddHandler btnSave.Click, btnhandler_click(ByVal sender As Object, ByVal e As EventArgs)
this code give me error as "expression expected" by underlining the ByVal And where do we do that? I have also added this Protected WithEvents btnsave As System.Web.UI.WebControls.Button and modified the below code: Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click Response.Write("clicked") End SubThanks and Regards,
-
How do we do that? Like this?
AddHandler btnSave.Click, btnhandler_click(ByVal sender As Object, ByVal e As EventArgs)
this code give me error as "expression expected" by underlining the ByVal And where do we do that? I have also added this Protected WithEvents btnsave As System.Web.UI.WebControls.Button and modified the below code: Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click Response.Write("clicked") End SubThanks and Regards,
Dot Net Jantu wrote:
How do we do that? Like this? AddHandler btnSave.Click, btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) this code give me error as "expression expected" by underlining the ByVal And where do we do that?
Did you read the article? The sample code shows you how to dynamically assign an event handler to an event. You're code doesn't look anything like that in the article so it's not surprising that it doesn't work. I suggest that you implement your code in line with the article instead of just making up something that isn't even syntactically correct.
Dot Net Jantu wrote:
Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click Response.Write("clicked") End Sub
The Handles keyword is unnecessary if the event handler is only being used for controls that are created dynamically. However, if you have a control called
btnSave
defined in your ASPX page then you would want to use it.Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
Dot Net Jantu wrote:
How do we do that? Like this? AddHandler btnSave.Click, btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) this code give me error as "expression expected" by underlining the ByVal And where do we do that?
Did you read the article? The sample code shows you how to dynamically assign an event handler to an event. You're code doesn't look anything like that in the article so it's not surprising that it doesn't work. I suggest that you implement your code in line with the article instead of just making up something that isn't even syntactically correct.
Dot Net Jantu wrote:
Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click Response.Write("clicked") End Sub
The Handles keyword is unnecessary if the event handler is only being used for controls that are created dynamically. However, if you have a control called
btnSave
defined in your ASPX page then you would want to use it.Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
Thanks, its working now.. I have one more problem, in the page load i have written this
Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)
but in the event: Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) ' when i type "txtn" and try for intelisence, it dosent show the control. End Sub RegardsThanks and Regards,
-
Thanks, its working now.. I have one more problem, in the page load i have written this
Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)
but in the event: Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) ' when i type "txtn" and try for intelisence, it dosent show the control. End Sub RegardsThanks and Regards,
The variable
txtname
has been defined locally in another procedure. It doesn't have scope in your event handler procedure so it won't appear in intellisense. To get a reference to the TextBox control that has raised the event, you need to callFindControl
on the control that contains the TextBoxes. You should define some naming convention for each TextBox you dynamically create so that you have a means of obtaining a reference to each one by name.Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
The variable
txtname
has been defined locally in another procedure. It doesn't have scope in your event handler procedure so it won't appear in intellisense. To get a reference to the TextBox control that has raised the event, you need to callFindControl
on the control that contains the TextBoxes. You should define some naming convention for each TextBox you dynamically create so that you have a means of obtaining a reference to each one by name.Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
I am doing it like this: but its wrong. I didt understand what you wanted to say, please correct me. dssave.Tables("dtUsers").Rows(0)("UserName") = nameHolder.FindControl(txtname.Text.Trim) it gives an error. Object reference not set to an instance of an object.
Thanks and Regards,
-
I am doing it like this: but its wrong. I didt understand what you wanted to say, please correct me. dssave.Tables("dtUsers").Rows(0)("UserName") = nameHolder.FindControl(txtname.Text.Trim) it gives an error. Object reference not set to an instance of an object.
Thanks and Regards,
Dot Net Jantu wrote:
ssave.Tables("dtUsers").Rows(0)("UserName") = nameHolder.FindControl(txtname.Text.Trim) it gives an error. Object reference not set to an instance of an object.
Yes, it will. To know why, you need to read the MSDN documentation to understand how the
FindControl
method works. Have you done this?Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
Dot Net Jantu wrote:
ssave.Tables("dtUsers").Rows(0)("UserName") = nameHolder.FindControl(txtname.Text.Trim) it gives an error. Object reference not set to an instance of an object.
Yes, it will. To know why, you need to read the MSDN documentation to understand how the
FindControl
method works. Have you done this?Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
as per the msdn documentation, I have done this.. Dim name As TextBox = nameHolder.FindControl("txtname") also I had done this If (Not name Is Nothing) Then ' Get name's parent. Dim myControl2 As Control = name.Parent Response.Write("Parent of the text box is : " & myControl2.ID) Else Response.Write("Control not found.....") End If and its showing as control not found..
Thanks and Regards,
-
as per the msdn documentation, I have done this.. Dim name As TextBox = nameHolder.FindControl("txtname") also I had done this If (Not name Is Nothing) Then ' Get name's parent. Dim myControl2 As Control = name.Parent Response.Write("Parent of the text box is : " & myControl2.ID) Else Response.Write("Control not found.....") End If and its showing as control not found..
Thanks and Regards,
-
Dot Net Jantu wrote:
and its showing as control not found..
Have you actually added a control to the Controls collection of nameHolder with the ID 'txtname'?
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
if this is what you mean then yes. Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)
Thanks and Regards,
-
if this is what you mean then yes. Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)
Thanks and Regards,
Dot Net Jantu wrote:
Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)
The description of the
Control.FindControl
which you said you had read, is as follows: Searches the current naming container for a server control with the specified id parameter. You haven't set theID
property for your control. The fact that you have declared a variable calledtxtname
and set it with a reference to your dynamically createdTextBox
object is not relevent.Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
Hi All, I have created a button at runtime on form load:
Dim btnSave As New Button btnSave.BackColor = Drawing.Color.Maroon btnSave.ForeColor = Drawing.Color.White btnSave.Text = "Save" ButtonHolder.Controls.Add(btnSave)
I Have also added a code to it as :Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnhandler.Click Response.Write("clicked") End Sub
but this second event code does not execute when i click on the button. Reason???Thanks and Regards,
Apart from what you've already been told, the button needs to be created in the LoadViewState function so that it exists early enough in the page lifecycle for the viewstate to be restored and the event to fire.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Dot Net Jantu wrote:
Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)
The description of the
Control.FindControl
which you said you had read, is as follows: Searches the current naming container for a server control with the specified id parameter. You haven't set theID
property for your control. The fact that you have declared a variable calledtxtname
and set it with a reference to your dynamically createdTextBox
object is not relevent.Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
hey thanks it worked. I would like to know that do i need to assign a id for the button also? And one more thing will i have to use that find control method in each and every procedure to retrieve the vbalue of the control?
Thanks and Regards,
-
hey thanks it worked. I would like to know that do i need to assign a id for the button also? And one more thing will i have to use that find control method in each and every procedure to retrieve the vbalue of the control?
Thanks and Regards,
Dot Net Jantu wrote:
I would like to know that do i need to assign a id for the button also?
You probably should do. You will need to if you want to locate instances of the Button control calling
FindControl
on the container object.Dot Net Jantu wrote:
And one more thing will i have to use that find control method in each and every procedure to retrieve the value of the control?
Yes, if you don't already have a reference to one of the controls you will need to call the
FindControl
method on the container object, passing in its ID to find it.Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush