Image with runat=server cannot see code-behind property
I've got the following in my .aspx:
<input type="image" src="<%=PayPalButtonImage %>" onserverclick="RedirectToPayPal" runat="server" />
In the code-behind I've got this property:
protected string PayPalButtonImage
{
get { return PayPalExpressCheckoutButtonUrl;}
}
protected void Re开发者_运维问答directToPayPal()
{
}
why can't it see this property or the server method RedirectToPayPal? I get a runtime error of :
'ASP.cart_aspx' does not contain a definition for 'RedirectToPayPal' and no extension method 'RedirectToPayPal' accepting a first argument of type 'ASP.cart_aspx' could be found
This may not solve your problem, but I don't think your method signature is correct. Try changing your RedirectToPayPal method to
protected void RedirectToPayPal(object sender, ImageClickEventArgs e)
{
}
protected void RedirectToPayPal(object sender, EventArgs e)
{
}
Pretty sure you need those missing arguments for asp.net page related events.
I don't think this is your real problem. Make sure you have built the solution and that the ASPX points to the code behind file you edited.
It's not the codebehind that's important, it's the Inherits property. Let's see your class definition and your @Page directive. If these don't match, the method cannot be found.
I think the reason you are getting the runtime error is becuase you haven't defined an event for this. Try the following:
CS:
public event EventHandler RedirectEvent;
protected void Page_Load(object sender, EventArgs e)
{
this.RedirectEvent += new EventHandler(RedirectToPayPal);
}
protected string PayPalButtonImage
{
get { return "hello"; }
}
protected void RedirectToPayPal(object sender, EventArgs e)
{
//DO Whatever here.
}
ASPX:
<input id="Image1" type="image" src="<%=PayPalButtonImage %>" runat="server" onserverclick="RedirectToPayPal" />
You're being oddly stubborn about posting code, for someone who is asking for help...
Here's a thought. Use the ASP controls for what they are designed for and quit trying to game the system:
<asp:ImageButton ID="imgButton" runat="server" OnClick="RedirectToPaypal" />
Your original control has no ID property, which is required to fire events on the backend.
精彩评论