datalekz
datalekz

Reputation: 129

Asp.net Button component doesn't work under Twitter-Bootstrap theme

I've been using twitter-bootstrap for my asp.net website theme and I've ran into some trouble when trying to make a simple button execute an event. The button is an asp component, from what I've read in the forums is that bootstrap doesn't manage quite well with asp.net components, either being a Script( most obvious ) or a CSS problem ( I don't know, I'm no expert on what's going on ).

As far as I've seen the button takes action when I place it inside a form, however it's not the action that should take place as far as I know.

And redirects to the same page but with these parameters:

?ctl00%24MainContent%24LogUsernameText=&ctl00%24MainContent%24LogPasswordText=&ctl00%24MainContent%24loginBtn=Sign+In%21

I've tried many answers that I've seen in other people questions, such as making it render as a HTML button and make it runat="server" and then on page load add an instance for it.

How can I get through this?

Code from master page:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Main.master.cs" Inherits="Main" %>

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">

    <link rel="stylesheet" href="css/bootstrap.min.css">
    <style>
        body {
            padding-top: 60px;
            padding-bottom: 40px;
        }
    </style>
    <link rel="stylesheet" href="css/bootstrap-responsive.min.css">
</head>
<body>
    <!--[if lt IE 7]>
        <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
    <![endif]-->

    <form id="form1" runat="server">

    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="navbar-inner">
            <div class="container">
                <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </a>
                <a class="brand" href="Home.aspx">bitRain</a>
                <div class="nav-collapse collapse">
                    <ul class="nav">
                        <li><a href="Home.aspx">Home</a></li>
                        <li><a href="Upload.aspx">Upload</a></li>
                        <li><a href="FAQ.aspx">FAQ</a></li>
                    </ul>
                    <ul class="nav pull-right">
                        <li><a href="LoginN'Register.aspx">Sign Up</a></li>
                        <li class="divider-vertical"></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Sign In <b class="caret"></b></a>
                            <ul class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
                                <form id="navSignIn">
                                <li>Username</li>
                                <li><asp:TextBox ID="UsernameField" runat="server"></asp:TextBox></li>
                                <li>Password</li>
                                <li><asp:TextBox ID="PasswordField" runat="server"></asp:TextBox></li>
                                <li><asp:CheckBox ID="navRemember" runat="server" Text="Remember Me." /></li> 
                                <li><asp:Button ID="navLoginBtn" runat="server" Text="Sign In" CssClass="btn" style="clear: left; width: 100%; height: 32px; font-size: 13px;" /></li> 
                                </form>
                            </ul>
                        </li>
                    </ul>
                </div><!--/.nav-collapse -->
            </div>
        </div>
    </div>

    <div class="container">

        <div class="hero-unit">

            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>

        </div>

        <hr>

        <footer>
            <p>bitRain Project</p>
        </footer>

    </div>
    <!-- /container -->

    <script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
    <script src="js/bootstrap.min.js"></script>

    <script src="js/main.js"></script>
    </form>
</body>

Chunk of code from the page:

<div class="span6 form-horizontal">
            <div class="heading">
                <legend class="form-heading">Sign In</legend>
            </div>
            <div class="control-group">
                <label class="control-label" for="inputUsername">Username</label>
                <div class="controls">
                    <asp:TextBox ID="LogUsernameText" runat="server"></asp:TextBox>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inputPassword">Password</label>
                <div class="controls">
                    <asp:TextBox ID="LogPasswordText" runat="server" TextMode="Password"></asp:TextBox>
                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <label class="checkbox">
                        <asp:CheckBox ID="LogRemember" runat="server" Text="Keep me signed in |" />
                        <a href="#" class="btn btn-link">Forgot my password</a>
                    </label>
                    <asp:Button ID="loginBtn" runat="server" CssClass="btn" Text="Sign In!" Width="220px" OnClick="loginBtn_Click" />
                </div>
            </div>
        </div>

Code on .cs file:

public partial class LoginN_Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void loginBtn_Click(object sender, EventArgs e)
{
    Account log = new Account();
    string username = LogUsernameText.Text;
    string password = LogPasswordText.Text;
    string authentication = log.UserLogin(username, password);
    if (!string.IsNullOrEmpty(authentication))
    {
        // Create a new ticket used for authentication
        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
           1, // Ticket version
           username, // Username associated with ticket
           DateTime.Now, // Date issued
           DateTime.Now.AddMinutes(30), // Date to expire
           true, // "true" for a persistent user cookie
           authentication, //Role
           FormsAuthentication.FormsCookiePath); //Path authorized

        string hash = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(
        FormsAuthentication.FormsCookieName, hash);


        if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;


        Response.Cookies.Add(cookie);

        string returnUrl = Request.QueryString["ReturnUrl"];
        if (returnUrl == null) returnUrl = "~/Users/User_Profile.aspx";

        Response.Redirect(returnUrl);

        base.OnLoad(e);
    }
    else
    {
        Alert.Text = "<div class=\"alert alert-error\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button><strong>Warning!</strong> Best check yo self, you're not looking too good.<div>";
        LogUsernameText.Text = "Fail";
    }
}
}

My guess is that theres some script redirecting my button event. I can't find it. I'm using the latest version of bootstrap theme.

Upvotes: 3

Views: 6105

Answers (2)

Darshan
Darshan

Reputation: 13

If it is the look of the bootstrap button you are aiming at then the asp button component will not work. Use the

<asp:LinkButton></asp:LinkButton>

control instead. It works the same way as the asp Button does.

Upvotes: 0

datalekz
datalekz

Reputation: 129

I found the answer to my question, on proprieties, you have the option UseSubmitBehaviour and set it to false, my guess is that it will get detected like a normal HTML submit button and bootstrap is prepared to change regular HTML components behaviour. ( I don't really know how to put it in better words. Feel free to edit. )

I mean... Bootstrap wasn't planned out to work 100% with asp.net components, so it would depend on javascript commands.

It has logic, since when it was a form it would grab all text fields and when the button was activated it would submit them for you. Creating the redirect that I mentioned above, hope this helps someone, as my brain went slowpoke and delayed me 2 days of work.

<asp:Button ID="loginBtn" runat="server" CssClass="btn" Text="Sign In!" Width="220px" OnClick="loginBtn_Click" UseSubmitBehavior="False" />

EDIT and Proper Solution:

Actually I got into exploring more how it all works and it's a validation issue, it will only submit if validations are fulfilled, either you have them or not, so you must go to proprieties if you're using VS and change Validation Required value to Disabled, don't worry a button will only validate inside it's form.

For those who don't have... A proprieties bar, add these to your button html code:

ValidateRequestMode="Disabled" 

I decided to improve this answer with more reliable information, because I noticed that when searching for this problem solution I could only find my question and not a good answer on any other source, I found the "proper" solution a couple of months ago though...

Upvotes: 5

Related Questions