Laziale
Laziale

Reputation: 8225

HTML button type and commandname in asp.net

I am trying to assign commandname to HTML button but obviously it doesn't work very well. This is what I am trying to achieve:

  <button type="submit" class="btn btn-info right" CommandName="Login" runat="server" id="loginBtn"><span class="icon16 icomoon-icon-enter white"></span> Login</button>
                                
  <asp:Button ID="btnLogin" CommandName="Login" runat="server" CssClass="btn btn-info right" Text="Login" />

The styling for some reason doesn't work on the asp button but it does for the HTML button. Now I want to somehow assign the commandname property to the HTML button if its possible. Is that doable?

Upvotes: 0

Views: 7156

Answers (3)

Morm Sopheak
Morm Sopheak

Reputation: 19

Better if you put HiddenField in htmlbutton and then Find child

<button runat="server"
    ClientIDMode="Static"
    class="btn btn-success"
    ID="button_recalculate"
    OnServerClick="button_recalculate_OnClick"
    >
    <asp:HiddenField runat="server" ID="hidden" Value=<%#Eval("ID") %>/>
    Voir
</button>

Server

try
{
    var type = (HtmlButton) sender;
    var child = type.FindControl("hidden");
    var id = ((HiddenField) child).Value.TransformToInt();
    SendValueToServer(id);
}
catch (Exception)
{
    //
}

Upvotes: 0

mschocobo
mschocobo

Reputation: 62

I used google to find an answer to this question; however since I've found more complete answers I am also answering this question even though it's older (in case any other googlers find it).

In asp.net you can use the DataControlCommands class to implement the following functionality:

  • Cancel
  • Delete
  • Edit
  • First
  • Insert
  • Last
  • New
  • Next
  • Page
  • Prev
  • Select
  • Sort
  • Update

Which means :

<asp:LinkButton runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />

among other things.

It's in the library entries for System.Web.UI.WebControls (msdn.microsoft.com)

Yes you could just put in the html and have it work fine- however if you're building something like a dynamic entity site (etc), then it's worth it to take the time to learn how asp.net does it inherently (imo).

Upvotes: 0

Win
Win

Reputation: 62260

CommandName is only available for Button Server Control; not in html button.

However, you can use LinkButton and style it the way you want it.

enter image description here

enter image description here

<asp:LinkButton ID="btnLogin" class="button" runat="server"
CommandName="Login"><span>Login</span></asp:LinkButton>

.button
{   
    background: transparent url('/Images/ButtonLeft.gif') no-repeat top left;  
    display: block;
    float: left;
    line-height: 11px; /* 21px (Button Background) = 5px (padding-top) + 11px (font-size) + 5px(padding-bottom) */
    height: 21px; /* Button Background Height */
    padding-left: 9px;  
    text-decoration: none;
    font-weight: bold;
    color: white;
    font-size: 11px;    
}

a:link.button, a:visited.button, a:active.button
{
    color: white;
    text-decoration: none;
    margin-right: 10px;
}

a.button:hover
{ 
    background-position: bottom left;
}

a.button span, a.button span 
{
    background: transparent url('/Images/ButtonRight.gif') no-repeat top right;    
    display: block;
    padding: 5px 9px 5px 0; /*Set 9px below to match value of 'padding-left' value above*/
}

a.button:hover span
{ 
    background-position: bottom right;
    color: white;
}

Upvotes: 1

Related Questions