imagebutton click handler
-
Which bit didn't you understand? ASP.NET is a stateless environment, which means that the server only deals with client requests and once the request is finished with anything that was created (like pages, controls, datasets etc.) that was to deal with that one specific request is deleted. If a client sends another request then the server will have to deal with that be creating objects to deal with the request. When you use a web application, it may appear to the user that they are the only ones using the server as they progress through the application (going through several screens for booking plane tickets for example). However to the server, all it is dealing with is a series of disconnected requests which it must process. ASP.NET does remove some of the burden from the developer by recreating that seamless feel to the application development by creating many of the necessary objects when a request arrives before your code even sees the request. This works when all you need to do is deal with static web pages that are customised by the application code. However if you start creating controls dynamically then you are now responsible for recreating them on postbacks because the server, as discussed above, will delete your dynamic controls when the request is finished with. If you do not recreate your dynamic control on postback the next request from the client will arrive with details of an event that the server does not know what to do with, so it ignores it. Now some sample code:
private void Page_Load(object sender, System.EventArgs e)
{
// Recreate the button on each postback
Button b = new Button();
b.ID = "MyDynamicButton";
b.Text = "Click me, I'm dynamic!";
b.Click+=new EventHandler(MyDynamicButton_Click);
Panel1.Controls.Add(b);
}In this case, I am adding the dynamic button into a panel. Does this help? If there is anything you are still unclear about then I'll try to respond to your questions.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
-
Which bit didn't you understand? ASP.NET is a stateless environment, which means that the server only deals with client requests and once the request is finished with anything that was created (like pages, controls, datasets etc.) that was to deal with that one specific request is deleted. If a client sends another request then the server will have to deal with that be creating objects to deal with the request. When you use a web application, it may appear to the user that they are the only ones using the server as they progress through the application (going through several screens for booking plane tickets for example). However to the server, all it is dealing with is a series of disconnected requests which it must process. ASP.NET does remove some of the burden from the developer by recreating that seamless feel to the application development by creating many of the necessary objects when a request arrives before your code even sees the request. This works when all you need to do is deal with static web pages that are customised by the application code. However if you start creating controls dynamically then you are now responsible for recreating them on postbacks because the server, as discussed above, will delete your dynamic controls when the request is finished with. If you do not recreate your dynamic control on postback the next request from the client will arrive with details of an event that the server does not know what to do with, so it ignores it. Now some sample code:
private void Page_Load(object sender, System.EventArgs e)
{
// Recreate the button on each postback
Button b = new Button();
b.ID = "MyDynamicButton";
b.Text = "Click me, I'm dynamic!";
b.Click+=new EventHandler(MyDynamicButton_Click);
Panel1.Controls.Add(b);
}In this case, I am adding the dynamic button into a panel. Does this help? If there is anything you are still unclear about then I'll try to respond to your questions.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
What you meant is to recreate the button on post back? But I already have a sub-routine which create a calendar(that has the dynamic button in it). Like what you said, the control is only called once. So does that mean i can just place the sub routine in the postback ?? This is how i do it:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim checkboxExists As Boolean = CType(Session("Checkbox"), Boolean) Dim buttonExists As Boolean = CType(Session("ButtonCreated"), Boolean) If IsPostBack Then checkboxExists = CType(Session("Checkbox"), Boolean) buttonExists = CType(Session("ButtonCreated"), Boolean) If checkboxExists Then createcheckbox() End If If buttoncreated Then createCalendar() End If End If Session("CheckBox") = checkboxExists Session("ButtonCreated") = buttoncreated
Private Sub createCalendar() imgholi = New ImageButton imgholi.ID = rID imgholi.ImageUrl = Server.MapPath("Pics\pencil.gif") AddHandler imgholi.Click, AddressOf ImageClickHandler cellleft.Controls.Add(imgholi) buttoncreated = True Session("ButtonCreated") = buttoncreated
Mine main concern now is the imagebutton. The codes work fine in the beginning but after adding in the checkbox (which is also created dynamically), the button cant works. Why? Is there problem with the codes in the pageload. That's what I think but uncertain what went wrong. -
Which bit didn't you understand? ASP.NET is a stateless environment, which means that the server only deals with client requests and once the request is finished with anything that was created (like pages, controls, datasets etc.) that was to deal with that one specific request is deleted. If a client sends another request then the server will have to deal with that be creating objects to deal with the request. When you use a web application, it may appear to the user that they are the only ones using the server as they progress through the application (going through several screens for booking plane tickets for example). However to the server, all it is dealing with is a series of disconnected requests which it must process. ASP.NET does remove some of the burden from the developer by recreating that seamless feel to the application development by creating many of the necessary objects when a request arrives before your code even sees the request. This works when all you need to do is deal with static web pages that are customised by the application code. However if you start creating controls dynamically then you are now responsible for recreating them on postbacks because the server, as discussed above, will delete your dynamic controls when the request is finished with. If you do not recreate your dynamic control on postback the next request from the client will arrive with details of an event that the server does not know what to do with, so it ignores it. Now some sample code:
private void Page_Load(object sender, System.EventArgs e)
{
// Recreate the button on each postback
Button b = new Button();
b.ID = "MyDynamicButton";
b.Text = "Click me, I'm dynamic!";
b.Click+=new EventHandler(MyDynamicButton_Click);
Panel1.Controls.Add(b);
}In this case, I am adding the dynamic button into a panel. Does this help? If there is anything you are still unclear about then I'll try to respond to your questions.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
What I have now is imagebutton and checkboxes which are created dynamically for both. 2problems: 1) Why the imagebutton click can't work? (It works fine with the same set of code before adding in the codes for checkboxes) 2) Checkboxes--> why do i have to click twice between it process with the selected checkbox?
-
Which bit didn't you understand? ASP.NET is a stateless environment, which means that the server only deals with client requests and once the request is finished with anything that was created (like pages, controls, datasets etc.) that was to deal with that one specific request is deleted. If a client sends another request then the server will have to deal with that be creating objects to deal with the request. When you use a web application, it may appear to the user that they are the only ones using the server as they progress through the application (going through several screens for booking plane tickets for example). However to the server, all it is dealing with is a series of disconnected requests which it must process. ASP.NET does remove some of the burden from the developer by recreating that seamless feel to the application development by creating many of the necessary objects when a request arrives before your code even sees the request. This works when all you need to do is deal with static web pages that are customised by the application code. However if you start creating controls dynamically then you are now responsible for recreating them on postbacks because the server, as discussed above, will delete your dynamic controls when the request is finished with. If you do not recreate your dynamic control on postback the next request from the client will arrive with details of an event that the server does not know what to do with, so it ignores it. Now some sample code:
private void Page_Load(object sender, System.EventArgs e)
{
// Recreate the button on each postback
Button b = new Button();
b.ID = "MyDynamicButton";
b.Text = "Click me, I'm dynamic!";
b.Click+=new EventHandler(MyDynamicButton_Click);
Panel1.Controls.Add(b);
}In this case, I am adding the dynamic button into a panel. Does this help? If there is anything you are still unclear about then I'll try to respond to your questions.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
Is there something related between the dynamic checkboxes and dynamic imagebutton? why is it that my imagebutton click can work if i were to use .net control-CHECKBOXLIST. Howwever, when i change to dynamic checkbox, the imagebutton click can't handle the click event.
-
What I have now is imagebutton and checkboxes which are created dynamically for both. 2problems: 1) Why the imagebutton click can't work? (It works fine with the same set of code before adding in the codes for checkboxes) 2) Checkboxes--> why do i have to click twice between it process with the selected checkbox?
terjk wrote: Why the imagebutton click can't work? You didn't post the code for the checkboxes so I can't see what difference there is. One thought is, what is
rID
? Is it the same on each postback? Are you assigning the same id to both the checkbox and the image button? (Remember IDs must be unique on the page otherwise ASP.NET becomes confused) terjk wrote: Checkboxes--> why do i have to click twice between it process with the selected checkbox? That does sound a bit odd. I vaguely remember something like that happening before, but I don't recall what caused it. I'll have a think about that and get back to you.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
-
terjk wrote: Why the imagebutton click can't work? You didn't post the code for the checkboxes so I can't see what difference there is. One thought is, what is
rID
? Is it the same on each postback? Are you assigning the same id to both the checkbox and the image button? (Remember IDs must be unique on the page otherwise ASP.NET becomes confused) terjk wrote: Checkboxes--> why do i have to click twice between it process with the selected checkbox? That does sound a bit odd. I vaguely remember something like that happening before, but I don't recall what caused it. I'll have a think about that and get back to you.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
codes for checkbox
If capacity = 0 Then txt = ownCname & " / " & ownDiv & " - " & resourcename cbx = New CheckBox cbx.Text = txt cbx.ToolTip = desc phDiv.Controls.Add(cbx) Session("Checkbox") = True
Phdiv is the placeholder where i put all my dynamic checkboxes. The codes is done in a subroutine - private sub createcheckbox() One thought is, what is rID? rId is unique-- it the id of the resource that is bind to the chkbx. -
terjk wrote: Why the imagebutton click can't work? You didn't post the code for the checkboxes so I can't see what difference there is. One thought is, what is
rID
? Is it the same on each postback? Are you assigning the same id to both the checkbox and the image button? (Remember IDs must be unique on the page otherwise ASP.NET becomes confused) terjk wrote: Checkboxes--> why do i have to click twice between it process with the selected checkbox? That does sound a bit odd. I vaguely remember something like that happening before, but I don't recall what caused it. I'll have a think about that and get back to you.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
-
terjk wrote: Why the imagebutton click can't work? You didn't post the code for the checkboxes so I can't see what difference there is. One thought is, what is
rID
? Is it the same on each postback? Are you assigning the same id to both the checkbox and the image button? (Remember IDs must be unique on the page otherwise ASP.NET becomes confused) terjk wrote: Checkboxes--> why do i have to click twice between it process with the selected checkbox? That does sound a bit odd. I vaguely remember something like that happening before, but I don't recall what caused it. I'll have a think about that and get back to you.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
BIG BIG PROBLEM! Any idea what error is this? An error has occurred because a control with auto-generated id '_ctl4' could not be located to raise a postback event. To avoid this error, explicitly set the ID property of controls that raise postback events. When the page is loaded the first time, everything is fine. However, when i click next, to show other day of the event calendar, It create TWO calendars instead of 1. Why is this so?
Dim buttoncreated As Boolean = CType(Session("ButtonCreated"), Boolean) If IsPostBack Then If Not buttoncreated Then createCalendar() End If End If Session("ButtonCreated") = buttoncreated (in page load)
Public Sub createCalendar() If Not statuscheck = 0 Then Dim img As New ImageButton img.ID = rid Session.Add("ButtonClick", imgResource) AddHandler img.Click, AddressOf ImageClickHandler img.ImageUrl = Server.MapPath("Pics\pencil.gif") cellleft.Controls.Add(img) row.Controls.Add(cellleft) buttoncreated = True Session("ButtonCreated") = buttoncreated
createcalendar() is called each time user click on BUTTON GO. -
BIG BIG PROBLEM! Any idea what error is this? An error has occurred because a control with auto-generated id '_ctl4' could not be located to raise a postback event. To avoid this error, explicitly set the ID property of controls that raise postback events. When the page is loaded the first time, everything is fine. However, when i click next, to show other day of the event calendar, It create TWO calendars instead of 1. Why is this so?
Dim buttoncreated As Boolean = CType(Session("ButtonCreated"), Boolean) If IsPostBack Then If Not buttoncreated Then createCalendar() End If End If Session("ButtonCreated") = buttoncreated (in page load)
Public Sub createCalendar() If Not statuscheck = 0 Then Dim img As New ImageButton img.ID = rid Session.Add("ButtonClick", imgResource) AddHandler img.Click, AddressOf ImageClickHandler img.ImageUrl = Server.MapPath("Pics\pencil.gif") cellleft.Controls.Add(img) row.Controls.Add(cellleft) buttoncreated = True Session("ButtonCreated") = buttoncreated
createcalendar() is called each time user click on BUTTON GO.terjk wrote: An error has occurred because a control with auto-generated id '_ctl4' could not be located to raise a postback event You need to look in the generated source of the web page to discover what _ctl4 is. In Internet Explorer View-->Source... then locate _ctl4 and work out what it attached to. Then from that point you can work out which control you have needs an ID. Remember for dynamically created controls that you want posted back to the server you should always create an ID, just like in the sample I gave before.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
-
terjk wrote: An error has occurred because a control with auto-generated id '_ctl4' could not be located to raise a postback event You need to look in the generated source of the web page to discover what _ctl4 is. In Internet Explorer View-->Source... then locate _ctl4 and work out what it attached to. Then from that point you can work out which control you have needs an ID. Remember for dynamically created controls that you want posted back to the server you should always create an ID, just like in the sample I gave before.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
Oh My God! There is FIND to search for _ctl4'. In return they say cannot find _ctl4' What should i do now? If i were to recreate the controls in page back, but why is it at the 2nd page load, the sub routine is stepped in twice. (subroutine is the creating of dynamic controls)
-
terjk wrote: An error has occurred because a control with auto-generated id '_ctl4' could not be located to raise a postback event You need to look in the generated source of the web page to discover what _ctl4 is. In Internet Explorer View-->Source... then locate _ctl4 and work out what it attached to. Then from that point you can work out which control you have needs an ID. Remember for dynamically created controls that you want posted back to the server you should always create an ID, just like in the sample I gave before.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
-
terjk wrote: An error has occurred because a control with auto-generated id '_ctl4' could not be located to raise a postback event You need to look in the generated source of the web page to discover what _ctl4 is. In Internet Explorer View-->Source... then locate _ctl4 and work out what it attached to. Then from that point you can work out which control you have needs an ID. Remember for dynamically created controls that you want posted back to the server you should always create an ID, just like in the sample I gave before.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event[^].
i Guess the main problem is the ID. But there is no way i can set it. I have look through the properties. There is a UNIQUEID. img.uniqueid=resourceid the uniqueid returns me _ct20 and others other. (similar to this) really need your help.... I have no idea if the problem lies in the computer or me. When i addhandler and a click eventhandler, with a breakpoint there and run the program. But when i click on the button, it doesnt step into the sub routine but it still can bring me to where i want. WHY? :~ :confused: