Can ASP.Net form have method=get or post attribute? - asp.net

I am new to asp.net.
My question is, can a ASP.net form with runat="server", have a method attribute in it?
For example:
<form id="form1" runat="server" method="get">
.......
</form>
Is this possible?

Thanks for your answers.
I would like to share some points which I found.
By default the form with runat="server", will have method="post".
But when we request a page for the first time, (i.e) request is not a postback, the method="get".
And it becomes method="post",while postback.
I checked this by placing a piece of code in code behind:
In Page_Load():
if(Request.RequestType=="GET")
{
Response.Write("Request is a GET type");
}
else if(Request.RequestType=="POST")
{
Response.Write("Request is a POST type");
}
By default, the output
For the first request of that page: Request is a GET type
In postback: Request is a POST type
If i give the following code in the WebForm1.aspx
<form id="form1" runat="server" method="get">
For this, the output will be:
For the first request of that page: Request is a GET type
In postback: Request is a GET type
This is what I found.
Thank you very much for your responses.

yes you can try like below.
design part
you can design a form like :
<form id="form1" runat="server" method="post">
<input type="radio" name="Gender" value="male" id="test" checked="checked" />
male
<input type="radio" name="Gender" value="female" />female
<input type="submit" value="test" />
<asp:Button ID="btn" runat="server" Text="value" />
</form>
and how to get value from the form :
if (Request.Form["Gender"] != null)
{
string selectedGender = Request.Form["Gender"].ToString();
}
with this way you can get the value from form in asp.net.
i hope it will help you.

Yes,You can use use Method attribute..
The default will be Method="Post"
The form is always submitted to the page itself. If you specify an action attribute, it is ignored. If you omit the method attribute, it will be set to method="post" by default. Also, if you do not specify the name and id attributes, they are automatically assigned by ASP.NET.
If you select view source in an .aspx page containing a form containg these properties...
Please refer : http://www.w3schools.com/aspnet/aspnet_forms.asp

Related

success message afer submit an html form

I have a simple html form like:
<form id="form1" method="post" target="_self">
<input type="text" name="lastname">
<input type="submit" value="Register" />
<form>
I want to replace the whole of this same page with just a success message
from the server, which will also show 'lastname' that was send.
Is there a simple way to do it with from the server side?
(asp.net forms application)
Thanks a lot
Liron
For message and lastname you can pass a query string and display message and another way to submit form using ajax call.

getting a value from user control

