Form and QueryStrings in ASP.Net - c#

I have a form on home.aspx that is this:
<form name="search" method="post" action="searchresults.aspx" id="searchform" runat="server">
<div class="searchField">
<input name="keywords" type="text" id="keywordSearch" value="Enter keywords" class="watermark" />
</div><!--end searchField-->
<div class="advanceSearchBox">
<p><b>Narrow results by:</b></p>
<asp:Literal ID="ltrlPopulation" runat="server" />
<asp:Literal ID="ltrlDatasource" runat="server" />
</div><!--end advanceSearchBox-->
<div style="float: right; margin-right: 2px;">
<asp:ImageButton ImageUrl="images/go_up.png" AlternateText="GO" Width="34" Height="24" id="keywordSearchGO" runat="server" />
</div>
</form>
And on my searchresults.aspx.cs page I have this QueryString but its always empty:
Response.Write(Request.QueryString["keywords"]);
Did I forget something?

You could change the opening form tag to:
<form name="search" method="get" action="searchresults.aspx" id="searchform" runat="server">
If you really want to be using query string for some reason.

That's because QueryString is for GET requests, not POST. You want to use Request.Form for posted data.
Response.Write(Request.Form["keywords"]);
Read more documentation for the Request.Form Collection here.

A post doesn't generate a query string. To access post data, you'll need to do so through the Form or Params property.
Request.Form["keywords"];
Request.Params["keywords"];

You're posting to searchresults.aspx, so you will need to access your posted variables through Request.Form or Request.Params.
string keyWords = Request.Form["keywords"];
OR
string keyWords = Request.Params["keywords"];

Related

Read text from textarea/textbox in HTML into C# codebehind. (ASP.NET)

I'm trying to use code behind to read text from HTML provided to me. After researching this topic I found that almost all instances of this involve Web Forms controls(asp:) for the textboxes but the HTML I was given does not, but instead is:
<p>
<label>Address</label>
<textarea class="w3-input w3-border" name="addr" cols="30" rows="4"></textarea>
</p>
<div class="w3-half w3-container">
<p>
<label>Phone:</label>
<input type="text" class="w3-input"/>
</div>
<div class="w3-half w3-container">
<label style="padding-left:10px;">Email:</label>
<input type="text" class="w3-input"/>
</div>
</p>
Will I still be able to read the user-provided text from these boxes or will I need to alter the HTML?
A couple of my unsuccessful code-behind attempts to extract the address supplied:
string address = ((textarea)Address.FindControl("addr")).Text;
string address = ((TextBox)Address.FindControl("addr")).Text;
Update:
Using the server control described in a solution offered, I get an error message stating that "A page can have only one server-side Form tag."
This results from the following markup:
<form runat="server">
<asp:textbox id="addr" runat="server" textmode="multiline" />
</form>
followed later by:
<form runat="server">
<asp:Button ID="Ship" runat="server" Text="Ship" OnClick="Ship_Click" style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue" />
<asp:Button ID="Rate" runat="server" Text="Rate" OnClick="Rate_Click" style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue" />
</form>
The textarea is located in a different section than the buttons and I'm unclear on how to make both functional either without a form tag or without having them share the same one. Thanks
You need to use a server control if you're looking to access the value in code behind. Use an ASP TextBox and set the TextMode to MultiLine:
<asp:TextBox ID="textarea1" runat="server" TextMode="MultiLine" />
Then in code behind:
string addr = textarea1.Text;
UPDATE to demonstrate multiple forms on the same page:
<form ID="form1" runat="server">
<asp:Button ID="Ship" runat="server" Text="Ship" OnClick="Ship_Click" style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue" />
<asp:Button ID="Rate" runat="server" Text="Rate" OnClick="Rate_Click" style="padding: 10px; margin-bottom: 10px;" class="w3-round w3-blue" />
</form>
<form id="form2" action="WebForm1.aspx" method="post">
<asp:TextBox ID="textarea1" runat="server" TextMode="MultiLine" />
</form>
From here, you can use either method of retrieving the textarea1 value in code behind for posts from form1 or form2...
form1:
string addr = textarea1.Text;
form2:
string addr = Request["textarea1"].ToString();
Add runat="server" to your TEXTAREA and INPUT tags. Then you can access them from code-behind. You also need to assign the ID attribute of each one.
<p>
<label>Address</label>
<textarea class="w3-input w3-border" name="addr" id="textarea1" runat="server" cols="30" rows="4"></textarea>
</p>
<div class="w3-half w3-container">
<p>
<label>Phone:</label>
<input type="text" class="w3-input" runat="server" id="input1" />
</div>
<div class="w3-half w3-container">
<label style="padding-left:10px;">Email:</label>
<input type="text" class="w3-input" runat="server" id="input2" />
</div>
</p>

