imagebutton onclick
-
I have imagebuttons which are created dynamically. Upon click, do a response.redirect and carry the value of the clicked imagebutton to another form. But how can i check which button is being clicked? I have assigned a unique id to each button.
Private sub createcalendar() ElseIf tobook Then imgholi = New ImageButton imgholi.ID = rID imgId = imgholi.ID Session.Add("ResourceLevel", imgId) imgholi.ImageUrl = Server.MapPath("Pics\pencil.gif") AddHandler imgholi.Click, AddressOf ImageClickHandler cellleft.Controls.Add(imgholi) buttoncreated = True Session("ButtonCreated") = buttoncreated
(There are many creating of new imagebutton)Public Sub ImageClickHandler(ByVal sender As Object, ByVal e As ImageClickEventArgs) Response.Redirect("MyBookingNewBooking.aspx?ResourceID=" & id & "BookingDate=" & dateformat) End Sub
There is no properties as .clicked..What should i do? -
I have imagebuttons which are created dynamically. Upon click, do a response.redirect and carry the value of the clicked imagebutton to another form. But how can i check which button is being clicked? I have assigned a unique id to each button.
Private sub createcalendar() ElseIf tobook Then imgholi = New ImageButton imgholi.ID = rID imgId = imgholi.ID Session.Add("ResourceLevel", imgId) imgholi.ImageUrl = Server.MapPath("Pics\pencil.gif") AddHandler imgholi.Click, AddressOf ImageClickHandler cellleft.Controls.Add(imgholi) buttoncreated = True Session("ButtonCreated") = buttoncreated
(There are many creating of new imagebutton)Public Sub ImageClickHandler(ByVal sender As Object, ByVal e As ImageClickEventArgs) Response.Redirect("MyBookingNewBooking.aspx?ResourceID=" & id & "BookingDate=" & dateformat) End Sub
There is no properties as .clicked..What should i do?Have you considered that instead of creating an ImageButton to create just a regular image and wrap it in and anchor? The final output would look something like this:
<a href="MyBookingNewBooking.aspx?ResourceID=someId&BookingDate=someDate">
<img src="Pics/Pencil.gif" width=100 height=100>
</a>Using a Literal control would allow you to build up the relevant string and it would then appear in your web page and there would be no need for the additional processing on the server to redirect the page when the image button is clicked as the client would go directly to the correct page. (This is good because it will save you server side processing)
"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 have imagebuttons which are created dynamically. Upon click, do a response.redirect and carry the value of the clicked imagebutton to another form. But how can i check which button is being clicked? I have assigned a unique id to each button.
Private sub createcalendar() ElseIf tobook Then imgholi = New ImageButton imgholi.ID = rID imgId = imgholi.ID Session.Add("ResourceLevel", imgId) imgholi.ImageUrl = Server.MapPath("Pics\pencil.gif") AddHandler imgholi.Click, AddressOf ImageClickHandler cellleft.Controls.Add(imgholi) buttoncreated = True Session("ButtonCreated") = buttoncreated
(There are many creating of new imagebutton)Public Sub ImageClickHandler(ByVal sender As Object, ByVal e As ImageClickEventArgs) Response.Redirect("MyBookingNewBooking.aspx?ResourceID=" & id & "BookingDate=" & dateformat) End Sub
There is no properties as .clicked..What should i do?sender
will be the button that was clicked. You can then check it's ID property and use that. regards, Paul Watson Bluegrass South Africa Christopher Duncan quoted: "...that would require my explaining Einstein's Fear of Relatives" Crikey! ain't life grand? Einstein says... -
I have imagebuttons which are created dynamically. Upon click, do a response.redirect and carry the value of the clicked imagebutton to another form. But how can i check which button is being clicked? I have assigned a unique id to each button.
Private sub createcalendar() ElseIf tobook Then imgholi = New ImageButton imgholi.ID = rID imgId = imgholi.ID Session.Add("ResourceLevel", imgId) imgholi.ImageUrl = Server.MapPath("Pics\pencil.gif") AddHandler imgholi.Click, AddressOf ImageClickHandler cellleft.Controls.Add(imgholi) buttoncreated = True Session("ButtonCreated") = buttoncreated
(There are many creating of new imagebutton)Public Sub ImageClickHandler(ByVal sender As Object, ByVal e As ImageClickEventArgs) Response.Redirect("MyBookingNewBooking.aspx?ResourceID=" & id & "BookingDate=" & dateformat) End Sub
There is no properties as .clicked..What should i do?You can also ImageButton's CommandName and CommandArgument properties. In the Click event handler, you can retrieve the values as shown below. private void ibDatePick_Click(object sender, System.Web.UI.ImageClickEventArgs e) { string parmCnt = ( (ImageButton) sender ).CommandArgument; } HTH :-) Harry
-
You can also ImageButton's CommandName and CommandArgument properties. In the Click event handler, you can retrieve the values as shown below. private void ibDatePick_Click(object sender, System.Web.UI.ImageClickEventArgs e) { string parmCnt = ( (ImageButton) sender ).CommandArgument; } HTH :-) Harry
private void ibDatePick_Click(object sender, System.Web.UI.ImageClickEventArgs e) { string parmCnt = ( (ImageButton) sender ).CommandArgument; } (ImageButton) is the name of mine button? But it doesn't have the properties .commandarguement. Only .gettype Does this is to asp.net syntax too? Or i have to inherits or imports something? Must I have any additional codes to the imagebutton (upon creating the button)? So I can apply the commandname or commandarguement(which is i can't in asp.net properties)? I have tried e.gettype, e. etc... but jus couldn't find the properties you mentioned.
-
sender
will be the button that was clicked. You can then check it's ID property and use that. regards, Paul Watson Bluegrass South Africa Christopher Duncan quoted: "...that would require my explaining Einstein's Fear of Relatives" Crikey! ain't life grand? Einstein says... -
Have you considered that instead of creating an ImageButton to create just a regular image and wrap it in and anchor? The final output would look something like this:
<a href="MyBookingNewBooking.aspx?ResourceID=someId&BookingDate=someDate">
<img src="Pics/Pencil.gif" width=100 height=100>
</a>Using a Literal control would allow you to build up the relevant string and it would then appear in your web page and there would be no need for the additional processing on the server to redirect the page when the image button is clicked as the client would go directly to the correct page. (This is good because it will save you server side processing)
"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[^].
-
But there isn't any suitable properties which i can use. sender.gettype??? How to check it's ID property?? At the button.clicked method??
You have to cast the
sender
to the appropriate type. In your case the imagebutton. This is because the sender is an object, which is the base class of all objects in .NET.
"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[^].
-
No, I didn't say that. However given the problems you are describing here and in the previous thread you started on this subject I really don't have enough information to help, nor do you seem to have the understanding of the framework to take the answers your getting and apply it to your situation. You are dealing with dynamic control creation and subsequent handling of events generated by these controls - this is a messy affair even for an experienced ASP.NET developer, and one which must be thought through in order to get it to work. The very idea of creating dynamic controls means that you are going beyond what the framework can intrinsically provide for you and you are hand-crafting your own solution.
"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[^].
-
No, I didn't say that. However given the problems you are describing here and in the previous thread you started on this subject I really don't have enough information to help, nor do you seem to have the understanding of the framework to take the answers your getting and apply it to your situation. You are dealing with dynamic control creation and subsequent handling of events generated by these controls - this is a messy affair even for an experienced ASP.NET developer, and one which must be thought through in order to get it to work. The very idea of creating dynamic controls means that you are going beyond what the framework can intrinsically provide for you and you are hand-crafting your own solution.
"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[^].
Okie... never mind about that. Like what you have told me, for dynamic controls is got to instantiate them in the page_load, right? Well, i have 3 event calendars-- daily, weekly & monthly. For daily and weekly everything works fine because i created the calendar using html table whereby i have a subroutine and i jus apply the method to the pageload when post back. However, for the monthly calendar, im using calendar control provided. Therefore, i got to use the method provided called DayRender. I can't possibly pass the dayrendar() into pageload, right? As you know the purpose of dayrender method. i can't just apply it to pageload as a sub routine. This is the error message i get Arguement not specified for a parameter 'e' of Private sub calevent_dayrender(sender as object, e as system.web.ui.webcontrols.dayrendereventargs)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Session("ButtonCreated") Is Nothing Then buttoncreated = CType(Session("ButtonCreated"), Boolean) Else buttoncreated = False End If If IsPostBack And buttoncreated Then ' calEvent_DayRender() End If
Private Sub calEvent_DayRender(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles calEvent.DayRender Dim img As New ImageButton AddHandler img.Click, AddressOf ImageClickHandler If e.Day.IsOtherMonth Then e.Cell.Controls.Clear() lblnametxt.Visible = False img.Visible = False lblline.Visible = False Else img.ImageUrl = Server.MapPath("Pics\pencil.gif") lblnametxt.Visible = True img.Visible = True lblline.Visible = True End If If isClose then If e.Day.Date > Date.Today Or e.Day.Date = Date.Today Then e.Cell.Controls.Add(img) buttoncreated = True Session("ButtonCreated") = buttoncreated End If
Hope you understand....This is where i created mine buttons -
private void ibDatePick_Click(object sender, System.Web.UI.ImageClickEventArgs e) { string parmCnt = ( (ImageButton) sender ).CommandArgument; } (ImageButton) is the name of mine button? But it doesn't have the properties .commandarguement. Only .gettype Does this is to asp.net syntax too? Or i have to inherits or imports something? Must I have any additional codes to the imagebutton (upon creating the button)? So I can apply the commandname or commandarguement(which is i can't in asp.net properties)? I have tried e.gettype, e. etc... but jus couldn't find the properties you mentioned.
Hi, "But it doesn't have the properties .commandarguement. Only .gettype" The reason for this behaviour is probably you have declared your ImageButton variable as
Dim imgBut
instead ofDim imgBut as ImageButton
If you declare it this way, and look for properties, you will see the commandName property, in addition to other properties. Now you can assign any string to CommandName property so as to uniquely identify yur button.imgBut.CommandName = "but1"
Then in the event handler, Place code like below.Private Sub ImageButton1_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click Dim butID As String Dim butCmdName As String butID = CType(sender, ImageButton).ID butCmdName = CType(sender, ImageButton).CommandName End Sub
All it does is - Uses the parameter "sender", which is actually the button that you clicked on - Since it is defined as Object, you will have to cast it to a ImageButton. Voila ! For the purpose you have asked for, you can just use ID property to identify the ImageButton uniquely. I hope this helps you Harry :-) -
Hi, "But it doesn't have the properties .commandarguement. Only .gettype" The reason for this behaviour is probably you have declared your ImageButton variable as
Dim imgBut
instead ofDim imgBut as ImageButton
If you declare it this way, and look for properties, you will see the commandName property, in addition to other properties. Now you can assign any string to CommandName property so as to uniquely identify yur button.imgBut.CommandName = "but1"
Then in the event handler, Place code like below.Private Sub ImageButton1_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click Dim butID As String Dim butCmdName As String butID = CType(sender, ImageButton).ID butCmdName = CType(sender, ImageButton).CommandName End Sub
All it does is - Uses the parameter "sender", which is actually the button that you clicked on - Since it is defined as Object, you will have to cast it to a ImageButton. Voila ! For the purpose you have asked for, you can just use ID property to identify the ImageButton uniquely. I hope this helps you Harry :-)OKAY! I understand... thanks However, I still have another problem. I'm using dynamic imagebuttons. Therefore there is a need to intantiate the imagebutton on page_load on post back, right? I have a monthly calendar and made use of the method day_render. This is where i created my imagebutton (for the 1st round) . The problem then come... how can i intantiate the button again on postback? Private sub page_load(..) cal_dayrender() end sub ?? i cant possibly pass the subroutine in like this right?
-
OKAY! I understand... thanks However, I still have another problem. I'm using dynamic imagebuttons. Therefore there is a need to intantiate the imagebutton on page_load on post back, right? I have a monthly calendar and made use of the method day_render. This is where i created my imagebutton (for the 1st round) . The problem then come... how can i intantiate the button again on postback? Private sub page_load(..) cal_dayrender() end sub ?? i cant possibly pass the subroutine in like this right?
Hi, You are right ! If you want to see all the dynamically created controls after each postback, you need to recreate those controls for each postback. The best way to do this would be to create a new method build_calendar, which would create all dynamic imagebutton controls. Then you can invoke this method from day_render, page_load or any other method you would like ! Inside page_load, you would want to call build_calendar, only for postbacks and not the first time. So use Page.IsPostBack property. Hope it helps. Harry :-)
-
Hi, You are right ! If you want to see all the dynamically created controls after each postback, you need to recreate those controls for each postback. The best way to do this would be to create a new method build_calendar, which would create all dynamic imagebutton controls. Then you can invoke this method from day_render, page_load or any other method you would like ! Inside page_load, you would want to call build_calendar, only for postbacks and not the first time. So use Page.IsPostBack property. Hope it helps. Harry :-)
Private Sub calEvent_DayRender(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles calEvent.DayRender e.Day.IsSelectable = False If e.Day.IsOtherMonth Then e.Cell.Controls.Clear() e.Cell.Dispose() End If For i = 0 To cbxlistResource.Items.Count - 1 If cbxlistResource.Items(i).Selected = True Then Dim img As New ImageButton 'img = New ImageButton Dim lblline As New Label AddHandler img.Click, AddressOf ImageClickHandler If e.Day.IsOtherMonth Then e.Cell.Controls.Clear() lblnametxt.Visible = False img.Visible = False lblline.Visible = False Else img.ImageUrl = Server.MapPath("Pics\pencil.gif") lblnametxt.Visible = True img.Visible = True lblline.Visible = True 'If Not e.Day.Date < Date.Today Then e.Cell.Controls.Add(lblnametxt) 'End If End If
This is the code i did in the dayrender method. I make used of the 'e' parameter that is pass in. If i were to create a new method, what about the e parameter that was passed in?