friendly URLS Login page postback - asp.net

Problem
I'm working on the loginscreen of my website. I used my own template but added the "RequiredFieldValidator" for my password and username. When I click the ASP:Button without filling in the textboxes. The RequiredFieldValidator errormessages appears for 1 second but then the page refreshes.
Platforms
Firefox
Chrome
Opera
My thoughts
I think this has to do with the friendly urls nuget. I didn't touch this but when I click the login button and look at the networkevents (in Chrome - F12) I see that it's /Account/Login?.......... so without the '.aspx'
HTML-Code
<form role="form">
<div class="form-group">
<div class="input-group login-input">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<asp:TextBox runat="server" id="LoginUserName" type="text" class="form-control" placeholder="Username"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="LoginUserName"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The user name field is required." />
</div>
<br>
<div class="input-group login-input">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<asp:TextBox runat="server" id="LoginPassword" type="password" class="form-control" placeholder="Password"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="LoginPassword"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The password field is required." />
</div>
<div class="checkbox">
<label>
<input runat="server" id="InputRememberMe" type="checkbox">
Remember me
</label>
</div>
<div class="input-group login-input">
<asp:Button runat="server" onclick="LogIn" class="btn btn-primary pull-right" Text="Login" />
<asp:PlaceHolder runat="server" ID="ErrorMessage" Visible="false">
<p class="text-danger">
<asp:Literal runat="server" ID="FailureText" />
</p>
</asp:PlaceHolder>
</div>
<hr>
Create Account
Password Recovery
<div class="clearfix"></div>
</div>
C# Code
protected void Page_Load(object sender, EventArgs e)
{
//RegisterHyperLink.NavigateUrl = "Register";
//OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];
//var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
//if (!String.IsNullOrEmpty(returnUrl))
//{
// RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
//}
}
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
var manager = new UserManager();
ApplicationUser user = manager.Find(LoginUserName.Text, LoginPassword.Text);
if (user != null)
{
IdentityHelper.SignIn(manager, user, InputRememberMe.Checked);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
FailureText.Text = "Invalid username or password.";
ErrorMessage.Visible = true;
}
}
}

I solved my problem. I had a form tag in my masterpage but also in my login page which gives a warning. This blocked everything...

Related

Coudn't pass the form values one page to another asp.net