onsubmit function wont fire while having a runat=server attribute

<%# Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" MasterPageFile="MasterPage.master" %>
<asp:Content ID="ContentPlaceHolder1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
<asp:Content ID="ContentPlaceHolder2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<br />
<div id="signupform">
<form class="signupform" name="formreg" runat="server" onsubmit="return valform()"> <br/>
<input dir="rtl" type="text" name="uname" class="uname"/> </br>
<input dir="rtl" type="text" name="fname" class="fname"/> </br>
<input dir="rtl" type="text" name="lname" class="lname"/> </br>
<input dir="rtl" type="password" name="pword" class="pword"/> </br>
<input dir="rtl" type="password" name="rpword" class="rpword"/> </br>
<input dir="rtl" type="text" name="mmail" class="mail"/> </br>
<input dir="rtl" type="text" name="rmail" class="rmail"/> </br>
<input dir="rtl" type="text" name="gil" class="gil"/> </br>
<input type="checkbox" name="anon" value="True" class="dropan"/></br>
<input type="submit" name="sub" class="subbutton" value=""/>
<%=registrationstatus %>
</form>
</div>
</asp:Content>
This is my code, My problem is that the onsubmit="return valform()" attribute on my form wont fire if the runat="server" attribute exists, they can't work together please help me I am clueless why these two attributes wont work together
This is because you can't use both together. As said in the docs -
<input type="text" id="Textbox1" runat="server">
Doing this will give you programmatic access to the HTML element on the server before the Web page is created and sent down to the client. The HTML element must contain an id attribute. This attribute serves as an identity for the element and enables you to program to elements by their specific IDs. In addition to this attribute, the HTML element must contain runat="server". This tells the processing server that the tag is processed on the server and is not to be considered a traditional HTML element.
Here is the reference.
http://msdn.microsoft.com/en-us/library/aa478973.aspx
under section - Using HTML Server Controls
So, HTML with run='server' will not be treated as a normal Html element and thus will always be processed by the server. If you want to override the default behavior, you have to bind your javascripts with after the html is loaded in the browser, may be using jQuery or something similar. With jQuery something like this would help -
$(function(){
$("#formreg").submit(function(){
return valform();
});
});
By examing your code, I reach on conclusion that you might be using another form tag on Master page and there is no javascript issue. Only one form tag(Server Side) can exists either on a master page or content page. They can not be nested. When you would use runat="Server" attribute on both of form tags then error occurs saying "A page can have only one server-side Form tag". So remove runat="server" attribute from one of the form tags, onsubmit would begin firing.
I solved it with very easy way.
if we have such a form
<form method="post" name="setting-form" >
<input type="text" id="UserName" name="UserName" value=""
placeholder="user name" >
<input type="password" id="Password" name="password" value="" placeholder="password" >
<div id="remember" class="checkbox">
<label>remember me</label>
<asp:CheckBox ID="RememberMe" runat="server" />
</div>
<input type="submit" value="login" id="login-btn"/>
</form>
You can now catch get that event before the form postback and stop it from postback and do all the ajax you want using this jquery.
$(document).ready(function () {
$("#login-btn").click(function (event) {
event.preventDefault();
alert("do what ever you want");
});
});

