Passing a value from Page_Load to Form with POST - asp.net

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 =

Related

Submit form (without runat=server) from aspx page

as we all know there is only one form possible on aspx page. How can I add html form to aspx page from code begind and submit data of this created form?
Here is form example:
<form name="payment" method="post" action="https://sci.interkassa.com/" accept-charset="UTF-8">
<input type="hidden" name="ik_co_id" value="51237daa8f2a2d8413000000" />
<input type="hidden" name="ik_pm_no" value="ID_4233" />
<input type="hidden" name="ik_am" value="1.44" />
<input type="hidden" name="ik_desc" value="Payment Description" />
<input type="submit" value="Pay">
</form>
I have and idea to build string using ClientScriptManager first and than attach this form to div using jQuery. Something like that. Is there some bettre ideas?
I'm going to use something like that, but I don't like this solution:
Dim sname As [String] = "Interkassa"
Dim stype As Type = Me.[GetType]()
Dim cs As ClientScriptManager = Page.ClientScript
If Not cs.IsStartupScriptRegistered(stype, sname) Then
Dim sb As New StringBuilder()
sb.Append("<script type=text/javascript>")
sb.Append("$(document).ready(function () { var s = ""<form name='payment' method='post' action='https://sci.interkassa.com/' accept-charset='UTF-8'><input type='submit' value='Оплата через Интеркассу'></form>"";")
sb.Append("html = $.parseHTML(s);$('#interkassa').append(html);});")
sb.Append("</script>")
cs.RegisterStartupScript(stype, sname, sb.ToString())
End If
You don't need to add the HTML using code behind. That's a bad practice because it provides poor separation between logic and markup. Instead, create a button on your page where it fires a server side event, then have your server side event handle the logic.
<asp:Panel runat="server" ID="MyPaymentPanel">
<asp:Button runat="server" ID="SubmitPaymentBtn" Text="Pay" OnClick="SubmitPaymentBtn_Click" />
</asp:Panel>
Then in your code behind...
protected void SubmitPaymentBtn_Click(object sender, EventArgs e)
{
//Retrieve values for ik_co_id, ik_pm_no etc from your database
//post the form if sending to a site you don't control, or update your database if you control sci.interkassa.com
}
Try like this
<button id="ClearButton" class="Button2" onclick="javascript:temp();return false;">CLEAR</button>
<script type=text/javascript>
Function temp
{
// your code
}
</script>

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

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

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>

Password-Box not showing any content

I have a small websolution that needs the user to input a password. I have two input boxes
<input type="password" runat="server" id="m_txtPassword1"/>
If I set some chars to the Value-property of the control like this:
m_txtPassword1.Value="someChars";
The password box is rendered empty. No bullets are shown. If I look into the rendered html-source, also no value-tag has been rendered. If I change the type to
<input type="text" runat="server" id="m_txtPassword1"/>
the chars are shown. Is this by design? How can I disable this feature?
Please note, I don't want to put a real password into the value-property, I only want to show the user that there is already a password set, and this is IMO done best with some 8 bullets in the input-control. But for this, I need the possibility to set the value-property of the control.
Update
For all, having the same problem: I have tried to declare <asp:textbox id="m_txtPassword1" runat="server" TextMode="Password" /> with the same result. Also m_txtPassword1.Attributes["value"]="someChars" has not helped.
It seems that this is realy not possible.
As a workaround, I declared the password-boxes as plain html without the runat="server" and have set the value-property in markup (via two properties from the code-behind). Not nice but I really want to show the user that he has already entered a password.
Another workaround would be to set the value through javascript on load.
This is by default. You cannot set a password.
I make this works,
<html>
<head>
<script type="text/javascript">
function alertValue()
{
alert(document.getElementById("password1").value);
}
function setpassword()
{
password1.value="someChars";
}
</script>
</head>
<body>
<form>
<input type="password" id="password1" value="" />
<input type="button" id="button1" onclick="alertValue()" value="Show default value" />
<input type="button" id="button2" onclick="setpassword()" value="Set value" />
</form>
</body>
</html>
Try this:
http://jsbin.com/ocexo5
It is by design, for security reasons - so that the password is not in the
HTML in plain text.
Now, you could write out javascript to set the value property of the textbox
on the client, of course this would still mean that you have the password in
plain text in the HTML.
Here is example of the page:
Markup:
<script type="text/javascript">
function setPwd()
{
var Pwd = "<%=Pwd %>";
var txtText = document.getElementById("MainContent_txtText");
txtText.value = Pwd;
}
</script>
<input type="password" id="txtText" runat="server"/>
And code-behind:
public partial class _Default : System.Web.UI.Page
{
public string Pwd;
protected void Page_Load(object sender, EventArgs e)
{
Pwd = "password";
ClientScript.RegisterStartupScript( this.GetType(),"somescript","setPwd();",true);
}
}
Textbox11.Attributes.Add("Value",whatever_your_password_is)

