Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 18th, 2004, 08:07 PM
Authorized User
 
Join Date: Dec 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to bsausser
Default CodeBehind: Multiple ImageButtons, One Click Event

I’m working with VS.NET 2000 and a Code behind file.

I’m creating a ImageButton control for each item in a collection in the code behind file, and I want each ImageButton control to raise the same event. Then I can determine the CommandName that would signify the list item selected.

I’m supposed to use OnCommand event, however Intelisence and compilation return errors.

Example:

foreach(string fileName in fileEntries)
{
    ibImageList = new ImageButton();
    ibImageList.ImageUrl = GetDirectoryName(fileName);
    ibImageList.Width = new Unit(125, UnitType.Pixel);
    ibImageList.CommandName = GetDirectoryName(fileName);
    ibImageList.OnCommand = "ibImageList_Command()";

    //Add it to the panel
    pnlImages.Controls.Add(ibImageList);
    pnlImages.Controls.Add(new LiteralControl("<br>"));
}

I've tried ibImageList.Command also, and get an error that says something like "...on the left of += or -=..."

I want to program the OnCommand during the enumeration of a collection process, but I don’t have that option? What am I missing here?

Brian


Brian Sausser, MCP
(858) 229-6129
__________________
Brian Sausser, MCP
(858) 229-6129
 
Old April 19th, 2004, 09:15 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Try something along these lines:

ibImageList.Click += new System.EventHandler(ibImageList_Command);
 
Old April 19th, 2004, 11:03 PM
Authorized User
 
Join Date: Dec 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to bsausser
Default

I tried that and got this compile error:
Method 'SausserFamily.photos._default.ibImageList_Command (object, System.Web.UI.WebControls.CommandEventArgs)' does not match delegate 'void System.EventHandler(object, System.EventArgs)'


Brian Sausser, MCP
(858) 229-6129
 
Old April 20th, 2004, 08:56 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Ok then, sounds like you need to fix the button click handler to have the right signature as the error suggests. You handler should look something like this:

Sub ibImageList_Command(sender As Object, e As System.EventArgs)
 
Old April 27th, 2004, 07:08 PM
Authorized User
 
Join Date: Dec 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to bsausser
Default

Nope, it's not compiling.

I'm supposed to use _OnCommand, but I can only use that when I'm in the asp tags in the aspx file. (refer to original problem)

I want to use behind *.cs file to create the ImageButtons. I need to have them all point to one procedure like .Click(...) and then read the command name and argument to determine what to do inside the .Click(...) procedure.

I'm not sure, I made my problem clear in the first message. Anyway, any ideas?

Brian

Brian Sausser, MCP
(858) 229-6129
 
Old May 15th, 2004, 12:03 PM
Authorized User
 
Join Date: Dec 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to bsausser
Default

Anyone?

I have the same problem in other areas, where I want to list a collection, with imagebuttons that point to one _click procedure.

I know this can be done, however I can not find an example in the documentation or on the web.

Brian

Brian Sausser, MCP
(858) 229-6129
 
Old May 15th, 2004, 10:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

Create a new event handler, named whatever, that has the parameters of what Peter supplied up above:

private void MyEH(object sender, eventargs e) {
  ..
}

..
ibImageList.Click += New System.EventHandler(MyEh)
ibImageList2.Click += New System.EventHandler(MyEh)

Hope this helps,

Brian
 
Old May 19th, 2004, 08:25 PM
Authorized User
 
Join Date: Dec 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to bsausser
Default

I'm still getting a compile error: "c:\inetpub\wwwroot\dendera\wfhm\test.aspx.cs( 30): Cannot implicitly convert type 'System.EventHandler' to 'System.Web.UI.ImageClickEventHandler'"

This is the exact code I have:
private void Page_Load(object sender, System.EventArgs e)
{
      ImageButton ibTest;
      for (int i=0; i<5; i++)
      {
    ibTest = new ImageButton();
    ibTest.ImageUrl = "../images/bluearrow.gif";
    ibTest.CommandArgument = i.ToString();
    ibTest.Click += new System.EventHandler(MyEh);

    pnlTest.Controls.Add(ibTest);
    pnlTest.Controls.Add(new LiteralControl(""));
      }
}
private void MyEh(object sender, EventArgs e)
{
    //Blah
}

I'm sorry about this; your help makes logical sense, but it's not working for me??

Brian


Brian Sausser, MCP
(858) 229-6129
 
Old June 10th, 2004, 03:05 PM
Registered User
 
Join Date: Jun 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hay Brian,

I was having the exact same issue, and here is how I solved it…

First off you can’t use System.EventHandler with an ImageButton. You need to use System.Web.UI.ImageClickEventHandler.

So, it should look like this...

ibImageList.Click += New System.Web.UI.ImageClickEventHandler(MyEh)


with the event function being ...

private void MyEh(object sender, System.Web.UI.ImageClickEventArgs e)
{
    ImageButton LocIB = (ImageButton)sender;
    // do your stuff here...
}

That should fix your compile errors.

Now for why the event is not firing.

My guess is your only creating the ImageButtons the first time the page loads. The problem with that is when you do a post back the bindings are gone. You need to make the bindings every time the page loads. For this to work well you will want to make sure you name every dynamic button you create, and make sure that instance of the button will have the same name for each page load. You set the name with ibImageList.ID = “whatever”.

Also, you can set the CommandArgument for the button and then read it in your function to figure out what button got clicked.

So, here is the logical progression of it all…

Someone goes to your page.
The page loads and runs the Page_Load event.
In that event the all your buttons get created along with the event bind.
Then someone clicks one of those buttons.
Everything gets wiped server side (other then viewstate and session stuff).
The Page_Load function is run again.
If this is a postback the server looks at the ID of the object posting back and looks to see if any events need to be fired.
If it finds an event associated with the ID of the clicked object it then runs that event.

So, if you have all your bindings in a “if (!Page.IsPostBack)” block, your events will never fire as they are not associated on the page reload.

I hope this helps.

God luck,
Jeremy


 
Old July 30th, 2004, 11:23 PM
Registered User
 
Join Date: Jul 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Everyone

I am developing one Mobile Application.
This is an devicespecific application
in which have one Mobile user control.
as follow

<mobile:panel id="BottomPanel" Runat="server">
<mobile:DeviceSpecific id="DeviceSpecific1" Runat="server">
<Choice Filter="isHTML32" >
<ContentTemplate>
<asp:ImageButton id="cmdLogoutASCX" ImageUrl="..\images\buttons\logout.gif" runat="server"></asp:ImageButton>
</ContentTemplate>
</Choice>
</mobile:DeviceSpecific>
</mobile:panel>

and this Mobile user control included in aspx file

when i am trying to create event for imagebutton in user control i am getting following error

Method does not match delegate 'void System.Web.UI.ImageClickEventHandler(object, System.Web.UI.ImageClickEventArgs)'

If you have any solution
Plz.

Thanks in advance.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Click event being cancelled!? Abbas ASP.NET 2.0 Professional 3 July 16th, 2007 05:18 PM
right click mouse event sen22 ASP.NET 2.0 Basics 5 December 16th, 2006 08:46 AM
Multiple codebehind files .NETamateur .NET Web Services 0 August 18th, 2006 02:20 PM
calling a codebehind function on onblur event vishnupriya.kamalakaram ASP.NET 1.0 and 1.1 Basics 2 July 7th, 2006 08:25 AM
Click Event dkr72 C# 3 December 8th, 2004 06:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.