masterpage with table layout: how to do it? - asp.net

Update: I found the cause of this error, I cannot create table layout in masterpage why ?
Error 1 The name 'txtUsername' does not exist in the current context
source code:
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master" CodeFile="Login.aspx.cs" Inherits="login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<form id="form1" runat="server">
<div>
Username: <asp:TextBox ID="TextBox1" runat="server" /><br>
Password:<asp:TextBox ID="TextBox2" runat="server" /><br>
<asp:Button ID="Button2" runat="server" onclick="Button1_Click" Text="Login" /><br>
<asp:Label ID="Label1" runat="server" Text="Please login" />
</div>
</form>
</asp:Content>
Update: supplement source code
this is the code behind
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FormsAuthentication.Authenticate(txtUsername.Text, txtPassword.Text))
{
lblStatus.Text = ("Welcome " + txtUsername.Text);
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);
}
else
{
lblStatus.Text = "Invalid login!";
}
}
}
this is the masterpage:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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></title>
<style>
body {
margin:0;
padding:0;
}
</style>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<asp:Table ID="Table1" width="100%" runat="server">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell ID="TableCell1" runat="server" BackColor="Blue" ColumnSpan="3">Header</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow2" runat="server">
<asp:TableCell ID="TableCell2" runat="server" Width="160">
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</asp:TableCell>
<asp:TableCell ID="TableCell3" runat="server" Width="800" BackColor="Red" >col2</asp:TableCell>
<asp:TableCell ID="TableCell4" runat="server" Width="160" ColumnSpan="3">col3</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow3" runat="server">
<asp:TableCell ID="TableCell5" runat="server">Footer</asp:TableCell>
</asp:TableRow>
</asp:Table>
</body>
</html>
code behind
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}

Related

How to submit data into 2 table in database at same time in asp.net?

I already successfully insert data to [Survey] table.
But I also want to append data(amount) into [Vote] table at the same time which will increase the data(amount) +1 to the selected Pizza the user select when they click submit.
Last if can complete this step, I will need to build a pie chart with Chart.js retrieve from database which i already created but it is set manually in the script by me. Scroll to the end, to check chart.js pie chart.
Currently Vote = 0.
[Vote] table Data
Here is my SQL query (I create a dbml item called "myconnection")
CREATE TABLE [dbo].[Survey]
(
[SurveyId] INT IDENTITY (1, 1) NOT NULL,
[MemberName] VARCHAR(100) NULL,
[PizzaVote] VARCHAR(100) NULL,
PRIMARY KEY CLUSTERED ([SurveyId] ASC)
);
CREATE TABLE [dbo].[Vote]
(
[VoteId] INT IDENTITY (1, 1) NOT NULL,
[PizzaName] VARCHAR(100) NULL,
[amount] INT NULL,
PRIMARY KEY CLUSTERED ([VoteId] ASC)
);
Here is my Survey.aspx markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Survey.aspx.cs" Inherits="MYDemo.Survey" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td class="auto-style1" colspan="2">
<h1>Survey</h1>
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td class="auto-style2">Name</td>
<td>
<asp:TextBox ID="txtSurName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSurName" ErrorMessage="Please enter your name!"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">Preferred Pizza?</td>
<td>
<asp:DropDownList ID="ddlPrePizza" runat="server">
<asp:ListItem Value="0">Select One</asp:ListItem>
<asp:ListItem Value="1">Island Supreme</asp:ListItem>
<asp:ListItem Value="2">Hawaiian Supreme</asp:ListItem>
<asp:ListItem Value="3">Blazing Seafood</asp:ListItem>
<asp:ListItem Value="4">Chicken Pepperoni</asp:ListItem>
<asp:ListItem Value="5">Deluxe Cheese</asp:ListItem>
<asp:ListItem Value="6">Island Tuna</asp:ListItem>
<asp:ListItem Value="7">Haiwaiian Chicken</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ddlPrePizza" ErrorMessage="Please select one pizza!" InitialValue="0"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Here is my code behind.(survey.aspx.cs)
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MYDemo
{
public partial class Survey : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String CS = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd2 = new SqlCommand("insert into Survey (MemberName,PizzaVote) values(#MemberName, #PizzaVote)", con);
cmd2.Parameters.AddWithValue("#MemberName", txtSurName.Text);
cmd2.Parameters.AddWithValue("#PizzaVote", ddlPrePizza.SelectedItem.Text);
con.Open();
cmd2.ExecuteNonQuery();
con.Close();
txtSurName.Text = string.Empty;
ddlPrePizza.SelectedIndex = 0;
}
}
}
}
Here is my Pie Chart that I created with Chart.js.
<%--<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ChartJS.aspx.cs" Inherits="LoginAndRegister.ChartJS" %>--%>
<%# Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="ChartJS.aspx.cs" Inherits="LoginAndRegister.ChartJS" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="server">
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%--<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="js/script.js"></script>--%>
<link href="StyleSheet1.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js"></script>
<script src="js/Chart.min.js"></script>
<title></title>
<style type="text/css">
.auto-style1 {
text-decoration: underline;
font-weight: bold;
}
</style>
</head>
<body>
<%--<form id="form1" runat="server">--%>
<div class="wrapper">
<h1>Online Survey on <span class="auto-style1"><strong>10000</strong></span> customer on their Prefer Pizza in Best Pizza</h1>
<br />
<br />
<br />
<br />
<%--<canvas id="myChart" width="300" height="300"></canvas>--%>
<canvas id="myChart"></canvas>
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["ISLAND SUPREME", "HAWAIIAN SUPREME", "BLAZING SEAFOOD", "CHICKEN PEPPERONI", "DELUXE CHEESE", "ISLAND TUNA", "HAWAIIAN CHICKEN"],
datasets: [{
backgroundColor: [
"#2ecc71",
"#3498db",
"#95a5a6",
"#9b59b6",
"#f1c40f",
"#e74c3c",
"#34495e"
],
data: [1000, 1309, 2003, 1117, 1028, 1024, 2007]
}]
}
});
</script>
</div>
<%--</form>--%>
</body>
</html>
</asp:Content>