i have to values values one form to another using asp.net i can pass to another only regno and name only another values cannot pass i got the error missing directive what i tried so far i attached below
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label>Reg No</label>
<asp:TextBox ID="regno" runat="server" class="form-control" required></asp:TextBox>
</div>
<div class="form-group">
<label>Name</label>
<asp:TextBox ID="name" runat="server" class="form-control" required></asp:TextBox>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" id="phone" name="phone" class="form-control" required />
</div>
<div class="form-group">
<label>Gender</label>
<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged" Text="Male" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="FeMale" />
</div>
<div class="form-group">
<label>Interest</label>
<asp:CheckBox ID="CheckBox1" runat="server" Text="Cricket" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="Football" />
<asp:CheckBox ID="CheckBox3" runat="server" Text="Cinima" />
</div>
<div class="form-group">
<asp:Button ID="Button1" runat="server" Text="Submit" class="btn btn-success" OnClick="Button1_Click" />
</div>
</div>
Button Event
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("form2.aspx?regno=" +
this.regno.Text + "&name=" +
this.name.Text + "&phone=" +
this.phone.Text ); this line i got error missing directive
}
Second form
Label1.Text = Request.QueryString["regno"];
Label2.Text = Request.QueryString["name"];
Label3.Text = Request.QueryString["phone"];
phone seems to be ordinary input and not TextBox (note the input tag and lowercase id). Also, you are requesting a txtphone while id=phone.
Be aware, string data should be Url encoded before sending it through querystring.
Edit: replace
<input type="text" id="phone" name="phone" class="form-control" required />
with
<asp:TextBox ID="phone" runat="server" class="form-control" required></asp:TextBox>
Edit: for checkboxes, use something like
var interests = new List<string>();
if (chkCheckBox1.Checked)
interests.Add(chkCheckBox1.Text);
if (chkCheckBox2.Checked)
interests.Add(chkCheckBox2.Text);
if (chkCheckBox3.Checked)
interests.Add(chkCheckBox3.Text);
var interestsString = string.Join(",", interests);
var sex = RadioButton1.Checked ? RadioButton1.Text : RadioButton1.Text;
Response.Redirect("form2.aspx" +
"?regno=" + this.regno.Text +
"&name=" + this.name.Text +
"&phone=" + this.phone.Text +
"&sex=" + HttpUtility.UrlEncode(sex) +
"&interest=" + HttpUtility.UrlEncode(interestsString) +
// if needed, decode with HttpUtility.UrlDecode(Request.QueryString["interest"]:
// var interests = HttpUtility.UrlEncode(interestsString);

bootstrap datepicker in asp.net

I am new to coding and trying out bootstrap datepicker in my small project.
I have used bootstrap datepicker in my code. It works fine for selecting a date and populating it on the box but I do not understand how to access this date in codebehind.
here is my code:
<div class="form-group">
<asp:Label ID="lblDOB" runat="server" CssClass="col-xs-12" class="form-control" Text="Date Of Birth" Font-Bold="true"></asp:Label>
<div class="row">
<!-- Include Bootstrap Datepicker -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.min.css" />
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker3.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.min.js"></script>
<div class="form-group">
<div class="col-xs-12 date">
<div class="input-group input-append date" id="datePicker">
<input type="text" class="form-control" name="date" />
<span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#datePicker')
.datepicker({
format: 'mm/dd/yyyy'
})
.on('changeDate', function(e) {
// Revalidate the date field
$('#eventForm').formValidation('revalidateField', 'date');
});
$('#eventForm').formValidation({
framework: 'bootstrap',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
name: {
validators: {
notEmpty: {
message: 'The name is required'
}
}
},
date: {
validators: {
notEmpty: {
message: 'The date is required'
},
date: {
format: 'MM/DD/YYYY',
message: 'The date is not a valid'
}
}
}
}
});
});
</script>
</div>
</div>
<div class="form-group">
<asp:Label ID="lblEmail" runat="server" CssClass="col-xs-12" AssociatedControlID="txtEmail" class="form-control" Text="Email"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server" CssClass="form-control" placeholder="Enter Email"></asp:TextBox>
</div>
<div class="form-group">
<asp:Label ID="lblPhoneNumber" runat="server" CssClass="col-xs-12" AssociatedControlID="txtPhoneNumber" class="form-control" Text="Phone Number"></asp:Label>
<asp:TextBox ID="txtPhoneNumber" runat="server" CssClass="form-control" placeholder="Enter Primary Phone Number"></asp:TextBox>
</div>
<div class="form-group">
<asp:Label ID="lblSecondaryNumber" runat="server" CssClass="col-xs-12" AssociatedControlID="txtSecondaryNumber" class="form-control" Text="Secondary Phone Number"></asp:Label>
<asp:TextBox ID="txtSecondaryNumber" runat="server" CssClass="form-control" placeholder="Enter Secondary Phone Number"></asp:TextBox>
</div>
<div>
<asp:Button ID="btnSubmit" runat="server" CssClass="btn btn-primary btn-lg" Text="submit" />
</div>
CodeBehind:
Protected Sub btnSubmit_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
InitiateAdd()
End Sub
Private Sub InitiateAdd()
Try
Dim conn As New Connection(Connection.EConnectionString.Library)
Using conn.Connection
Actions.AddInformationUser(datepicker.text,txtEmail.Text, txtPhoneNumber.Text, txtSecondaryNumber.Text, conn.Connection)
End Using
Catch ex As Exception
Library.Helper.ShowMessage(ex.Message)
End Try
End Sub
If I want to use the Email in my codebehind then I would use txtEmail.text, but I do not understand how to get the value in datepicker in my codebehind.I tied it with datePicker.text as datePicker was the id given to the div but it did not work.
The datepicker appears to be getting it's values stored in:
<input type="text" class="form-control" name="date" />
Try adding a runat="server" attribute in there and and Id, use that Id in your code to pick it up.