I have an issue that i believe there is a simple solution for but being new to ASP.NET it is not very clear to me.
I have a user control that has a for loop that makes bunch of hyperlink elements in a list (looks like below>
<li><a href="blahblah.aspx?ID=1..../></li>
<li><a href="blahblah.aspx?ID=2..../></li>
<li><a href="blahblah.aspx?ID=3..../></li>
<li><a href="blahblah.aspx?ID=4..../></li>
Next, that uc is used in another page with in a <form> tag with method="post" and <asp:button> that isn't really used.
Next, when one of the links is clicked it will go to blahblah.asp and get the ID in there via Request.QueryString
So far so good.
What i want to do though (and the reason for this post) is that i want to not use ID as a query parameter. I want to pass the ID in the body. So the link would be blahblah.aspx and it wouldn't show the ID.
What would be the best way to accomplish this. I've tried couple different ways but it's not working.
Not sure if this will work for you, but instead of using html a tags, use the asp LinkButton control. You can then handle the command event of this control so have your for loop generate the following controls (or better yet use a repeater):
<asp:LinkButton id="button1" runat="server" CommandArgument="1" OnCommand="SendToBlah" />
<asp:LinkButton id="button2" runat="server" CommandArgument="2" OnCommand="SendToBlah" />
private void SendToBlah(Object sender, CommandEventArgs e)
{
Session["ID"] = e.CommandArgument;
Response.Redirect("blahblah.aspx");
}
You can then use your session argument inside your blahblah.aspx page however you see fit.
To send the data in the HTTP body instead of the query string, you need to do an HTTP post. This is easy enough, just use a form with a hidden parameter:
<li>
<form action="blahblah.aspx" method="post">
<input type="hidden" name="ID" value="1" />
<input type="submit" value="submit" />
</form>
</li>
To get the data on the target page, use Request.Params instead of QueryString.
Now, if you want to use a hyperlink rather than a "submit" button, you can always submit the form using Javascript:
<li>
<form id="myform1" action="blahblah.aspx" method="post">
<input type="hidden" name="ID" value="1" />
</form>
Submit the form
<form id="myform2" action="blahblah.aspx" method="post">
<input type="hidden" name="ID" value="1" />
</form>
Submit the form
</li>
<script type='text/javascript'>
window.submitForm = function(id) {
document.forms[id].submit();
return false;
}
</script>

Pass two variables in anchor link

having the following anchor tag, I would like to pass one variable for use in the redirect form. As you see I have here the destination url and the name of the link. I'm using Request["changeId"] for get it in the form.
<td class="id"><a href='/workplace/managechange?ChangeId=<%#DataBinder.Eval(Container.DataItem, "aux_RelatedChangeID.Id")%>'><%# DataBinder.Eval(Container.DataItem, "aux_customChangeId")%></a></td>
Can I pass another variable to the form, <%# DataBinder.Eval(Container.DataItem, "aux_approvalID")%>, and get it via Request?
Has this variable to be visible in the url?
Thanks!
IT will not be possible to hide a parameter using a get request / link, if you used a form post then it would be 'hidden' from the user.
This would require either...
<form action="/workplace/managechange" method="post">
<input type="hidden" name="ChangeId" value="<%#DataBinder.Eval(Container.DataItem, "aux_RelatedChangeID.Id")%>" />
<input type="hidden" name="approvalID" value="<%# DataBinder.Eval(Container.DataItem, "aux_approvalId")%>" />
<input type="submit" value="Submit Values" />
</form>
Or doing this by AJAX potentially.
Do you need to pass both variables , could you not derive it when the managechange page / action is called?
Also why do you need to hide this item from the query string?
It is already public and from looking at the query string it is not seo'd, so that woudl not appear to be a reason either.
Regards
Steve
I found the solution to my first question:
<td class="id"><a href='/workplace/managechange?ChangeId=<%#DataBinder.Eval(Container.DataItem, "aux_RelatedChangeID.Id")%>&approvalID=<%# DataBinder.Eval(Container.DataItem, "aux_approvalId")%>'><%# DataBinder.Eval(Container.DataItem, "aux_customChangeId")%></a></td>
And I'm thinking will not be possible hide it in the url, right?

How to obtain a value of an input (type text) on server side in ASP.NET?

Using the HTML markup
<form id="form" runat="server">
<input id="donkey" type="text" placeholder="monkey" runat="server" />
</form>
I hoped to get the entered value in code behind by
String s = Request.Form["donkey"];
but it only produces null value. When I investigate the data structure I get something like $ctl00$main$donkey so I know it's there. After a while, I simply switched to
<form id="form" runat="server">
<asp:TextBox id="donkey" type="text" runat="server"></asp:TextBox>
</form>
but I still wonder how to reference the object from server-side if I for some reason won't switch to ASP-component.
If you want to access to the value using request.form, add name attribute to input tag and remove runat attribute.
<input id="donkey" name="donkey" type="text" />
Otherwise use
<asp:TextBox ID="donkey" type="text" runat="server"></asp:TextBox>
and on cs
string s = donkey.Text;
if you want to get value of input use like this
String s = donkey.value;
I'm not sure about ASP.net but for a regular form field to submit properly it should have a name attribute. That would be the key that you could then lookup the form value.
Just donkey.Value will return the value from text input which should have runat="server". It will create an object of System.Web.UI.HtmlControls.HtmlInputText.

Passing a value from Page_Load to Form with POST

I'm really new to ASP and have a project where I need to capture the login of the person logged into Sharepoint. I have created the following code and am able to get the login. I am having trouble pass the parameter to a form. What is the best way to do this: I know the 2nd part the POST is wrong, I don't know what to do here. Thanks for your help. Scott
<script language="C#" runat="server">void Page_Load(Object sender, EventArgs e)
{
string userName = "NA";
userName = HttpContext.Current.User.Identity.Name;
string userWithoutDomain = userName.Substring(userName.IndexOf('\\') + 1);
myuserid.Text = userWithoutDomain;
}
</script>
<html>
<body>
<form method="POST" action="http://srs.xxx.edu:9001/signon/authenticate.asp" id="myuserid" name="myuserid">
<input type="hidden" name="COOKIEPATH" value="/SRS/">
<input style="width:70px;font-size:8pt;" size="8" name="myuserid" >
<input style="width:70px;font-size:8pt;" size="8" name="myuserid" >
<input class="button" type="submit" value=" Log On " style="display:none;">
</form>
<form id="form1" runat="server">
<asp:label id="myuserid" runat="server" />
</form>
</body>
</html>
You can pass values from the codebehind to html page using for example hidden values, see here.
Usually though values are placed into form elements in the codebehind directly. If the element has the "runat=server" attribute then you can access the element in the Page_Load method using c# and just set the text value directly.
myuserid.Text =

Resources