Can I add an image to an ASP.NET button?

后端 未结 7 546
野趣味
野趣味 2020-12-20 11:17

I want to add an image, instead of the default button.

I already have a CSS class for the image, will this work?



        
相关标签:
7条回答
  • 2020-12-20 11:21

    I actually prefer to use the html button form element and make it runat=server. The button element can hold other elements inside it. You can even add formatting inside it with span's or strong's. Here is an example:

    <button id="BtnSave" runat="server"><img src="Images/save.png" />Save</button>
    
    0 讨论(0)
  • 2020-12-20 11:25

    I dont know if I quite get what the issue is. You can add an image into the ASP button but it depends how its set up as to whether it fits in properly. putting in a background images to asp buttons regularly gives you a dodgy shaped button or a background image with a text overlay because its missing an image tag. such as the image with "SUBMIT QUERY" over the top of it.

    As an easy way of doing it I use a "blankspace.gif" file across my website. its a 1x1 pixel blank gif file and I resize it to replace an image on the website.

    as I dont use CSS to replace an image I use CSS Sprites to reduce queries. My website was originally 150kb for the homepage and had about 140-150 requests to load the home page. By creating a sprite I killed off the requests compressed the image size to a fraction of the size and it works perfect and any of the areas you need an image file to size it up properly just use the same blankspace.gif image.

    <asp:ImageButton class="signup" ID="btn_newsletter" ImageUrl="~/xx/xx/blankspace.gif" Width="87px" Height="28px" runat="server" /
    

    If you see the above the class loads the background image in the css but this leaves the button with the "submit Query" text over it as it needs an image so replacing it with a preloaded image means you got rid of the request and still have the image in the css.

    Done.

    0 讨论(0)
  • 2020-12-20 11:29

    Although you can "replace" a button with an image using the following CSS...

    .className {
       background: url(http://sstatic.net/so/img/logo.png) no-repeat 0 0;
       border: 0;
       height: 61px;
       width: 250px
    }
    

    ...the best thing to do here is use an ImageButton control because it will allow you to use alternate text (for accessibility).

    0 讨论(0)
  • 2020-12-20 11:34

    Assuming a Css class of "image" :

    input.image { 
      background: url(/i/bg.png) no-repeat top left; 
      width: /* img-width */; 
      height: /* img-height */ 
    }
    

    If you don't know what the image width and height are, you can set this dynamically with javascript.

    0 讨论(0)
  • 2020-12-20 11:37
    .my_btn{ 
      font-family:Arial; 
      font-size:10pt; 
      font-weight:normal; 
      height:30px; 
      line-height:30px; 
      width:98px; 
      border:0px;
      background-image:url('../Images/menu_image.png'); 
      cursor:pointer;
    }
    
    <asp:Button ID="clickme" runat="server" Text="Click" CssClass="my_btn" />
    
    0 讨论(0)
  • 2020-12-20 11:44

    Why not use an ImageButton control?

    0 讨论(0)
提交回复
热议问题