Delegate and EventArgs Question

Hi,

I am very new to programming and i have a quick question. I am developing a webpage and I want a delegate to handle a button click. I am programming in J#. What I want to do is display the element found at ArrayList(i) after clicking on a button.

Example:

<input type="button" id="display" value="Display" runat="server" onserverclick="write(<%=i %>)" />

I want the method 'write' to take the value 'i'.

The method will be:

ArrayList theList;

public void write(int i)
{
aDiv.set_InnerHtml("Value in array at index " + i + "is" + theList.get_Item(i));
}

I get the error: The argument in the creation of delegate 'System.EventHandler' must name a method.

I am so confused. This sounds like an easy question, but I'm not sure how to write it.
Please help, thank you.

Allan


Answer this question

Delegate and EventArgs Question

  • Thiruvenkadam

    Thank you very much for the response Thilak. I inserted the snippet in my code but it did not display anything. I studied your code and I am starting to realize what is happening. I am just confused on how the selected "i" will be printed out. Also, where you inserted "i++", is that correct I will continue to study your code. Again, I really appreciate you responding Thilak.

  • Chuck D

    Hi Alan,

    I have given the code snippet for example only. In your first question you used i in your script. can you clarify me what is i in your code.i++ need not be in that place it can be any where.but make sure that the change i++ occured before binding the event.

    regards,

    Raj Thilak


  • IvoGrootjes

    Hi,

    For passing arguments to eventArgs we have to create a wrapper you can follow the following code snippet for passing argument.

    class MyEvent

    {

    private MyDelegate YouDoIt;

    private int param;

    public MyEvent(MyDelegate d, int p)

    {

    YouDoIt = d;

    param = p;

    }

    public void DoIt(Object sender, System.EventArgs e)

    {

    write(sender, new MyEventArgs(param));

    }

    }

    public class MyEventArgs extends EventArgs

    {

    public MyEventArgs(int i)

    {

    myVar = i;

    }

    private int myVar;

    /** @property */

    public int get_MyProperty()

    {

    return myVar;

    }

    /** @property */

    public void set_MyProperty(int value)

    {

    myVar = value;

    }

    }

    private static int i;

    delegate void MyDelegate(Object sender, MyEventArgs e);

    MyEvent mevent;

    ArrayList theList;

    public void write(Object sender, MyEventArgs eventArgs)

    {

    aDiv.set_InnerHtml("Value in array at index " + eventArgs.get_MyProperty() + " is " + theList.get_Item(eventArgs.get_MyProperty()));

    }

    protected void Page_Load(Object sender, EventArgs e)

    {

    theList = new ArrayList();

    theList.Add("ra:");

    theList.Add("raB");

    theList.Add("THR");

    i++;

    mevent = new MyEvent(new MyDelegate(this.write), i);

    display.add_ServerClick(new System.EventHandler(mevent.DoIt));

    }

    remove onserverclick="write(<%=i %>)" in

    <input type="button" id="display" value="Display" runat="server" onserverclick="write(<%=i %>)" />

    hope this will work for you.

    regards,

    Thilak


  • Dick Grier

    I have also tried writing the method:

    public void write(Object sender, EventArgs e) {}

    I am not sure how to get the arguments in e.

  • Delegate and EventArgs Question