Why is linkbutton OnClick not firing?

This is driving me crackers, it's very basic code, just a very basic three textbox contact form and a linkbutton.
<div class="form-group has-feedback">
<asp:Label ID="lblYourName" AssociatedControlID="txtYourName" CssClass="col-sm-3 control-label" runat="server" Text="Your name"></asp:Label>
<div class="col-sm-6">
<asp:TextBox ID="txtYourName" TextMode="SingleLine" CssClass="form-control" runat="server" placeholder="Your name"></asp:TextBox>
<span class="glyphicon glyphicon-user form-control-feedback"></span>
<asp:Label ID="lblNoName" runat="server" Visible="false" Text="Please enter your name"></asp:Label>
</div>
</div>
<div class="form-group has-feedback">
<asp:Label ID="lblEmail" runat="server" AssociatedControlID="txtEmail" Text="Email" CssClass="col-sm-3 control-label"></asp:Label>
<div class="col-sm-6">
<asp:TextBox ID="txtEmail" CssClass="form-control" runat="server" placeholder="Email" TextMode="Email"></asp:TextBox>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
<asp:Label ID="lblNoEmail" runat="server" Visible="false" Text="Please enter your email"></asp:Label>
</div>
</div>
<div class="form-group">
<asp:Label ID="lblMessage" AssociatedControlID="txtMessage" CssClass="col-sm-3 control-label" runat="server" Text="Your message"></asp:Label>
<div class="col-sm-6">
<asp:TextBox ID="txtMessage" CssClass="form-control" TextMode="MultiLine" placeholder="Your message" runat="server"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="col-sm-offset-3 col-sm-6">
<asp:LinkButton ID="lnkSubmit" runat="server" OnClick="lnkSubmit_Click" CssClass="btn standard-hover-effect bg-red btn-lg btn-block">
<span class="text">Contact us <i class="fa fa-arrow-right"></i></span>
</asp:LinkButton>
</div>
</div>
The OnClick of the linkbutton points to this simple MailMessage method where it checks name and email boxes are filled in and then sends an email.
protected void lnkSubmit_Click(object sender, EventArgs e)
{
lblNoName.Visible = false;
lblNoEmail.Visible = false;
if (string.IsNullOrEmpty(txtYourName.Text) || string.IsNullOrEmpty(txtEmail.Text))
{
if (!string.IsNullOrEmpty(txtYourName.Text))
{
lblNoName.Visible = false;
}
else
{
lblNoName.Visible = true;
}
if (!string.IsNullOrEmpty(txtEmail.Text))
{
lblNoEmail.Visible = false;
}
else
{
lblNoEmail.Visible = true;
}
}
else
{
MailMessage mm = new MailMessage(txtEmail.Text, "foo#bar.com");
mm.Subject = "Feedback from website";
mm.Body = "Email from " + txtYourName.Text + "<br /><br />" + txtMessage.Text;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.websitelive.net";
smtp.Send(mm);
panContactThanks.Visible = true;
}
}
But when I click the submit button, nothing happens. At all. The OnClick doesn't even fire so the breakpoint in the code behind doesn't even get called. It's as if the OnClick even isn't even in the code and it's just a dummy button. What have I missed out?
Please try CauseValidation="false" in link button.
Like:
<asp:LinkButton ID="lnkSubmit" runat="server" OnClick="lnkSubmit_Click"
CauseValidation="false" CssClass="btn standard-hover-effect bg-red btn-lg btn-block">
Wrap your html inside form tag
<form id="someForm" runat="server">
<!--your html-->
</form>
You are using .net server controls, so you need to wrap them inside form. Don't forget to add runat="server"
http://forums.asp.net/t/1463877.aspx?Does+an+aspx+must+have+a+form+tag+
Probably you forgot to add the isPostBack condition in your Page_load
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Control initialization here
}
}
Take a look at ASP.NET page life cycle: https://msdn.microsoft.com/en-us/library/ms178472.aspx
IsPostBack: https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.110).aspx

