Dynamically created web control , please help , ex

staffybtuk

New Member
In the following code, how do I associate onClick event handler to the aImgBt control's "onclick" event ??<BR><BR>Another restriction is I will have tens of such ImgBt dynamically created in my page depending on the data in the database. So, the ID of these ImgBt is bound with the usrName, so ecah onclick event handler would have it's on name associated with the usrName. How can I do this ??<BR><BR>Thanks,<BR><BR>Ben<BR>private sub page_load( ...) <BR>...<BR> for loop ...<BR> Dim usrName As String = "Michael" 'name is from DB.<BR><BR> Dim aImgBt As ImageButton = New ImageButton()<BR> aImgBt.AlternateText = "aPicture"<BR> aImgBt.ID = "IMGBT_" & usrName<BR> => but how do I setup the "onclick" event handler for aImgBt ??<BR><BR> ...<BR> next <BR>...<BR><BR>end subMy vb explanation sucked as I didnt really know how in vb. I have a better understanding of how to do it now that I read up on it. Basically You must declare your ImageButton as global and not as a local variable in page_load. You gotta declare it WithEvents like this...<BR><BR>Public WithEvents aImgBt as ImageButton<BR><BR>You can still create it with the new keyword in page_load but in order to use withevents it must be declared globally. Then you use the vb.net AddHandler Statement to add an EventHandler for the click event. Heres some code...<BR><BR><BR>-----------------BEGIN EVENTTEST.ASPX<BR><BR><%@ page language="vb" %><BR><BR><script runat="server"><BR><BR> Public WithEvents aImgBt As ImageButton ' declare the imagebutton withevents<BR><BR> sub page_load(sender as object,E as eventArgs)<BR> aImgBt = new ImageButton()<BR> dim usrName as string = "foobar"<BR> aImgBt.AlternateText = "aPicture"<BR> aImgBt.ID = "IMGBT_" & usrName<BR> ' Right here we tell The event to be called for Click to be the sub EventHandler which I have declared below.<BR> ' Note: Events and there types are in the sdk. You dont use onClick. Click is the event. OnClick is used when declaring the asp tag. Its not really the event.<BR><BR> AddHandler aImgBt.Click, AddressOf EventHandler<BR> foob.Controls.Add(aImgBt)<BR><BR><BR> end sub<BR> <BR> ' When button is click this event will be called and the word test is printed out<BR> sub EventHandler(Sender as Object,E as ImageClickEventArgs)<BR> Response.Write("test")<BR> Response.End()<BR> end sub<BR> <BR></script><BR><BR><form runat="server"><BR><asp:panel id="foob" runat="server"><BR><BR></asp:panel><BR></form>foobar,<BR>Thanks again for the good samples. I got it to work right after I tried it.<BR>2 pieces unsolved are :<BR>1). I would have various number of such ImageButtons. How can I declare them with WithEvents as public ?? Any workaround.<BR><BR>2). As these many ImageButtons share the saem event handler sub, how can I programatically change the image of the caller ImageButton when the event is raised ??<BR><BR>I actually tried to use this ImageButton as a 3-way checkbox (on/off/negate). If this is not the right solution for such use, what's other choices ?<BR><BR>Thanks,<BR>Ben<BR>1). I was totally wrong about needing it to be withevents and public. WithEvents apparently is used for something else. ( oops ). So make any image you want anywhere the addhandler code will work.<BR><BR><BR>2). Use the sender parameter of the event. Its the imagebutton itself. You have to convert it first though....<BR><BR>Dim btnImage as ImageButton = Ctype(Sender,ImageButton)<BR><BR>From here you could use the id to determine which imagebutton it is... and also Controls have kindof like an unused property you can set ( check sdk ).. so if you wanted to assign it a username or id and reference it later you could
 
Back
Top