How to make gridview column datepicker

How can I make my Expense Date column datepicker on clicking Image.
I am using image in that column but when I am clicking that Image it is not showing date picker.
How can I do this ?
Here is my code-
Default.aspx-
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="EditExpenses.aspx.cs" Inherits="EditExpenses" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" type="text/css"/>
<script src="//code.jquery.com/jquery-1.10.2.js" type="text/jscript"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js" type="text/jscript"></script>
<link rel="stylesheet" href="/resources/demos/style.css" type="text/css"/>
<script type="text/javascript">
$(function () {
$("#<%= GridView1.ClientID %>").datepicker();
});
</script>
<style type="text/css">
.hiddencol
{
display:none;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h3>Edit Expenses</h3>
<div align="center">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True"
AutoGenerateEditButton="True" onrowcreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="ExpenseId" HeaderText="Expense Id"
SortExpression="Expense_Id" ApplyFormatInEditMode="False"/>
<asp:BoundField DataField="Expense_Category" HeaderText="Expense Category"
SortExpression="Expense_Category" ApplyFormatInEditMode="True"
/>
<asp:BoundField DataField="Expense_Description"
HeaderText="Expense Description" SortExpression="Expense_Description"
ApplyFormatInEditMode="True" />
<asp:TemplateField HeaderText="Expense Date"
SortExpression="Expense_Date" >
<ItemTemplate>
<asp:Label ID="lblEffDate" runat="server" Text='<%#Bind("Expense_Date","{0:MM/dd/yyyy}") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ClientIDMode="Static" ID="txtEffDate" runat="server" readonly="true" Text='<%#Bind("Expense_Date","{0:MM/dd/yyyy}") %>' />
<img alt="Calendar" id="calender1" src="Image/calender.jpeg" height="10" width="10" style="cursor: pointer;" onclick="javascript: NewCssCal('txtEffDate')" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Amount" HeaderText="Amount"
SortExpression="Amount" ApplyFormatInEditMode="True"
/>
</Columns>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testAzharConnectionString %>"
SelectCommand="SELECT ExpenseId, Expense_Category, Expense_Description, Amount, Expense_Date FROM [CompanyExpenses3]"
UpdateCommand="Update [CompanyExpenses3] SET Expense_Category=#Expense_Category, Expense_Description=#Expense_Description,Expense_Date=#Expense_Date, Amount=#Amount WHERE ExpenseId=#ExpenseId"
OnUpdated="OnDSUpdatedHandler"></asp:SqlDataSource>
</asp:Content>
Default.aspx.cs-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class EditExpenses : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void OnDSUpdatedHandler(Object source, SqlDataSourceStatusEventArgs e)
{
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].CssClass = "hiddencol";
}
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].CssClass = "hiddencol";
}
}
}
Any help'll be appreciated.
Your old code(change it)-
<script type="text/javascript">
$(function () {
$("#<%= GridView1.ClientID %>").datepicker();
});
</script>
Your new code-
<script type="text/javascript">
$(document).ready(function(){
$("#txtEffDate").datepicker();
});

Custom validator not working in asp.net