asp.net dynamic forms question

I'm trying to figure out a way to create a page that can be used for generic forms. What i had in mind was someone here at our office would use the obout edit to create html that would look like a form. This html would be saved to a database. When the user wanted to fill out the form and mail it, they would click on the correct form retrieval and this html would be put into a label control. So now the page has a label control on it with some html as the text of the label. The user could type in the values that they wanted. So the html might look something like this:
<p style="margin: 0px; text-align: center;">
Quarterly Report of Employees Serverd</p>
<br />
<br />
Employee:
<input type="text" style="width: 300px; height: 22px;" />
<br />
<br />
Address:
<input type="text" style="width: 300px; height: 22px;" />
<br />
<br />
<br />
The user would type in the input type="text" a value of say John Smith and then type into the address input type as well.
Now I want to grab all this html and mail it off. I know how to do the mailing portion, but the grabbing the html I'm not getting. I can grab the html in the label, but the text that that user typed in is not included in that text. So how do I get that html and the text the user typed in. Any ideas.
thanks
shannon
You need to encode the html to store them securely.
Use literal control instead of label or text controls.
Check this link for the first option.
Thanks :)
and you can use a way like this :
<input id="textField" type="text" runat="server"/>
C# - Code Behind
Mail.Send(textField.Value);
and don't forget to use ValidateRequest="false" in the header section of asp.net..
:)
You can iterate through the Request items to get the submitted values. If you want to send of an email which substitutes the entered values for the HTML input fields, you'll have to parse out the form HTML to find your input fields:
<input type="text" id="firstName" name="firstName"
style="width: 300px; height: 22px;" />
And then replace it with
Request.Form["firstName"]
Notice that the input fields will need identifiers so you can retrieve them from the HTTP request.
Hope this helps!
For one thing, you're really going to want to get a little more information into your html - at the very least some name attributes on the form elements:
Employee: <input type="text" name="employee" />
This way, you could create an aspx page like:
<%# Page language="C#"
autoeventwireup="true"
codebehind="DynamicForm.aspx.cs"
inherits="TempWebApp.DynamicForm" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal id="m_HtmlForm" runat="server"></asp:Literal>
<asp:Button id="m_SubmitForm"
onclick="SubmitForm_OnClick"
runat="server"
text="Submit" />
</div>
</form>
</body>
</html>
Then in your code you read your html from the database into the .Text of the literal control:
using System;
using System.Web.UI;
namespace TempWebApp
{
public partial class DynamicForm : Page
{
protected void Page_Load(object sender, EventArgs e)
{
m_HtmlForm.Text = getFormData();
}
protected void SubmitForm_OnClick(object sender, EventArgs e)
{
// Just showing we've picked up the form data.
// You'll also see ViewState and others in here.
foreach (string key in Request.Form.AllKeys)
{
m_HtmlForm.Text += string.Format("<br />Key: <b>{0}</b><br />Value:<br />{1}<br />", key, Request.Form[key]);
}
}
private string getFormData()
{
// Excluded for brevity
}
}
}
Obviously, in your button/postback handler, you would need to ensure you have the original HTML from the database, and then either modify the input elements to add a text attribute to them with the value, or more likely, replace them with the actual values - otherwise you'll have to do lots of additional parsing of the form if your users start saving forms with radio buttons, check boxes, select lists, etc.

Resources