Override asp net web form action - c#

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.

Related

C# function error as cannot redirect to another page

so I am still new to C# and I currently testing out my project regarding a search function when user key in what they want to find out in a the textbox and press enter and it will redirect them to my search page. However everytime i tried to type things in the search button and press enter what came out is
http://localhost:51182/Default.aspx
to
http://localhost:51182/Default.aspx?ctl00%24search=(whatever I keyed).
As the search function is in the masterpage.aspx i cannot use asp:buttons or what.
This is my code in the masterpage.
<form class="navbar-form navbar-right">
<input type="text" id="search" class="form-control" placeholder="Search..." runat="server">
<button class="btn btn-default" type="submit" id="Button1" onserverclick="Button1_OnClick"><i class="glyphicon glyphicon-search"></i></button>
</form>
This is my c# code for masterpage.
protected void Button1_OnClick(object sender, EventArgs e)
{
//The code was commented out since the search name was not able to be recognized in my webform input id so i tried to deal with the response first.
//String searchtext = search;
//String Search = Server.MapPath("Website\\HTML\\SearchEngine.aspx");
Response.Redirect("~/Website/HTML/SearchEngine.aspx?search=");
}
I mainly put the codes that I think that matters and if required the other part just say so. In addition to it if this is a duplicate to other question, I am sorry as I been trying it out on some other question answer for their fixes to this but i wasn't able to get my working then I submit this question.
Hope you guys can help me out.
<form class="navbar-form navbar-right">... </form>
In ASP.NET WebForm, you cannot use another form tag, since there is a main form tag with runat="server" already.
The easiest way to achieve is to use Panel. When user types inside textbox and press Enter key, it will call Button1_OnClick event.
For example,
<asp:Panel ID="SearchPanel" runat="server" DefaultButton="SearchLinkButton">
<asp:TextBox runat="server" ID="SearchTextBox" CssClass="form-control"
placeholder="Search..." />
<asp:LinkButton runat="server" ID="SearchLinkButton" CssClass="btn btn-primary"
OnClick="SearchLinkButton_Click">
<i class="glyphicon glyphicon-search"></i>
</asp:LinkButton>
</asp:Panel>
So you can't have nested forms, but that's not to say that you can't have multiple forms on the same page, they just can't overlap, and only one can be an ASP.NET Form[runat=server]. Just make sure the search form, which can be a vanilla HTML form, is placed in the master page outside of any Form[runat=server] and format the search form like so:
<form class="navbar-form navbar-right" action="/Website/HTML/SearchEngine.aspx">
<input type="text" name="search" class="form-control" placeholder="Search..." />
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</form>

Processing HTML form with c#

I am a newbie to C# development. I created an HTML form, and I want to run its input using C#.
My HTML form right now:
<form action="default.aspx.cs" enctype="multipart/form-data" method="post">
<input type="text" name="first_name" id="fname"
placeholder="e.g. Jane Doe" required maxlength="20" >
<p>
Please specify a file, or a set of files:<br>
<input type="file" name="datafile" size="40">
</p>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<!--<input type="submit" value="Submit"> -->
</div>
</form>
My Default.aspx.cs file contains:
protected void Button1_Click(object sender, EventArgs e)
{
}
I want to pass the contents from the html form to that Button1_Click function. The name input is there as a dummy field, my actual job is to get that file.
You can access the values through the Request.Form object like so;
Request.Form["fname"]
Or you can do it the more correct way, by changing the <input> tags to be <asp:TextBox> tags, you can then access them by name eg. fname.Text

prevent Asp.NET button from submitting form

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.

Protect an ASPX page with a password

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;
}
}

Form fields not retained on postback

I'm developing a website using EPiServer. I have a form which submits to itself.
On submit, I check if there are any fields missing. If yes, then error message is shown.
The problem is that my fields are reset when submitted.
I could check this using jQuery, but I'm not. I'm cheking this from code behind.
I've tried setting EnableViewState=true sevceral places, but no luck.
Here's part of my code:
<asp:Content ID="Content3" ContentPlaceHolderID="RightContentPlaceHolder" runat="server">
<asp:Panel ID="panelComplaint" runat="server">
<li>
<h3>Postnummer*</h3>
<input id="senderPostCode" name="postCode" type="text" size="20" enableviewstate="true" />
<asp:Label runat="server" id="lblPostCode" CssClass="missingField" Text="Mangler tekst" Visible="false" />
</li>
<li>
<h3>Post sted*</h3>
<input id="senderCity" name="city" type="text" size="100" />
<asp:Label runat="server" id="lblCity" CssClass="missingField" Text="Mangler tekst" Visible="false" />
</li>
<li>
<div class="spacer10px"></div>
<button type="submit" name="Send" >Send me</button>
</li>
</asp:Panel>
</asp:Content>
What do I need to do, in order to retain form fields?
Have you tried adding runat="server" to the input fields? As far as I'm aware, this is needed to ensure that asp.net round-trips data properly. I'm not sure if there's anything specific to EPiServer that would make that differ.
The enableviewstate attribute, unless it's specific to EPiServer, won't actually be doing anything.
If having the ID mashed up by ASP.net is a problem, consider upgrading to .NET 4.0, if you can, as this provides ways for you to control how the ID's are modified. If not, you could try placing a friendly translation into the page that you can then access via JQuery, for example:
<script language="javascript" type="text/javascript">
function getSenderPostCode() { return eval('<%=senderPostCode.ClientID%>'); }
var senderPostCodeField = JQuery('#getsenderPostCode');
alert(senderPostCodeField.length());
</script>
That said, having "getSenderPostCode()" to reference would mean that you could probably skip the step with JQuery of getting a reference to it, so the following would work:
alert(getSenderPostCode().length();
By adding runat="server" to your controls you then wouldn't need to use Request.Form["senderPostCode"].ToString() to access the content of that field, you could instead access it directly:
var contentOfSenderPostCode = senderPostCode.Value.ToString();

Categories

Resources