<%= Html.Button()%> with htmlAttributes

Last post 05-09-2008 8:54 PM by TheDeathArt. 7 replies.

Sort Posts:

  • <%= Html.Button()%> with htmlAttributes

    05-07-2008, 5:37 AM
    • Loading...
    • gyan_flip
    • Joined on 04-25-2008, 11:08 AM
    • Baghi - Bhojpur - Bihar - India
    • Posts 49

    I want to add a button using <%= Html.Button()%> and also I want to set button width, back color, etc. I am using the following code for this, but I am getting error: <%= Html.Button("btnLogin", this.GetLocalResourceObject("btnLogin").ToString(), "javascript:Login();", new Style { Width = 100 })%>

    How I can write this?

    Gyan
    Filed under:
  • Re: <%= Html.Button()%> with htmlAttributes

    05-07-2008, 6:00 AM
    • Loading...
    • johram
    • Joined on 06-13-2006, 6:36 AM
    • Sweden
    • Posts 1,865
    • Moderator

    What is Html.Button? Is it something you've implemented yourself? I don't recognize it as a standard construct...

    [Edit: I didn't notice this was in the MVC forum. I'll just keep my mouth shut ;-)]

    If this post was useful to you, please mark it as answer. Thank you!
  • Re: <%= Html.Button()%> with htmlAttributes

    05-07-2008, 6:06 AM
    • Loading...
    • gyan_flip
    • Joined on 04-25-2008, 11:08 AM
    • Baghi - Bhojpur - Bihar - India
    • Posts 49

    johram:
    What is Html.Button? Is it something you've implemented yourself? I don't recognize it as a standard construct...

    <%= Html.Button()%>: Html.Button() is an extension method to create html button. This is used in Asp.Net MVC Framework.

    Gyan
  • Re: <%= Html.Button()%> with htmlAttributes

    05-07-2008, 8:43 AM
    • Loading...
    • VinBrown
    • Joined on 07-06-2007, 1:57 PM
    • Posts 15

     All of the 'normal' attributes get passed in as part of the object HtmlAttributes parameter. So you would do something like this..

    <% object parms = new{ onclick="javascript:Login();", style="width:100px" } ; %>
    <%= Html.Button("btnLogin", "Login", parms) %>

      

    I forget the order of the Button() parameters. I'm pretty sure name is first then caption, then HtmlAttributes. You can always include the object code inline within the argument list for Button(), I just like to do it like I have it above.

  • Re: <%= Html.Button()%> with htmlAttributes

    05-08-2008, 12:45 AM
    Answer
    • Loading...
    • gyan_flip
    • Joined on 04-25-2008, 11:08 AM
    • Baghi - Bhojpur - Bihar - India
    • Posts 49

    VinBrown:
    <% object parms = new{ onclick="javascript:Login();", style="width:100px" } ; %> <%= Html.Button("btnLogin", "Login", parms) %>

    The above coge is giving error. I tried this again today, and my code works. We can do it as below:

    <%= Html.Button("btnLogin", "Login", "javascript:Login();", new { style = "width:100px" })%>

    Gyan
    Filed under:
  • Re: <%= Html.Button()%> with htmlAttributes

    05-08-2008, 7:40 AM
    • Loading...
    • VinBrown
    • Joined on 07-06-2007, 1:57 PM
    • Posts 15

     Interesting. Which version of MVC are you using? Glad to see you worked it out.

  • Re: <%= Html.Button()%> with htmlAttributes

    05-08-2008, 8:47 AM
    • Loading...
    • gyan_flip
    • Joined on 04-25-2008, 11:08 AM
    • Baghi - Bhojpur - Bihar - India
    • Posts 49

    I am using the latest version.

    Gyan
  • Re: <%= Html.Button()%> with htmlAttributes

    05-09-2008, 8:54 PM
    • Loading...
    • TheDeathArt
    • Joined on 06-28-2006, 10:26 PM
    • Denmark
    • Posts 35
    "javascript:" is only to be used in URLs, and it's a really really really poor way of using javascript, and shows one should go read a tutorial !
    It's onclick="login()" if anything.
    I'll recomend reading up about how javscript works here: http://w3schools.com/jsref/default.asp

    The correct implementation would be

    <%= Html.Button("btnLogin",this.GetLocalResourceObject("btnLogin").ToString(),"Login()",new { style="width: 100px" }) %>

    producing following HTML:

    <button  style="width: 100px"  onclick="Login()"  name="btnLogin"  id="btnLogin"  >Login</button>
Page 1 of 1 (8 items)