asp.net can't take the text of input text

This is my aspx
<p>Date: <input type="text" id="datepicker" >
<asp:Button id="BookingForDate" runat="server" OnClick="BookingForDate_Click" Text="Search"/> </p>
<table id="DateBookingTable" runat="server" style="visibility:hidden">
<tr><th>ID</th><th>PlanTime</th></tr>
</table>
on my code in c# I can't see datepicker
why?
note please that i can take all the controllers from id except this one.
I already tried restart the visual studio
You have 2 options
add runat="server" to the input
<input type="text" id="datepicker" runat="server" />
or create an TextBox
<asp:TextBox runat="server" id="datepicker"></asp:TextBox>
You should add
runat="server"
to your input. No you are using an html control and not an asp.net server side control. Hence you can't access the value of the textbox the way you want.
So try the following and then you will be able to access the value of your textbox:
<asp:TextBox id="datepicker" runat="server"/>
Then in your code behind class you can access the value of this textbox
datepicker.Text
Another approach, as I pointed initially, it would be the following:
<input type="text" id="datepicker" runat="server">
You forgot to add the runat-attribute to your input-tag. Try this:
<input type="text" id="datepicker" runat="server">
With that change, you should be able to access the element from code behind
See MSDN
You have not given name to your control and also add runat="server"
<input type="text" id="datepicker" runat="server" name='datepicker' />

Post data to another site from code behind

I have a web form which is posting a few fields to a payment site. The structure the example is using is
<form method="post" action="<%= FormAction %>">
<input type="hidden" name="Hash" value="<%= sHash %>" />
<input type="hidden" name="MID" value="<%= sMID %>" />
......
<div class="submit">
<input type="submit" value="Post Data" />
</div>
Within the example project they have set the same variables within the code behind file (after declaring them) with values. When the button above is clicked it posts the data to the relevant page without errors and the user is redirected to the payment page.
So i am now trying the same under the click event of an image button on my site but doesnt work:
<asp:ImageButton ID="ButtonCheckout" runat="server" Text="Go to payment page" ImageUrl="~/Images/Payment.gif" />
and of course it doesnt work, ive added all the markup as above and added all sample code.
When i run the app it flashes and stops. I look in Fiddler and notice its not going to the payment site at all. The masterpage has declared in it - incase this makes any difference.
Reading around it seems i could use the image button to post the data and do exactly the same as the submit button (submit button is above which was part of the sample application from the payment provider) by using the onClick event in the markup with Javascript but im struggling to understand how to tie it all together and lost to what i could do next to get this working?
You can't just post form data with an image button. You need to submit the form data - otherwise your image button is just a link. I do this a lot with Javascript.
Try adding this.form.submit() to the clientclick. Or name your form and use this.formname.submit()
Either of those should post your form data.
You can such so write:
<form id="Form2" method="post" runat="server" action="<%= FormAction %>">
<input type="hidden" name="Hash" value="<%= sHash %>" />
<input type="hidden" name="MID" value="<%= sMID %>" />
......
<div class="submit">
<asp:ImageButton ID="ButtonCheckout" runat="server" Text="Go to payment page"/>
</div>
</form>
but you must add a few attributes in top of web page,eg:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" EnableEventValidation="false" EnableViewStateMac="false" ValidateRequest="false" ViewStateEncryptionMode="Never" %>

.net generating onclick

I am doing a form POST that looks like
<form action="http://ServerName/fse5/main/FormPost.aspx" id="frmLogin" method="post" target="_blank">
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<%= CompleteRequest %>
<input type="submit" name="__exclude__Submit" value="Run Illustration Software Online" runat="server" />
</table>
</form>
However, when I render this HTML the input tag looks like
<input onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(''); " name="ctl00$PlaceHolderMain$IllustrationSoftwareComponent$ctl00" type="submit" value="Run Illustration Software Online" />
How come ASP.net is adding onClick and name attribute? How do I stop this?
To stop it, don't make it a user control by adding runat="server"; it seems that you don't want it/need it.

Categories

Resources