why it says there are not my text boxes - asp.net

i wrote below code . but i do not know why i get error. but every thing seems ok.
it says there are not my text boxes in context. but there are .
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<form id="form1" runat="server">
<asp:TextBox ID="address" runat="server"/>
<div class="form1">
<label for="fname">
<span>name:</span>
<asp:TextBox ID="fname" runat="server"/>
</label>
<label for="lname">
<span>lname: </span>
<asp:TextBox ID="lname" name="lname" runat="server" />
</label>
.
.
.
</div>
protected void send2_Click(object sender, EventArgs e)
{
tbl_register tbl = new tbl_register();
tbl.firstname = fname.Text; // The name 'fname' does not exist in the current context
tbl.lastname = lname.Text; // The name 'lname'does not exist in the current context
tbl.address = address1.Text; //..
tbl.phone = phone.Text; //..
tbl.email = email3.Text; //..
db.tbl_registers.InsertOnSubmit(tbl);
db.SubmitChanges();
}

The textboxes as coded are children of the label. I do not code my lables in that way. In order to use controls that are child controls you must use Control.FindControl, in this case fname.FindControl("fname"). I would not name controls the same as other controls. You can also close the label tag such that it does not enclose those controls, and then the control can be referenced via the name.

the line
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
suggests that this is a webform that uses a MasterPage.
inside the content place holder you have a form declaration that runs at server:
<form id="form1" runat="server">
however, on your MasterPage.master you already have a form that runs at server that wraps the content placeholder.
asp.net does not allow 2 nested forms to both run at server.

Related

asp:button onclick event causing Internal Error

MasterPage:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
ASPX page:
<%# Page Title="" Language="C#" MasterPageFile="~/NewFMaster.master" AutoEventWireup="true" CodeFile="Home17.aspx.cs" Inherits="Home17" %>
....
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
....
<form id="form1" runat="server">
<div class="form-group">
<div class="col-sm-6">
<asp:TextBox ID="txtFullName" runat="server" CssClass="name" placeholder="Full Name"></asp:TextBox>
</div>
<div class="col-sm-6">
<asp:TextBox ID="txtContact" runat="server" MaxLength="12" CssClass="name" placeholder="Contact Number" />
</div>
</div>
<asp:Button ID="submitButton" runat="server" CssClass="submit-btn" style="width:150px" Text="Submit" OnClick="submitButton_Click"/>
</form>
....
</asp:Content>
CodeBehind:
public partial class Home17 : System.Web.UI.MasterPage
{
....
protected void submitButton_Click(object sender, EventArgs e)
{
Response.Redirect("Home16.aspx");
}
If I remove OnClick="submitButton_Click" from asp:Button line, then the page atleast displays, otherwise I get "Internal Server error"
Web.Config: CustomerErrors are off & have tried with both true/false for debug
UPDATE 24 Apr 2018:
Now Internal Server Error is shown on all pages. Entire site has gone done.
In the error Log last error is:
(I have added the new keys)
<Error>
<Message>Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster,
ensure that <machineKey> configuration specifies the same validationKey and validation algorithm.
AutoGenerate cannot be used in a cluster.
http://go.microsoft.com/fwlink/?LinkID=314055</Message>
<PageName>http://www.***.*******.com/Product.aspx?productID=69584&productID=69584</PageName>
<Date>4/23/2018 8:58:44 PM</Date>
<UTC>4/24/2018 3:58:44 AM</UTC>
</Error>
Note: The hosting provider is BigRock. (have asked them for help but they simply said I'll have to resolve it, its not their issue)

Change id of elements in ContentPlaceHolder

I use masterpage in asp.net web form, I use span to display message to users
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<div class="col-md-10 col-sm-10">
<span id="spnMessage" class="btnText" runat="server"></span>
</div>
</asp:Content>
and code is:
spnMessage.InnerText = "Add record successfully ";
but in runtime id of span change to "ContentPlaceHolder2_spnMessage" and my code does not work. What should I do?
Change the span into a Literal. Then there will be no more issues with ID conflicts.
<asp:Literal ID="spnMessage" runat="server"></asp:Literal>
Then accessing the Literal from code behind will always work.
spnMessage.Text = "Add record successfully ";
Or if you need to use the <span> element for CSS purposes, you can still wrap the Literal with it.
<span><asp:Literal ID="spnMessage" runat="server"></asp:Literal></span>
You could also use a aspnet Label.
A label will generate it's own <div> tag in HTML.
<asp:Label ID="spnMessage" runat="server" Text="Add record successfully"></asp:Label>
Will become
<div>Add record successfully</div>

How to disable autocomplete when using master pages

We are migrating a existing asp.net application without master pages to a new one with master pages.
Old Code:
<form id="Form1" method="post" runat="server" autocomplete="off">
When using master pages how will we put autocomplete="off" in ContentPlaceHolder?
can we use
<asp:Content ID="Content1" ContentPlaceHolderID="ContentArea" runat="server" autocomplete="false">
If you want to disable it globally you should use jQuery:
$(document).ready(function () { $("input").attr("autocomplete", "off"); });
Add autocomplete="off" to Site.Master's Form tag.
Like this...
<form id="MasterForm" runat="server" autocomplete="off">

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 =

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