I am making a page that the user should enter his password again to open and when he navigate away and come back again he should re-enter the password. NO SESSIONS OR COOKIES, just a simple page you enter the password you view it.
How can I do this ?
you should use 2 pages, one to enter the password, and the other to show the page...
the password page, will have a form as POST that points to page2.aspx
example of protected.aspx:
<form action="page2.aspx" mehod="post">
Pasword: <input type="password" id="pwd" name="pwd" />
<input type="submit" value="Enter" />
</form>
and the Page_Load event on page2.aspx should be something like
if(Request["pwd"] == null || Request["pwd"] != "123") {
Response.Redirect("InvalidPassword.aspx");
}
Use two divs.
One which contains the main content and other containing a textbox and button.
<div id="MainDiv" runat="server" Visible="false">Main Content goes here. </div>
And the login div
<div id="LoginDiv" runat="server" Visible="true">
<asp:TextBox ID="PasswordTextBox" runat="server"></asp:TextBox>
<asp:Button ID="LoginButton" runat="server" Text="Button" OnClick="LoginButton_Click" /></div>
On login button click handler, check the password and toggle the visibility.
protected void LoginButton_Click(object sender, EventArgs e)
{
if(PasswordTextBox.Text=="Password")
{
MainDiv.Visible=true;
LoginDiv.Visible=false;
}
}
Related
I have the following web form:
Working Form:
<form id="transactionParameters" method="post" runat="server" action="remote_url.aspx">
<fieldset>
<p>
<label for="Username">Username:</label>
<asp:TextBox id="Username" Text="TomSelleck" runat="server" />
</p>
<asp:Button ID="SubmitForm" runat="server" onclick="SubmitForm_Click" Text="Button" />
</fieldset>
</form>
And this successfully submits the form value Username to remote_url.aspx and brings the user to the remote page.
The problem I face is that I need to add a value to the form before submitting the form and redirecting the user e.g:
Desired functionality:
<form id="transactionParameters" method="post" runat="server">
<fieldset>
<p>
<label for="Username">Username:</label>
<asp:TextBox id="Username" Text="TomSelleck" runat="server" />
</p>
<asp:Button ID="SubmitForm" runat="server" onclick="SubmitForm_Click" Text="Button" />
</fieldset>
</form>
// This method is fired but I don't know how to execute the commented out code
protected void SubmitForm_Click(object sender, EventArgs e)
{
// Form["Username"] = Username.Text.Uppercase();
// Form["NewValue"] = DateTime.Now.ToString();
// ExecuteAction(Form, "remote_url.aspx");
}
You can accomplish this in several different ways.
Query String
Session
Cookie
Internal variable / object
The question will be what are the requirements? The easiest would be the Query String, simply do Response.Redirect("...aspx?username=Tom");.
Then on the other form you would simply parse the Query String. Request.QueryString["username"]?.ToString().
The drawback would be your url has a username in clear text and is exposed easily. The Session would store a unique identifier of that information into a cookie, making those approaches similar. But equally easy and may meet your requirements.
An internal object variable after the method fires, storing that object in memory making it accessible and exposed on the other page load event will work also.
I am trying to get html textbox value when click on html button without using "runat" attribute.I need to do it in code behind ,is it possible?
<button id="button1" onclick="btnclick">Click Here</button><input type="text" id="txtBox1" name="txtBox12" />
and My code Behind is like:
protected void btnclick(object sender, EventArgs e)
{
string name = Request.Form["txtBox12"];
Response.Write(name);
}
MODIFIED
To access any control in code behind it needs to have runat=server attribute attached to it.
In your case, like you pointed you can transfer the value of input text to asp hidden field and then access
its value on button click in server-side but in this case too you have to place hidden field inside form runat=server tag.
<form id="form1" runat="server">
<input type="text" id="txt" onkeyup="PassValue(this);";/>
<asp:HiddenField ID="hf" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />
</form>
-----------------------
<script type="text/javascript">
function PassValue(obj) {
document.getElementById("hf").value = obj.value;
}
</script>
So, I have a Paypal form that worked wonders. However, I now need to add a coupon field, where someone can enter a code, and get a reduction based on whatever the backend replies.
This all works wonderfully, but I've ran into an issue when adding the option to know before checking out whether your code is valid or not. Currently, my form (once simplified) looks like this :
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
id="payPalForm" runat="server">
<asp:TextBox runat="server" ID="txtCode" />
<asp:Button Text="Validate" OnClick="ValidateDiscount" runat="server" />
<asp:Label ID="txtDesc" runat="server" />
<input type="submit" name="Submit" value="Pay up!" />
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
...
</form>
With the backend having the function :
protected void ValidateDiscount(object sender, EventArgs e)
{
this.txtDesc.Text = "fetch from database using: " + txtCode.Text;
}
My issue is that wheneve I click on the Validate button, the form is submitted and I end up on the Paypal website. I used Jquery at first with preventDefault(), but that actually prevents my server-side function from firing. I've also tried putting a standard <button> or <input type='button'> tag instead, but I couldn't get it to fire my server-side function.
Is there any way to have the Validate button not submit the form, or should I just remove the action from the form and manually submit the form when clicking on the submit button?
You have set your form action to post to PayPal.
This is the action:
action="https://www.sandbox.paypal.com/cgi-bin/webscr"
Here is where you have it in you form tag:
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
id="payPalForm" runat="server">
Remove this from your form tag and it should postback to your application.
I am a new ASP.NET Web Forms developer and I am struggling right now with hiding a part of the user control in some of the pages which have it based on certain factors.
ASP.NET code of the user control:
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="pnlView" runat="server" CssClass="panel">
<span class="lead text-info">This is a simple test user control</span>
</asp:Panel>
<asp:Panel ID="pnlActions" runat="server" Visible="false">
<asp:Button ID="btnConfirm" runat="server" Text="Confirm" CssClass="btn btn-primary btn-lg"
OnClick="btnConfirm_Click" />
<asp:Button ID="btnReject" runat="server" Text="Reject" CssClass="btn btn-danger"
OnClick="btnReject_Click" />
<asp:Label ID="lblInfo" runat="server" Text="" CssClass="label label-info"></asp:Label>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Code-Behind of the user control:
protected void btnConfirm_Click(object sender, EventArgs e)
{
lblInfo.Text = "Confirmed";
}
protected void btnReject_Click(object sender, EventArgs e)
{
lblInfo.Text = "Rejected";
}
The asp.net panel with id "pnlActions" should be displayed based on the username of the user who is going to access the .aspx page that has this user control. So how can I do that?
Here's the ASP.NET code of the .aspx page:
<div class="row">
<div class="col-md-6">
<span class="lead">This is the user control</span>
<uc:TestUserControl ID="TestUserControl1" runat="server"></uc:TestUserControl>
</div>
<div class="col-md-6">
<p class="well">
The user control on the left side has an asp.net panel control which
has two buttons. These two buttons should be shown if the user
is an administrator, and they should be hidden for the rest of users.
This user control will be used on four pages across this test application.
</p>
</div>
</div>
Code-behind of the .aspx page:
string username = "JohnA";
protected void Page_Load(object sender, EventArgs e)
{
if (username == "JohnA")
{
//pnlActions control in the user control should be displayed
}
else
{
//pnlActions control should be hidden
}
}
These control specific actions should be placed in your user controls code behind, rather than in your pages layout, to provide more modularity.
Later the goal is to put all of these custom components in different variations on the page, thats the point of custom user controls.
You canĀ“t accomplish that by putting business logic of your individual components in the layout.
If you really want to do that you can do it by exposing your panels by public properties in your controls code behind or modifing the designer code(not always a good idea...).
You could read into this article:
http://www.codeproject.com/Articles/28783/Your-First-ASP-NET-Custom-Control
try to find control by id in user control code .
or there is a step to get the page control first who hosts the user control and then find the control by id from that page . once u have reference to your control you can definitely do all operations.
Please use the following line in the user control.
this.Master.FindControl("pnlView").Visible = true;
HTML Page
<body>
<form id="form1" action="Default.aspx" method="post">
<input runat="server" id="txtuser" type="text" />
<input runat="server" id="txtpwd" type="password" />
<input type="submit" value="Login"/>
</form>
</body>
Code-behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Login();
}
}
private void Login()
{
if (checkUser(Request.Params["txtuser"],Request.Params["txtpwd"]))
{
Response.Redirect("Success.aspx");//if success
}
}
I am developing a web page for old mobile version (like nokia N70) facing a problem. When I submit my username and password then check user return true to redirect to a new page. But it won't redirect to success.aspx. So I debug point on the Response.Redirect code, it can stop there and I continue run become error because getting the username&password null. Then I realized it loaded the page twice. How to solve it?
You want to login when there IS a PostBack. Not the other way around.
Change
if (!Page.IsPostBack)
to
if (Page.IsPostBack)
Make sure you have set AutoEventWireup to true in Code Front:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"...
otherwise Page_Load is never fired.
You should use Forms Authentication in a proper way, like explained here
You should do something like this for your redirect:
if (checkUser(userName.Text, password.Text))
{
FormsAuthentication.RedirectFromLoginPage(userName.Text, false);
}