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 najadPosted Jul 30, 2023, 12:08 PM
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
venus najadPosted Jul 28, 2023, 1:45 PM
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 MohantyPosted Jul 28, 2023, 5:34 AM
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.
Make sure to keep the
imgMap_Clickmethod unchanged:Tahir AnsariPosted Jul 27, 2023, 11:20 PM
C#:
Make sure imgMap_Click method defined in your code-behind:
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.