Input submit onclick and onserverclick not working - c#

I'm working on a simple login screen with the below code:
<form id="login">
<div class="formHeader">
<h1>Login</h1>
</div>
<div class="formDiv">
<input type="text" placeholder="Email" name="txtEmail"/>
<div class="inputImage fa fa-user"></div>
</div>
<div class="formDiv">
<input type="password" placeholder="Password" name="txtPassword"/>
<div class="inputImage fa fa-lock"></div>
</div>
<div class="formDiv">
<label id="remember">
<input type="checkbox" name="cbxRemberMe" value="Checked"/>
<div class="checkbox"></div><span>Remember me</span>
</label>
</div>
<div class="formDiv">
<input type="submit" value="LOGIN" onclick="btnLogin_Click" />
</div>
<div class="formFooter"><a class="forgot" href="#">Forgot Password</a></div>
and back-end code
protected void btnLogin_Click(object sender, EventArgs e)
{
}
But the button does not call the backend code at all.
Nothing happens except the URL changes from
"http://localhost:15726/Pages/Login.aspx" to
"http://localhost:15726/Pages/Login.aspx?txtEmail=&txtPassword=&ctl00=LOGIN"
I cannot put a Runat=Server attribute in the form as I have 2 forms on this page (login and register)
any help is appreciated.
What I have tried:
Playing around with Runat=Server, onclick() and OnServerClick() (swapping around and changing them up)
Making input type=submit / input type=button
none of these work.

Have you tried these two approaches?
<asp:Button runat="server" id="btnLogin" Text="LOGIN" OnClick="btnLogin_Click">
or
< button onserverclick="btnLogin_Click" runat="server" id="btnLogin">LOGIN</button>
Edit
This was supposed to be a comment and not answer. Sorry for that.
However, the entire form doesn't seem to be "webform" form compatible. If you want to be able to read the values from the form elements the form should have a runat="server" tag, too.

Firstly, you need to specify whether it is a server control or client control. Specify runat=server to tell that it should be available for the Code-Behind file.
Finally, Use any javascript function with OnClick and any code behind function with OnServerClick
Something like this,
aspx
Plain HTML
<input type="submit" runat="server" value="LOGIN" onclick="return js_btnLogin_Click()" OnServerClick="btnLogin_Click"/>
or with ASPX Control
<asp:Button runat="server" Text="LOGIN" OnClientClick="return js_btnLogin_Click()" OnClick="btnLogin_Click">
javascript
<script type="text/javascript">
function js_btnLogin_Click()
{
//return false, in case if you want to stop posting the form to the server
return true;
}
</script>
Code-Behind
protected void btnLogin_Click(object sender, EventArgs e)
{
}

Related

How to get text from a template text field in asp.net?

I have used a login-template for the page in .net application. How can I get the texts from the input text fields of the template and store it in a string variable?
<form id="form1" runat="server" role="form" method="post" class="login-form">
<div class="form-group" runat="server">
<label class="sr-only" for="form-username">Username</label>
<input type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form_username" runat="server"/>
</div>
<div class="form-group" runat="server">
<label class="sr-only" for="form-password">Password</label>
<input type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form_password" runat="server"/>
</div>
<button type="submit" class="btn" onclick="btn_login_Click" runat="server">LOGIN</button>
</form>
To get the Text in a TextBox you can simply do this:
private void AnyMethod()
{
string textValue = this.MyTextBox.Text;
}
You can access the raw post data with:
string exampleInput = Request.Form["<inputElementName>"];
If your input element is having the attribute 'runat="server"' you should be able to access it with
string exampleInput = <inputElementName>.Value;
If this does not work, you would need to show us a little bit more of your code/page.
Sample form:
<form id="login" runat="server">
<asp:TextBox ID="tbInput1" runat="server"
Width="75px"
TabIndex="1">
</asp:TextBox>
<asp:Button ID="btSubmit" runat="server" Text="Submit" />
</form>
Access with
tbInput1.Text
I guess ,if runat=server is there then it should directly get value using :
form_username.value.Tostring().
After doing little research on above issue got to know that ,
we can't access the login control properties directly .
if we want to access name then we have to use : User.Identity.Name
try doing this ! it might help you .
For further reference please visit below link :
https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.redirecttologinpage.aspx

