Page Method not responsing in asp.net - asp.net

i am calling two server side function with the help of page method but nothing happen.
here i am giving my code. so please tell me what mistake i made,
my aspx code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
Test
<br />
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnSetvwState" runat="server" Text="Set Session Val" OnClientClick="SetSessionVal;return false;" />
<asp:Button ID="btnGetVwState" runat="server" Text="Get Session Val" OnClientClick="GetSessionVal;return false;"/>
<script language="javascript">
function preview() {
alert($get('TextBox1').value);
PageMethods.GetMessage("Hi", "Joy", onSuccess)
return false;
}
function onSuccess(res) {
alert(res);
}
function GetSessionVal() {
PageMethods.GetViewState(onSuccess)
return false;
}
function SetSessionVal() {
alert("pp");
PageMethods.SetViewState("Hi", "Tridip", onSuccess)
return false;
}
</script>
</form>
</body>
</html>
my server side code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string GetMessage(string pr1,string pr2)
{
return pr1 + " " + pr2;
}
[System.Web.Services.WebMethod(EnableSession=true)]
public static void SetViewState()
{
HttpContext.Current.Session["data"] = "Hellp";
}
[System.Web.Services.WebMethod(EnableSession = true)]
public static string GetViewState()
{
return (String) HttpContext.Current.Session["data"] ;
}
}
}
please help me to catch the mistake. thanks

missing () here (SetSessionVal, GetSessionVal):
<asp:Button ID="btnSetvwState"
runat="server" Text="Set Session Val"
OnClientClick="SetSessionVal();return false;" />
<asp:Button ID="btnGetVwState"
runat="server" Text="Get Session Val"
OnClientClick="GetSessionVal();return false;"/>

Related

My ASP.NET login page showing 'TextBox' must be placed inside a form tag with runat=server

Here is the following complete code
1) is the master page aspx
2) is the webform page aspx
3) is the webform C# file
The complete details as i suppose
now this ASP:content is already a form and also when i am adding form tag it is showing the page can have only form tage .Please can anyone help ?
Master page
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="eliblogin" runat="server" Text="Login"></asp:Label>
</form>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
masterpage.cs file
using System;
using System.Collections.Generic;
using System.Linq;
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)
{
Page.LoadComplete += new EventHandler(set_button);
}
protected void eliblogout_Click(object sender, EventArgs e)
{
var user = Context.User.Identity;
//authentication manager
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
//logout
authenticationManager.SignOut();
Response.Redirect("~/Index.aspx");
}
private void set_button(Object sender,EventArgs e)
{
var user = Context.User.Identity;
if (user.Name!=null && user.IsAuthenticated)
{
try
{
//Username.Text = user.Name;
eliblogin.Visible = true;
eliblogin.Text = "yes man you did it";
// eliblogout.Visible = true;
}
catch (Exception ex)
{
//Username.Text = ex.ToString();
}
}
else
{
// Username.Text = "Hello Guest";
eliblogin.Visible = true;
// eliblogout.Visible = false;
}
}
}
Login.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Pages_Account_Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="liStatus" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="username"></asp:Label><br />
<br />
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Password"></asp:Label><br />
<br />
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"> </asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" />
</asp:Content>
login.aspx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
public partial class Pages_Account_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
//User Store
UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
userStore.Context.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["elibraryConnectionString"].ConnectionString;
//User Manager
UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
//Create a new User
var user = manager.Find(txtUsername.Text,txtPassword.Text);
if (user!=null)
{
try
{
//Create user Object
//Database will be created Automatically
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user,DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties
{
IsPersistent=false
},userIdentity);
}
catch (Exception ex)
{
liStatus.Text = ex.ToString();
}
}
else
{
liStatus.Text = "invalid username or password";
}
}
catch (Exception ex)
{
liStatus.Text = ex.ToString();
}
}
}
You should move
<form id="form1" runat="server">
<asp:Label ID="eliblogin" runat="server" Text="Login"></asp:Label>
</form>
from master page to login.aspx page and place all asp content of login page into this form tag.

Can a Templated User Control be used for data entry?