Why didn't it work?
If the user selects “Programmer” in the drop down list and inputs “12” to the textbox, I want the validation to fire. But nothing’s happening when I click the submit button.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!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">
<script language="JavaScript">
var text = document.getElementById("Textbox2");
function Date(oSrc, args) {
args.IsValid = (args.Value == "Programmer" && text == "12");
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CustomValidator ID="dateValidator" runat="server"
ClientValidationFunction="Date" ControlToValidate="DropDownList1" Display="Dynamic"
ErrorMessage="Sample error!"></asp:CustomValidator>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True">Select a profession</asp:ListItem>
<asp:ListItem>Programmer</asp:ListItem>
<asp:ListItem>Lawyer</asp:ListItem>
<asp:ListItem>Doctor</asp:ListItem>
<asp:ListItem>Artist</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
try below
<script language="JavaScript">
function Date(oSrc, args) {
args.IsValid = (args.Value == "Programmer" && document.getElementById('<%=TextBox2.ClientID%>').value== "12");
}

Update value in modalpop panel inside the updatePanel

Here I try to update the Label value base on click event of Linkbutton which is inside the repeater control. Scenario is When I click on Linkbutton same time Modal popup open and the Label text also change.
Below is my code.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ModalPopupUpdatePanel.aspx.cs"
Inherits="ModalPopupUpdatePanel" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!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>ModalPopups and UpdatePanels</title>
<link rel="stylesheet" href="StyleSheet.css" />
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptMgr1">
</asp:ScriptManager>
<div style="width: 300px; left: 100px">
<div >
<asp:Label ID="Label2" runat="server">Update Panel that contains a ModalPopup and its associated PopupPanel inside it</asp:Label>
<asp:UpdatePanel runat="server" ID="updatePanel2" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<br />
<asp:Repeater runat="server" ID="btnresr" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:LinkButton runat="server" ID="text" OnClick="Button5_Click" Text=' <%# DataBinder.Eval(Container.DataItem, "AgentName")%>' BackColor="Red"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:Button runat="server" ID="button2" Text="Launch Modal Popup2" style="display:none"/>
<ajaxToolkit:ModalPopupExtender runat="server" ID="modalPopupExtender2" TargetControlID="button2"
PopupControlID="modalPanel2" OkControlID="okBtn2" CancelControlID="cancelBtn2"
BackgroundCssClass="modalBackground">
</ajaxToolkit:ModalPopupExtender>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:PTBWConnectionString %>"
SelectCommand="SELECT [AgentName], [AgentID] FROM [AgentMaster]">
</asp:SqlDataSource>
<asp:Panel runat="server" ID="modalPanel2" BackColor="AliceBlue" Style="display: none">
<asp:UpdatePanel runat="server" ID="updatePanel4" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Label runat="server" ID="label5" ></asp:Label>
<asp:Button runat="server" ID="postbackBtn" Text="Click to Cause postback" OnClick="postbackBtn_Click" /><br />
<asp:Button runat="server" ID="cancelBtn2" Text="OK" />
<asp:LinkButton runat="server" ID="okBtn2" Text="Cancel" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</form>
</body>
</html>
and Code behind
using System;
using System.Web.UI.WebControls;
public partial class ModalPopupUpdatePanel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void updateLabel_Click(object sender, EventArgs e)
{
}
protected void postbackBtn_Click(object sender, EventArgs e)
{
label5.Text = "After Postback";
}
protected void Button5_Click(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;
label5.Text = "This is before postback,saroop";
modalPopupExtender2.Show();
}
}
I am block here please help me..Is there any Jquery way then also please here me
That is because the lable5 is not avaiable like that, it is nested inside the RepeaterItem.
Can you try this.
LinkButton l = (LinkButton)sender;
RepeaterItem rpt = (RepeaterItem)l.Parent();
Label label5 = (Label)rpt.FindControl("label5");
label5.Text = "This is before postback,saroop";
modalPopupExtender2.Show();

How to get data from controls in Repeater

I have repeater with item template:
<asp:Repeater ID="queryParametersRepeater" runat="server"
DataSourceID="queryParametersObjectDataSource">
<ItemTemplate>
<tr class="itemTemplate">
<td class="labelTd" style="width: 300px;">
<asp:HiddenField runat="server" Value='<%# Eval("ParameterType") %>' />
Define <%# Eval("ParameterName") %> (type <%# Eval("ParameterType") %>)
</td>
<td class="valueTd">
<asp:TextBox runat="server" Width="300px" Text='<%# Eval("ParameterName") %>' />
<asp:CheckBox runat="server" Width="300px" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
In jQuery I change display property of textBox and chekBox. How can I get data from those controls?
I can't use FindControls() function, because I don't know id of my controls.
You can use ID's and findcontrol to get the values. Here is a working example:
<%# Page Language="C#" %>
<%# Import Namespace="System.Collections.Generic" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind the data source
RepeaterExample.DataSource = new List<KeyValuePair<int, string>>{
new KeyValuePair<int, string>(1, "Test1"),
new KeyValuePair<int, string>(2, "Test2"),
new KeyValuePair<int, string>(3, "Test3")
};
RepeaterExample.DataBind();
}
}
protected void cmdSubmit_Click(object sender, EventArgs e)
{
//read the values and output them
litResults.Text = "";
foreach (RepeaterItem i in RepeaterExample.Items)
{
TextBox txtExample = (TextBox)i.FindControl("txtExample");
if (txtExample != null)
{
litResults.Text += txtExample.Text + "<br />";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="RepeaterExample" runat="server">
<HeaderTemplate>
test</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtExample" runat="server" Text='<%#Eval("Value") %>'></asp:TextBox>
</ItemTemplate>
</asp:Repeater><br />
<asp:Button ID="cmdSubmit" runat="server" Text="Submit" OnClick="cmdSubmit_Click" />
<br />
<asp:Literal ID="litResults" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
Can you add a class to your controls?
If so you could try with $(".givenClass").

Resources