ASP - OnServerClick Doesn't work properly

I'm using ASP.NET with C# 4.0.
My problem is that I want only one button to activate a certain method on server.
What do i mean? I'm glad you asked.
I have 2 buttons.
Any my aspx code is:
<form id="form2" runat="server">
<div class="box-generic">
<div class="form-group well" style="">
<input type="submit" name="button_filter" value="filter" class="btn btn-primary" />
<input type="button" id="all_events_button" name="all_events_button" value="All Events CSV" class="btn btn-primary" OnServerClick="downloadAllEvents" runat="server"/>
</div>
</div>
</form>
If I press the "filter" button it works awesome.
Whenever I press the "All Events CSV" button, it works great.
Scenario
1. Clicking on All Events. (Activates the all events button normally)
2. Clicking on Filter.
downloadAllEvents() Method is being activated. BAD BAD BAD
And the weirdest part about it is that this method will be activated through filter only after it has been activated through AllEvents.
EDIT:
I'm currently checking inside the method that the call didn't came from the filter button.
not a pretty sight.
Haven't found someone with the same problem.
Thanks in advance :)
I did a below sample page, and its working perfectly... can you POST the full page ... ?
Try this code and tell me what exactly is happening:
<%# Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Am Load<br/>");
}
protected void downloadAllEvents(object sender, EventArgs e)
{
Response.Write("Am Download");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<div class="box-generic">
<div class="form-group well" style="">
<input type="text" name="x" />
<input type="submit" name="button_filter" value="filter" class="btn btn-primary" />
<input type="button" id="all_events_button" name="all_events_button" value="All Events CSV" class="btn btn-primary" onserverclick="downloadAllEvents" runat="server" />
</div>
</div>
</form>
</body>
</html>
Not the answer I was hoping for, BUT it works and it works good. (Bugless)
In my downloadAllEvents() I check who sent the post request and I deny the post request from anyone who is not all_events_button
By doing so i transfer the responsibility from the sender to the listener.
If anyone has better answer please provide it.

Custom postback inside aspx and redirect

I need to perform a postback in order to process a gateway payment by redirect, so i've got an example in order to do inside an html with server side code (runat=server in the inputs):
<form action="https://xxxxxxxx" method="post">
<input runat="server" type="text" id="param1" name="param1" value="asd" />
<input runat="server" size="100" type="text" id="parm2" name="parm2" value="asd" />
<input runat="server" type="text" size="50" id="param3" name="param3" value="asd" />
<input runat="server" type="submit" value="Post!" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
// New instance of library and his magic
LibraryXXX r = new LibraryXXX();
r.Parameter.Add("xxxx", xxxx);
// more logic goes here
}
Clinking the submit button sends the postback and the page loads an html. I want traslate that sample to an aspx webform. The problem is i can't have more than one form label inside an aspx. So, how can i do the same postback inside an aspx?

HTML Button Calling ASP Class?