I've found code examples that demonstrate templated user controls. They work fine for displaying data, but I can't get them to work for entering data. I have a TextBox in my template, and can change the value in it, but I can't get that changed value in my code. I attempt to get the user control's values in a button click handler, but I always get just the initial value. Also, when I click the button, the templated user control's value(s) are reset. Is data entry supposed to work with templated user controls?
My code is similar to the MSDN topic "How to: Create Templated ASP.NET User Controls".
Default.aspx ===============================
<%# Page Language="C#" %>
<%# Register TagPrefix="uc" tagname="TemplateTest" Src="TemplatedUC.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">
<script runat="server">
protected void Page_Load()
{
DataBind();
}
protected void ButtonShowContent_Click(object sender, EventArgs e)
{
ContentLabel.Text = string.Format("Index: {0} - Message: {1}<br>", TemplateTest1.Index, TemplateTest1.Message);
}
</script>
<head runat="server">
<title>Templated User Control Test</title>
</head>
<body>
<h1>Testing Templated User Control</h1>
<form id="Form2" runat="server">
<uc:TemplateTest ID="TemplateTest1" runat="server">
<MessageTemplate>
Index: <asp:Label runat="server" ID="Label1" Text='<%# Container.Index %>' /><br />
Message: <asp:TextBox runat="server" ID="Label2" Text='<%# Container.Message %>' /><br />
</MessageTemplate>
</uc:TemplateTest>
<asp:Button ID="ButtonShowContent" runat="server" Text="Show Content" onclick="ButtonShowContent_Click" />
<asp:Label ID="ContentLabel" runat="server" Text=" " Width="100%" />
</form>
</body>
</html>
TemplatedUC.ascx ============================
<%# Control Language="C#" ClassName="TemplatedUC" %>
<%# Import Namespace="System.ComponentModel" %>
<script runat="server">
private ITemplate messageTemplate = null;
private MessageContainer container = new MessageContainer(1, "Initial Text");
[ TemplateContainer(typeof(MessageContainer)) ]
[ PersistenceMode(PersistenceMode.InnerProperty) ]
public ITemplate MessageTemplate {
get
{
return messageTemplate;
}
set
{
messageTemplate = value;
}
}
public int Index
{
get { return container.Index; }
set { container.Index = value; }
}
public string Message
{
get { return container.Message; }
set { container.Message = value; }
}
void Page_Init() {
if (messageTemplate != null) {
messageTemplate.InstantiateIn(container);
PlaceHolder1.Controls.Add(container);
}
}
public class MessageContainer: Control, INamingContainer {
private int m_index;
private String m_message;
internal MessageContainer(int index, String message)
{
m_index = index;
m_message = message;
}
public int Index {
get { return m_index; }
set { m_index = value; }
}
public String Message
{
get { return m_message; }
set { m_message = value; }
}
}
</script>
<asp:Placeholder runat="server" ID="PlaceHolder1" />
If you run this and change the value for 'Message', then when you click the button, the data will be displayed. Thank you.

Access a content page variable through the master page

I know in ASP.net, we can access a master page variable through content page, but is there anyway we can access a content page variable through the master page?
Yes you can. You have to implement a base class and content class should be derived from that base class.
EDIT : Posted markup and changed code for a clearer example
I have created a base page inherited the System.Web.UI.Page, then made the content page inherit it. My basepage:
namespace WebApplication2
{
public class BasePage : System.Web.UI.Page
{
public BasePage() { }
public virtual string TextValue()
{
return "";
}
}
}
Here's my content page markup:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<asp:Label ID="lblContentText" Text="Contentpage TextValue:" runat="server"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:Content>
Content page code:
namespace WebApplication2
{
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override string TextValue()
{
return TextBox1.Text;
}
}
}
My Masterpage markup:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication2.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<title><%: Page.Title %> - My ASP.NET Application</title>
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body>
<form runat="server">
<header> </header>
<div id="body">
<asp:Label ID="lblText" runat ="server" Text="Masterpage Text :" />
<asp:TextBox ID="txtMaster" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Click to read content page TextValue " OnClick="Button1_Click" />
<asp:ContentPlaceHolder runat="server" ID="MainContent" />
</div>
<footer>
</footer>
</form>
</body>
</html>
And the implementation in masterpage code behind:
namespace WebApplication2
{
public partial class SiteMaster : MasterPage
{
BasePage Currentpage = null;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Currentpage = this.Page as BasePage;
if (Currentpage != null)
{
txtMaster.Text = Currentpage.TextValue();
}
}
}
}
If you see any error like BasePase is not recognized, Please make sure that either it is using same namespace(i.e. WebApplication2), or the namespace is added to the implementation page (i.e. using WebApplication2;).
Hope it helps!

