hellol everyone... i have created a dynamically ImageMap which by clicking on it, it opens another page. but i dont know how i define. i am using these codes, buti get an error: Delegate to an instance method cannot have null 'this'.
ImageMap ImageMap1 = new ImageMap();
ImageMap1.Height = 150;
ImageMap1.Width = 150;
ImageMap1.ImageUrl = GridView1.Rows[rowind].Cells[11].Text;
ImageMapEventHandler imgMap_Click = null; //if i do not put it to null, it asks in the next row:
ImageMap1.Click += new ImageMapEventHandler(imgMap_Click); // use of unassinged local variable
tcell_2.Controls.Add(ImageMap1);
trow_2.Controls.Add(tcell_2);
mytable.Controls.Add(trow_2);
protected void imgMap_Click(object sender, ImageMapClickEventArgs e)
{...}
appreciate and thankful your respond, kind regards

venus najadRank #2,456 · 573 reputationPosted Jul 28, 2023, 9:45 AM EDT
Amit Mohanty and Tahir Ansari, thanks for your responds... but in your cases i get this error:
No overload for imgMap_Click matches delegate ImageMapEventHandler, therefore i must define it before, but it requires assignment of mgMap_Click, which i dont know assign to what?
thanks if you respond, kind regards
Amit MohantyRank #112 · 20K reputationPosted Jul 28, 2023, 1:34 AM EDT
The error you are encountering is because you are trying to assign a null value to the imgMap_Click delegate, which is not allowed when using new ImageMapEventHandler(imgMap_Click).
To fix this issue, you need to assign the imgMap_Click delegate to the actual method you want to handle the ImageMap click event. In your case, the method you want to handle the click event is imgMap_Click.
ImageMap ImageMap1 = new ImageMap(); ImageMap1.Height = 150; ImageMap1.Width = 150; ImageMap1.ImageUrl = GridView1.Rows[rowind].Cells[11].Text; // Define the imgMap_Click method ImageMapEventHandler imgMap_Click = new ImageMapEventHandler(imgMap_Click); // Attach the imgMap_Click method to the Click event of ImageMap1 ImageMap1.Click += imgMap_Click; tcell_2.Controls.Add(ImageMap1); trow_2.Controls.Add(tcell_2); mytable.Controls.Add(trow_2);Make sure to keep the
imgMap_Clickmethod unchanged:protected void imgMap_Click(object sender, ImageMapClickEventArgs e) { // Your code to handle the ImageMap click event goes here // ... }Tahir AnsariRank #366 · 6.8K reputationPosted Jul 27, 2023, 7:20 PM EDT
C#:
ImageMap ImageMap1 = new ImageMap(); ImageMap1.Height = 150; ImageMap1.Width = 150; ImageMap1.ImageUrl = GridView1.Rows[rowind].Cells[11].Text; ImageMap1.Click += imgMap_Click; // Use the existing method directly as the event handler tcell_2.Controls.Add(ImageMap1); trow_2.Controls.Add(tcell_2); mytable.Controls.Add(trow_2);Make sure imgMap_Click method defined in your code-behind:
protected void imgMap_Click(object sender, ImageMapClickEventArgs e) { // Your event handling logic here // ... }directly using the imgMap_Click method as the event handler without creating a new delegate instance, you should be able to resolve the "Delegate to an instance method cannot have null 'this' error.
venus najadRank #2,456 · 573 reputationPosted Jul 30, 2023, 8:08 AM EDT
hello again... i have solved tjhe problem by using lambda as follow:
ImageMap1.Click += (sender, args) =>
{
_ = new EventHandler(imgMap_Click);
};
and the click method is:
protected void imgMap_Click(object sender, EventArgs e)
{
string email = HttpUtility.UrlEncode(Email);
string title = HttpUtility.UrlEncode(Title1);
string redirectUrl = "adpage.aspx?email=" + email + "&title=" + title + "&message=" + "Ad registered!";
Response.Redirect(redirectUrl);
}
the table with its items are filled in the page. but when i click on the image,it stays in the same page with the table??!! it doesnt go to the adpage.aspx???!!! something in empty or null and i dont know which... i dont know either how i should assign and assign with what... appreciate your respond, kind regards