I am making a login page to test encryption methods on xml files but i am stuck right now at this problem.
I have a login with an ID login1 inside my default.aspx like this:
<%# Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="Default" %>
<asp:Content id="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content id="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<h2 style="color: Green">
Login using Xml file in ASP.NET 4, C#</h2>
<asp:Login id="Login1" runat="Server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt" Height="166px" OnAuthenticate="Login1_Authenticate"
Width="264px" FailureText="Username and password did not match!">
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
</asp:Login>
</div>
</asp:Content>
but inside my default.aspx.cs i cant use login1 i get the error: The name 'Login1' does not exist in current context
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username;
string pwd;
string CurrentUser = "";
string CurrentPwd = "";
bool LoginStatus = false;
username = Login1.UserName;
pwd = Login1.Password;
I fixed it by changing the CodeFile="Default.aspx.cs" to CodeBehind="Default.aspx.cs
I am unable to figure out how to store a List of strings as a global variable, add to that list on button click for any unknown number of times, store that list in a session, and then properly access that session data in a separate page. I have gotten close, but have weird things occurring and my efforts to debug are doing even more unusual things.
Here is the first page script with Button1_Click() bound to OnClick of Button1:
<%# Page Language="C#" Debug="true" %>
<%# Import Namespace="System" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Web" %>
<%# Import Namespace="System.Web.UI" %>
<%# Import Namespace="System.Web.UI.WebControls" %>
<%# Import Namespace="System.Drawing" %>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8"/>
<title></title>
<script type="text/javascript">
function validateNums(ele, evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
if (ele.id == 'TextBox1') {
var i = document.getElementById('TextBox1').value;
document.getElementById('TextBox1').value = i.slice(0, -1);
}
else
{
var b = document.getElementById('TextBox2').value;
document.getElementById('TextBox2').value = b.slice(0, -1);
}
}
}
</script>
<script runat="server">
private static int counter = 0;
List<string> reg = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
string[] states = { "AK","AL","AR","AS","AZ","CA","CO","CT","DC","DE","FL","GA","GU","HI","IA","ID",
"IL","IN","KS","KY","LA","MA","MD","ME","MH","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY",
"OH","OK","OR","PA","PR","PW","RI","SC","SD","TN","TX","UT","VA","VI","VT","WA","WI","WV","WY" };
int b = 0;
foreach (string i in states)
{
this.DropDownList1.Items.Add(states[b]);
b++;
}
if (Session["counter"] != null)
{
Session.Clear();
}
else { counter = 0; }
}
protected void Page_Unload(object sender, EventArgs e)
{
Session["counter"] = counter;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Length == 0) Alert_First();
if (TextBox2.Text.Length == 0) Alert_Second();
if (DropDownList1.SelectedValue == "") Alert_State();
if (TextBox4.Text.Length == 0) Alert_Password();
if (TextBox1.Text.Length != 0 && TextBox2.Text.Length != 0 && DropDownList1.SelectedValue != "")
{
if (Password_Validate()) Save_Registery();
else Alert_Password();
}
}
private bool Password_Validate()
{
foreach (char i in TextBox4.Text)
{
if (char.IsNumber(i))
return true;
}
return false;
}
private void Alert_First()
{
TextBox1.BackColor = Color.Red;
TextBox1.Text = "A value is required";
}
private void Alert_Second()
{
TextBox2.BackColor = Color.Red;
TextBox2.Text = "A value is required";
}
private void Alert_State()
{
DropDownList1.BackColor = Color.Red;
}
private void Alert_Password()
{
TextBox4.BackColor = Color.Red;
TextBox4.Text = "A value is required with at least one numeric value";
}
private void Save_Registery()
{
if (Session["regList"] != null)
{
reg.AddRange((List<string>)Session["regList"]);
}
reg.Add(TextBox1.Text);
reg.Add(TextBox2.Text);
reg.Add(DropDownList1.Text);
reg.Add(TextBox4.Text);
Session["regList"] = reg;
TextBox3.Text = counter.ToString();
counter++;
}
protected void Button2_Click(object sender, EventArgs e)
{
int count = 0;
foreach(string i in reg)
{
TextBox3.Text = TextBox3.Text + reg[0].ToString();
count++;
}
}
</script>
</head>
<body style="margin:auto; max-width:300px;">
<form id="form1" runat="server">
<div style="float:left; max-width:200px;" >
<asp:Label ID="Label1" runat="server" Text="First Name: "></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Last Name: "></asp:Label>
<br />
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="State"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="" Text=""></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label4" runat="server" Text="Password: "></asp:Label>
<br />
<asp:TextBox ID="TextBox4" runat="server" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label5" runat="server" Text="All Fields are Required" Visible="false"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
<asp:HiddenField ID="HiddenField1" runat="server" />
</div>
<a href="Results.aspx" style="">
<div>
<p>
Results
</p>
</div>
</a>
</form>
</body>
</html>
And here is the second page:
<%# Page Language="C#" Debug="true" %>
<%# Import Namespace="System" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Web" %>
<%# Import Namespace="System.Data" %>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
int counter = Convert.ToInt16(Session["counter"]);
Repeater1.DataSource = (List<string>)Session["regList"];
Repeater1.DataBind();
}
</script>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= Session["counter"] %>
</div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Container.DataItem %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
This outputs only the LAST set of data that I entered via the previous page. The count I added to help debug tells me that there is only 3 values in my session variable of "regList". My question is why is my data getting overwritten every time the Button1_Click() is fired?
In the pageload event of the first page, you are resetting the seesion list by reg,
Session["regList"] = reg;
which by that time the reg is an empty list, and it becomes an issue
I am developing an calendar user control using Ajax calendar extender.
User control code
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" ClientIDMode="Predictable" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControl" %>
<link rel="stylesheet" href="GridViewCSSThemes/YahooGridView.css" type="text/css" media="screen" />
<div style="position:relative;border:none;">
<asp:TextBox ID="txtDate" MaxLength="10" ToolTip="DD/MM/YYYY" Width="100"
CssClass="tb10" runat="server">
</asp:TextBox>
<asp:ImageButton ImageUrl="~/GridViewCSSThemes/Images/Calendar_scheduleHS.png" ID="imgCalender" runat="Server"
BorderWidth="0" ImageAlign="absmiddle" />
<ajaxControl:CalendarExtender ID="AjaxCalenderCtrl" runat="server" Format="dd/MM/yyyy" PopupPosition ="TopLeft"
TargetControlID="txtDate" CssClass="red" FirstDayOfWeek="Sunday" PopupButtonID="imgCalender">
</ajaxControl:CalendarExtender>
<ajaxControl:TextBoxWatermarkExtender WatermarkCssClass="tb10" ID="txtWaterMarkDate"
runat="server" WatermarkText="DD/MM/YYYY" TargetControlID="txtDate">
</ajaxControl:TextBoxWatermarkExtender>
<ajaxToolkit:MaskedEditExtender ID="MaskedEdit_dt" runat="server"
TargetControlID="txtDate"
Mask="99/99/9999"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
MaskType="Date"
AcceptAMPM="true"
AcceptNegative="Left"
ErrorTooltipEnabled="True" />
<ajaxToolkit:MaskedEditValidator ID="MaskedEditV_dt" runat="server"
ControlExtender="MaskedEdit_dt"
ControlToValidate="txtDate"
EmptyValueMessage="Date is required"
InvalidValueMessage="Date is invalid"
Display="Dynamic"
TooltipMessage="Input a date"
EmptyValueBlurredText="Date is required"
InvalidValueBlurredMessage="Date is invalid"
IsValidEmpty="false"
ValidationGroup="MKE" />
<%--<asp:RegularExpressionValidator ID="regexpvalEndDateEdit" ErrorMessage="!" ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d"
ControlToValidate="txtDate" runat="server"></asp:RegularExpressionValidator>--%>
</div>
Code behind user control
internal string _DValue;
public string DValue
{
get
{
if (_DValue == "")
{
_DValue = txtDate.Text;
}
else
{
txtDate.Text = _DValue;
}
return _DValue;
}
set { _DValue = value; }
}
public string IdClientId
{
get { return this.ClientID; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
I am able to get controls value using server side code.
Now i need to access text-box(txtDate) value and MaskedEditValidator(MaskedEditV_dt) inerHtml from javascript.
How can i do this.
Edit-1
User control in aspx page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="CustomControlTest2.aspx.cs" Inherits="CustomControlTest2" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControl" %>
<%# Register TagPrefix="uc1" TagName="UCCalender" Src="~/WebUserControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>User Control Test</title>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true" />
<div>
<table>
<tr>
<td>
<uc1:UCCalender ID="UCCalStartDate" runat="server" DValue="" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnExe" runat="server" Text="Submit" onclick="btnExe_Click" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
Code behind
protected void btnExe_Click(object sender, EventArgs e)
{
lblMsg.Text = UCCalStartDate.DValue;
}
ASP.NET loads to display the UserControl, it ONLY renders the contents of the UserControl. The Controls in the usercontrol will rendered with the ID as $content_UControlName_Control. You can check this after the rendering the page. You can access the contol using that ID from Javascript like document.getElementById(content_UControlName_Control).
Finally i got answer. Thanks to Nag
On button click
<asp:Button ID="btnExe" runat="server" Text="Submit" OnClientClick="getValue('UCCalStartDate_txtDate','UCCalStartDate_MaskedEditV_dt');" onclick="btnExe_Click" />
Javascript
function getValue(id,msk) {
alert(document.getElementById(id).value);
alert(document.getElementById(msk).innerHTML);
}
Finaly i am able to access calander control's text-box value using java-script
In nested master pages child master pasge code behind not working properly..
if i debug and set a stopper in child master page code behind file its not working..
Following is my code:
here is main master page html code
<%# Master Language="C#" AutoEventWireup="true"
CodeFile="AdminMaster.master.cs"
Inherits="Admin_AdminMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div class="mainouter">
<div class="Contents">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</form>
</body>
</html>
here is the child master page html code
<%# Master Language="C#" MasterPageFile="~/Admin/AdminMaster.master"
AutoEventWireup="false"
CodeFile="AdCommercialDetail.master.cs" Inherits="Admin_AdCommercialDetail" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table style="width: 100%;">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</td>
<td>
fdgsdf
</td>
<td>
dfgfdg
</td>
</tr>
</table>
</asp:Content>
code behind of master page
public partial class Admin_AdminMaster : System.Web.UI.MasterPage {
protected void Page_Load(object sender, EventArgs e)
{
}
}
code behind of child master page
public partial class Admin_AdCommercialDetail :
System.Web.UI.MasterPage {
SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase("ConnectionString");
protected void Page_Load(object sender, System.EventArgs e)
{
this.Label1.Text = "gfjhfhggggggggggggggggg";
Label1.Text = "fgdjgjhghklgfsaddffgghhjjkdsdfghjk";
}
}
Here in your child master place the controls outside the ContentPlaceHolder like,
<%# Master Language="C#" MasterPageFile="~/Admin/AdminMaster.master"
AutoEventWireup="false"
CodeFile="AdCommercialDetail.master.cs" Inherits="Admin_AdCommercialDetail" %>
<table style="width: 100%;">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</td>
<td>
fdgsdf
</td>
<td>
dfgfdg
</td>
</tr>
</table>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
</asp:Content>
What is the correct procedure to bind RSS FEED in Repeater control in ASP.NET? need with Aspx page design code and Aspx.cs code.
A simple google search should find you plenty of options.
Heres one for example:
http://blogs.x2line.com/al/archive/2008/06/21/3469.aspx
<%# Control Language="c#" AutoEventWireup="true" EnableViewState="false" %>
<%# Import namespace="System.Xml" %>
<script runat="server" language="C#">
public string rssUrl = "http://blogs.x2line.com/al/rss.aspx";
private System.Xml.XmlDocument doc;
public override void DataBind()
{
doc = new System.Xml.XmlDocument();
doc.Load(rssUrl);
base.DataBind();
}
public void Page_Load(System.Object s, System.EventArgs e)
{
this.DataBind();
}
</script>
<asp:Repeater
runat="server"
id="rptrRss"
DataSource='< %# doc.SelectNodes("/rss/channel/item[position()<=5]") %>'>
<HeaderTemplate>
<div>
<a href='<%# doc.SelectSingleNode("/rss/channel/image/link").InnerText %>'>
<img
src='<%# doc.SelectSingleNode("/rss/channel/image/url").InnerText %>'
alt='<%# doc.SelectSingleNode("/rss/channel/image/title").InnerText %>' />
</a>
</HeaderTemplate>
<ItemTemplate>
<a
href='< %# (Container.DataItem as XmlNode)["link"].InnerText %>'>
<%# (Container.DataItem as XmlNode)["title"].InnerText %>
(<%# (Container.DataItem as XmlNode).SelectSingleNode("author |
title[not(../author)]").InnerText %>)
< /a>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>