Append empty text box Rows to a grid

I have a grid with two columns first column with check box and second column with text box. I have a add and save button down the grid.Can you please tell how to obtain if i click add button i need to append one more row to the grid with empty text box and check box so that i can type and click save
need to do possibly without java script
If I am getting you correctly you want something like below
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdDemo" runat="server" AutoGenerateColumns="False" EnableModelValidation="True">
<Columns>
<asp:TemplateField HeaderText="CheckBox">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("IsCheckBox") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TextBox">
<ItemTemplate>
<asp:TextBox ID="Label1" runat="server" Text='<%# Bind("IsTextBox") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Caching;
public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grdDemo.DataSource = new Demo().GetData();
grdDemo.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
var list = new Demo().GetData();
list.Add(new Demo() {IsCheckBox = false, IsTextBox = ""});
Cache["list"] = list;
grdDemo.DataSource = list;
grdDemo.DataBind();
}
}
public class Demo
{
public bool IsCheckBox { get; set; }
public string IsTextBox { get; set; }
public List<Demo> GetData()
{
if (HttpContext.Current.Cache["list"] == null)
{
List<Demo> list = new List<Demo>()
{
new Demo(){IsCheckBox=true,IsTextBox = "text1"},
new Demo(){IsCheckBox=false,IsTextBox = "text2"},
};
return list;
}
return (List<Demo>)HttpContext.Current.Cache["list"];
}
}

asp.net TemplateField.ItemTemplate

TemplateField tf1 = new TemplateField();
tf1.ItemTemplate = ???
How to initialize this property?
If I need to this programmatically then what to do?
TemplateField tf1 = new TemplateField();
tf1.ItemTemplate = Page.LoadTemplate("ItemTemplate.ascx");
You would usually do this sort of thing in the markup:
<TemplateField ...>
<ItemTemplate>
<asp:TextBox .../>
</ItemTemplate>
</TemplateField>
or do the same thing using the designer.
Example follows. The commented-out markup produces the same thing as the codebehind does:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
SelectCommand="SELECT Person.Contact.FirstName, Person.Contact.LastName
FROM Person.Contact
INNER JOIN HumanResources.Employee ON Person.Contact.ContactID = HumanResources.Employee.ContactID
WHERE (Person.Contact.LastName LIKE N'A%')
ORDER BY Person.Contact.LastName, Person.Contact.FirstName">
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<%-- <ItemTemplate>
<asp:Label runat="server" ID="lblLast">Name: </asp:Label>
<asp:Label runat="server" ID="lblName" Text='<%# DataBinder.Eval(Container.DataItem, "LastName")+", "+DataBinder.Eval(Container.DataItem, "FirstName") %>' />
</ItemTemplate>--%>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
</form>
</body>
</html>
Codebehind:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Repeater1.ItemTemplate = new TheItemTemplate();
}
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
}
public class TheItemTemplate : ITemplate
{
#region Implementation of ITemplate
public void InstantiateIn(Control container)
{
var lblLast = new Label {ID = "lblLast", Text = "Name: "};
container.Controls.Add(lblLast);
var lblName = new Label {ID = "lblName"};
lblName.DataBinding += delegate(object sender, EventArgs e)
{
var theLabel = (Label) sender;
var dataItem = DataBinder.GetDataItem(theLabel.BindingContainer);
theLabel.Text = DataBinder.Eval(dataItem, "LastName") + ", " +
DataBinder.Eval(dataItem, "FirstName");
};
container.Controls.Add(lblName);
}
#endregion
}

Resources