Was hoping someone would be able to help me. I'm basically attempting to call a method within an class using a HTML button. The reasoning behind this is because I'm intending to make it work similar to a login (and wanting it to hold sessions upon logging in), as well as keeping the current, visual effects of the button.
Upon research, there is some places basically saying you can do it, however it simply won't work for me for some reason. Where it says you can do it-
HTML Button like a ASP.NET Button
Here is my code-
<form runat="server">
<div class="input">
<asp:TextBox ID="txtUser" runat="server" type="email" placeholder="Username"></asp:TextBox>
<span><i class="fa fa-envelope-o"></i></span>
</div>
<div class="input">
<asp:TextBox ID="TextBox1" runat="server" type="password" placeholder="Password"></asp:TextBox>
<span><i class="fa fa-lock"></i></span>
</div>
</form>
<button id="button1" runat="server" onserverclick="doIt" class="submit"><i class="fa fa-long-arrow-right"></i></button>
With the C# code containing
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void doIt(object sender, EventArgs e)
{
Response.Redirect("index.html");
}
}
Am I going about this the totally wrong way? (The redirect was just there as a test for now, until I'm able to get it to run the method since it doesn't run it.)
Any help is appreciated.
Below is all my HTML code:
<form class="login">
<fieldset>
<legend class="legend">Login</legend>
<form runat="server">
<div class="input">
<asp:TextBox ID="txtUser" runat="server" type="email" placeholder="Username"></asp:TextBox>
<span><i class="fa fa-envelope-o"></i></span>
</div>
<div class="input">
<asp:TextBox ID="TextBox1" runat="server" type="password" placeholder="Password"></asp:TextBox>
<span><i class="fa fa-lock"></i></span>
</div>
<button id="button1" runat="server" onserverclick="doIt" class="submit"><i class="fa fa-long-arrow-right"></i></button>
</form>
</fieldset>
<div class="feedback">
login successful <br />
redirecting...
</div>
</form>
You have a nested form in your HTML, which is invalid markup. Remove the inner form, and add runat="server" to the form remaining. Then place your button inside the form like so:
<form class="login" runat="server">
<fieldset>
<legend class="legend">Login</legend>
<div class="input">
<asp:TextBox ID="txtUser" runat="server" type="email" placeholder="Username"></asp:TextBox>
<span><i class="fa fa-envelope-o"></i></span>
</div>
<div class="input">
<asp:TextBox ID="TextBox1" runat="server" type="password" placeholder="Password"></asp:TextBox>
<span><i class="fa fa-lock"></i></span>
</div>
<%-- place the button here --%>
<button id="button1" runat="server" onserverclick="doIt" class="submit"><i class="fa fa-long-arrow-right"></i></button>
</fieldset>
<div class="feedback">
login successful
<br />
redirecting...
</div>
</form>
Each web form - .aspx - should contain one form, and must contain runat="server" for the .cs file to manage. All server-side elements - elements with the runat="server", such as your <button> must be contained within it for the code behind to be able to point to it. If you don't, you should receive a Invalid postback or callback argument exception yellow screen of death.

Value of input returning blank

I am trying to retrieve the value of an input in my code behind, but it is returning "". I am using the remodal plugin to display a popup, and then I have a textbox within this modal popup
Here is the ASP.NET code I am using.
Add Customer
<div class="remodal" data-remodal-id="addCustomer">
<button data-remodal-action="close" class="remodal-close"></button>
<h1>Add Customer</h1>
<h4>Customer</h4>
<div class="form-group has-feedback">
<label class="sr-only" for="name2">Customer</label>
<input type="text" class="form-control" id="TXT_CreateCustomer" placeholder="Customer" name="name2" runat="server"/>
<i class="fa fa-building-o form-control-feedback"></i>
</div>
<br />
<button id="BTN_NewJob_AddCustomer" data-remodal-action="confirm" class="remodal-confirm" runat="server" onserverclick="BTN_NewJob_AddCustomer_ServerClick">Create Customer</button>
</div>
This is the C# server code that I am using to try and retrieve that value from the textbox.
protected void BTN_NewJob_AddCustomer_ServerClick(object sender, EventArgs e)
{
string example = TXT_CreateCustomer.Value;
}
The server code is very simple and should work, but it only returns "" when text is typed into the textbox.
Any ideas on what would prevent getting the text from the textbox?
Thanks.
You are not using the asp: on your form elements, therefore they are not filling in. asp:input instead of input, also runat='server' is required.
Edit: Sorry, let me clarify. Not asp:input, asp:textbox.
So, for example:
<asp:Textbox id="myTextBox" runat="server" />
I don't know exactly, will it help or no, but try to use following in the code behind:
string example = TXT_CreateCustomer.innerText;

Categories