I am making a Login Page with ASP.Net using C#... and i have put my html code in Login.aspx page . that contains a textbox named username but when I give reference of my textbox in Login.aspx.cs to make the validations on this text box ... as
if(string.IsNullOrEmpty(username.Text))
{
...
...
}
i am getting an error that is saying ...-"The 'username' doesn't exist in the current context"......
how can i get rid of this error .... it is making me mas .... Help will be appreciable towards this error ... please help me ....
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
It depends on what you have taken.
If you have draged and droped ASP TextBox the automatically code has generated as:
<asp:TextBox ID="username" runat="server"></asp:TextBox>
which automatically contains runat="server"
But if you have taken HTML text box then code is as follows:
<input id="username" type="text" />
In this you will have to add the line on your own as runat="server"
It will look as follows:
<input id="username" type="text" runat="server"/>
Then you can use it for serverside.
Hope its helpful.
Related
I have a Default.aspx and a web user control that adds in Default.aspx
Is there any way that i can change value of a div in Default.aspx from web user control?
for example :
<meta name="description" content="" id="MetaDescription" runat="server"/>
<meta name="keywords" content="" id="MetaKeywords" runat="server"/>
or :
<div id="test" runat="server"></div>
Thank you
You can change value of parent page's any control, I made my sample for label control by using the following code. I have made one sample please check it.
Main Page ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UseUsercontrol.WebForm1" %>
<%# Register TagPrefix="My" TagName="UserInfoBoxControl" Src="~/WebUserControl1.ascx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<My:UserInfoBoxControl runat="server" ID="MyUserInfoBoxControl" />
<asp:Label ID="Label1" runat="server" Text="Main Page"></asp:Label>
</div>
</form>
</body>
</html>
User control ASCX:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="UseUsercontrol.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
User Control CS:
protected void Button1_Click(object sender, EventArgs e)
{
Label parentPanel = this.Parent.FindControl("Label1") as Label;
parentPanel.Text = TextBox1.Text;
}
Here i am updating parent page's Label control value with child's textbox latest value which enter by user.
Hope it will help you. if i am going wrong then please let me know i will try to update it.
You can access a dive from codebehind but you cannot manipulate it's inner html.
If you want to ad a div and it's inner html from codebehid,
Create a user control and add a literal control
<asp:Literal Text="" ID="literal" runat="server" />
Add it to the aspx page
From the code behind of the aspx page,
Literal litereal = MyUserControl1.FindControl("literal") as Literal;
litereal.Text = string.Format("<div id=\"test\" runat=\"server\">{0}</div>", "any text or html");
I have an Asp.net application in which i'd like to add a calendar extendar. So i use this code:
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Home.aspx.vb" Inherits="Home" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<cc1:ToolkitScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="true"></cc1:ToolkitScriptManager>
<span style=" margin-left: 60%">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="error1" Text="error1" ControlToValidate="FirstDate" ForeColor="Red" Font-Bold="true"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="error2" Text="error2" ControlToValidate="FirstDate" ValidationExpression="^[0-3]+[0-9]\/[0-1]+[0-9]\/[0-9]{4}" ForeColor="Red" Font-Bold="true"></asp:RegularExpressionValidator>
<asp:TextBox ID="FirstDate" runat="server" ></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="FirstDate" CssClass="test">
</cc1:CalendarExtender>
</span>
</asp:Content>
The problem is : when i click into the TextBox the calendar isn't shown :
Why this happens?
How can i modify my code to fix this error?
I don't believe that the CalendarExtender behaves the way you're expecting. At least, it doesn't for me. What I usually do is add an ImageButton and make the CalendarExtender's PopupButtonID equal to the ImageButton's ID.
I have a page that works fine with multiple a grid and multiple buttons. The page works fine until I add an asp:UpdatePanel. Then I get the following message pushing any of my buttons:
Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format.
There is no javascript on the page just straight html.
Here is the page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/Site.Master" AutoEventWireup="true"
CodeBehind="TestUpdatePanel.aspx.cs" Inherits="ASCWeb.TestUpdatePanel" %>
<asp:Content ID="mHeadContent" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="mBodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtUser" runat="server" />
<asp:ImageButton ID="btnAdd" runat="server" ImageUrl="~/Images/Add.png" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
If I take the TextBox out, it works fine. Nothing is in the code behind.
What would cause this?
Thanks
As per experience, I only encounter that exception when calling Javascript from codebehind, like when using ScriptManager.RegisterClientScriptBlock() to call window.alert(), for instance. But for this issue, I think this resolves it: http://forums.asp.net/t/1823287.aspx/2/10.
Say I have one full-page form, but within the form, there are two or more events that need to take place on submission: Login & Register
Site.Master:
<%# Master Language="C#" AutoEventWireup="true"
EnableViewState="true" CodeBehind="Site.Master.cs"
Inherits="Site.SiteMasterPage" %>
<!doctype>
<html>
<head runat="server">
<%-- stuff --%>
</head>
<body>
<form ID="MainForm" action="" runat="server">
<asp:Login id="LoginControl" runat="server" />
<asp:CreateUserWizard id="RegisterControl" runat="server" />
</form>
</body>
</html>
If my cursor is focused inside of an input type="text" for asp:Login, and I hit Return (with javascript off), the page submits, but I am not logged in.
The same thing happens when I attempt to register (filling out the createUserWizard and hitting the Return key instead of actually clicking "Register", firing some event)
Is there any non-JavaScript solution for getting the Return key to submit the proper, currently focused portion of the form?
The panel control allows you to define a default button within the scope of it's contents:
<asp:Panel runat="server" DefaultButton="submitButtonA">
<asp:LinkButton ID="submitButtonA" runat="server" Text="Submit A"/>
</asp:Panel>
<asp:Panel runat="server" DefaultButton="submitButtonB">
<asp:LinkButton ID="submitButtonB" runat="server" Text="Submit A"/>
</asp:Panel>
The default button sounds like it might be your friend tonight - http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton.aspx
Actually it might not be, I haven't ever tried it with no Javascript.
I have had this problem for a while now and no matter what i cannot seem to resolve it. I have a master page and a content page.
The content page simply contains a button
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>
The master page simply contains the content place holders. The button1 one event fires normally, however as soon as a form is added to the master page the button stops firing. the master page looks something like this
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
Member Login
<asp:Button ID="btnLogin" runat="server" Text="Login" CssClass="bt_login"
onclick="btnLogin_Click" />
</form>
</div>
</div>
</div>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</form>
Can some one please help, the button works fine without the form in teh master page, however i need a form in the master page
Thanks
Ok after running the exact same project on another machine, everything worked fine. So I'm guessing there is an issue with the way i installed visual studio and asp.net.