Submit form that posts to another site

I have an online banking login that I'm trying to take from an old client site and put on their new site. If I copy the form and paste it into my new site, it just reloads the page when submitted. I've tried to change it to an asp.net form using the appropriate and other applicable tags with the same result.
If I take the form and put it in a simple index.html file, it submits and takes me to the correct external site.
I was unable to set the ID & values with the asp.net form and that may be one of the issues.
Any help is appreciated.
Here is the HTML form version that works in .html page that needs to work in .aspx page:
<form action="some external site" method="POST" autocomplete="OFF" target="_top">
<input type="hidden" name="sssid" value="my value">
<input type="hidden" name="iid" value="another value">
<div class="field-container">
<label for="aid" id="aid-label">Access ID</label>
<input name="aid" id="aid" type="text" size="8" onFocus="toggle_label(this, 'focus');" onBlur="toggle_label(this,'blur');" />
</div>
<div class="field-container">
<label for="passcode" id="passcode-label">Passcode</label>
<input name="passcode" id="passcode" type="password" size="8" onFocus="toggle_label(this, 'focus');" onBlur="toggle_label(this,'blur');"/>
</div>
<div class="form-row">
<input type="submit" name="Submit" value="Go" class="button">
</div>
</form>
Here is my asp.net form version that I tried:
<form action="some external site" method="POST" autocomplete="OFF" target="_top">
<input type="hidden" name="sssid" value="some value">
<input type="hidden" name="iid" value="another value">
<asp:TextBox runat="server" ID="sssid" CssClass="form-control" type="hidden" ClientIDMode="Static"></asp:TextBox>
<asp:TextBox runat="server" ID="iid" CssClass="form-control" type="hidden" ClientIDMode="Static"></asp:TextBox>
<div class="form-group">
<asp:Label runat="server" id="lblAccessID"></asp:Label>
<asp:TextBox runat="server" ID="txtAccessID" CssClass="form-control"> </asp:TextBox>
</div>
<div class="form-group">
<asp:Label runat="server" id="lblPassCode"></asp:Label>
<asp:TextBox runat="server" ID="txtPasscode" CssClass="form-control" TextMode="Password"></asp:TextBox>
</div>
<button type="submit" class="btn btn-blue">GO
<img src="/Content/img/icon-sm-arrow.png" alt="" />
</button>
</form>
My asp.net form code behind:
protected void Page_Load(object sender, EventArgs e)
{
txtAccessID.Attributes.Add("placeholder", "Access ID");
txtPasscode.Attributes.Add("placeholder", "Passcode");
}
I had to use the following to post this correctly:
<asp:LinkButton runat="server" ID="btnSubmit" CssClass="btn btn-blue" PostBackUrl="urlhere" Text='GO <img src="/Content/img/icon-sm-arrow.png" alt="" />' />
set PostBackUrl property of Button control => https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl(v=vs.110).aspx
the reason webforms always posting it 2 itself may be due to their __doPostBack script.

find hidden field value when link button is inside nested repeater

