1

Why can't i call ImageButton.ImageUrl??

Default.aspx:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="Test1.jpg" CommandName="Delete" OnCommand="ImageButton_Command" />



 </asp:Content>

Default.aspx.cs:

void ImageButton_Command(object sender, CommandEventArgs e)  
      {  
       string name = ***ImageButton1.ImageUrl***; //(Error)  

}

2
  • 1
    and what does the error say? Feb 18, 2013 at 10:55
  • CS0122: Default2.ImageButton_Command (Object, System.Web.UI.WebControls.CommandEventArgs) because the security is not accessible
    – Will_G
    Feb 18, 2013 at 11:02

1 Answer 1

0

There is no reason from the code provided for this control not to be recognised; it could be a discrepancy between your .aspx and .aspx.cs files (does the former properly relate to the latter?), or it could just be a build problem, have you done a complete clean and rebuild?

A solution, regardless of why this is happening for your current situation could be this:

protected void ImageButton_Command(object sender, CommandEventArgs e) {
  var button = sender as ImageButton;
  if (button != null) {
    string name = button.ImageUrl;
  }
}

It's important to do the null check here, since technically this event handler could be called by something other than the button(s) subscribed to raise the event, even though this shouldn't be the case.

2
  • CS0122: Default2.ImageButton_Command (Object, System.Web.UI.WebControls.CommandEventArgs) because the security is not accessible
    – Will_G
    Feb 18, 2013 at 11:06
  • Try making the event handler method public or protected. Feb 18, 2013 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.