<asp:Repeater ID="rptHotels" runat="server" OnItemDataBound="rptHotels_ItemDataBound">
<ItemTemplate>
<div class="hotel-box">
<div class="hotel-img">
<asp:HiddenField ID="hdnHotelCode" runat="server" Value='<%#Eval("HotelCode")%>' />
<a class="preview" href='<%#Eval("ImageURL_Text") %>' title='<%#Eval("HotelName")%>' target="_blank">
<img src='<%#Eval("ImageURL_Text") %>' alt='<%#Eval("HotelName")%>' height="75px"
width="100px" />
</a>
</div>
<div class="hotel_heeading_content">
<div class="hotel_heading">
<h2>
<asp:LinkButton ID="lnkHotelDetail" runat="server" OnClick="lnkHotelDetail_Click">
<%#Eval("HotelName")%>
(
<%#Eval("boardType")%>)
</asp:LinkButton>
</h2>
</div>
<div class="stars">
<span class="stars">
<%#Eval("StarRating")%></span>
</div>
<div class="hotel_text">
<%#Eval("HotelAddress")%>,
<%#Eval("Destination")%>
,<%#Eval("Country")%>
<img src="images/ico_point2.png" alt="" id="mapicon" class="mapicon" />
<input type="hidden" id="hdnLatitude" class="hdnLatitude" runat="server" value='<%#Eval("Latitude")%>' />
<input type="hidden" id="hdnLongitude" class="hdnLongitude" runat="server" value='<%#Eval("Longitude")%>' />
<input type="hidden" id="hdnInfoWindow" class="hdnInfoWindow" runat="server" />
</div>
</div>
<p>
<asp:Literal ID="ltDes" runat="server"></asp:Literal>
</p>
<p>
more info
</p>
<div class="btn">
<asp:LinkButton ID="lnkPrice" runat="server" Text=' <%#Eval("totalPrice")%>' OnClick="lnkHotelDetail_Click" ></asp:LinkButton>
</div>
<div class="roominfo">
<asp:Repeater ID="rptRooms" runat="server">
<HeaderTemplate>
<div class="rooms">
<div class="roominfoheader">
<div class="roomheaderlbl">
Room Name</div>
<div class="roomheaderlbl">
Total Room Rate</div>
<div class="roomheaderlbl">
Book Now</div>
</div>
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="rooms">
<div class="roominforow">
<div class="roominforowlbl">
<asp:Label ID="lblRoomName" runat="server" Text='<%#Eval("roomCategory") %>'></asp:Label></div>
<div class="roominforowlbl">
$
<asp:Label ID="Label1" runat="server" Text='<%#Eval("totalRoomRate") %>'></asp:Label></div>
<div class="roominforowlbl">
<asp:LinkButton ID="lnkBookNow" runat="server" Text="Book Now" OnClick="lnkBookNow_Click"></asp:LinkButton></div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
I have above HTML for nested repeater .I am able to find the hidden field value which contain hotel Code by following method
protected void lnkHotelDetail_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
var item = (RepeaterItem)btn.NamingContainer;
HiddenField hdnHotelCode = (HiddenField)item.FindControl("hdnHotelCode");
}
but the problem is now i have to find the hidden field value when a nested repeater item template link button is clicked .You can check that lnkBookNow is link button which is inside the rptRooms repeater.
protected void lnkBookNow_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
var item = (RepeaterItem)btn.NamingContainer;
HiddenField hdnHotelCode = (HiddenField)item.FindControl("hdnHotelCode");
}
I tried something like this but its not finding hidden field.
The problem here is that lnkBookNow.NamingContainer is rptRooms. This control obviously does not contain hdnHotelCode.
I think you should be able to do this with:
protected void lnkBookNow_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
var item = (RepeaterItem)btn.NamingContainer.NamingContainer.NamingContainer;
HiddenField hdnHotelCode = item.FindControl("hdnHotelCode") as HiddenField;
}
btn.NamingContainer is a RepeaterItem in rptRooms. The NamingContainer of that is the Repeater itself. Finally, the NamingContainer of rptRooms is the RepeaterItem of rptHotels, in which you want to find your HiddenField.
Note my use of the as keyword instead of an explicit cast - this will protect you from NullReferenceExceptions if FindControl returns null. Of course, you should explicitly check that hdnHotelCode isn't null before you try to access it.

Resources