html form post to an aspx page, the header fields are not being posted. Chrome says the request is a GET, and does not show Form Data at all - html

Here is the posting page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Properties login</title>
</head>
<body>
<form id="formstart" method="POST" action="proplist.aspx">
<table id="loginTable" class="center">
<tr>
<td><label >User name</label></td>
<td><input type="text" name="usertext" text="asm"/></td>
</tr>
<tr>
<td><label ID="passwd" >Password</label></td>
<td><input type="text" name="passtext" text=""/></td>
</tr>
<tr>
<td><label ID="Label2" >Press to continue</label></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Simple html, no asp in case that causes any problem.
Here is the target aspx page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="proplist.aspx.cs" Inherits="PropertyW.proplist" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="formlist" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Comic Sans MS" Font-Size="Larger" ForeColor="#3333CC" Text="Property List"></asp:Label>
<div id="loginResponse" runat="server" >not logged in
</div>
<div id="namePassed" runat="server"></div>
</form>
</body>
</html>
Here is the code behind.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PropertyW
{
public partial class proplist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
NameValueCollection nvc = Request.Form;
string userName = "asm", password = "17kgrovE";
string sName = Request.Form["usertext"];
string sPass = nvc["passtext"];
if (nvc.Count > 0)
{
sName = nvc["usertext"] + ".nvc";
}
string sMsg = string.Format("name passed = {0}, nvc count = {1}", sName, nvc.Count);
namePassed.InnerHtml = sMsg;
if (!string.IsNullOrEmpty(sName))
{
userName = sName;
}
if (!string.IsNullOrEmpty(sPass))
{
password = sPass;
}
// CheckLogin(userName, password);
}
}
}
}
Here is what chrome shows in the network tab. There is no entry for the Form data.
In the code behind the form fields are null. (usertext and passtext).
Chrome shows the request as a GET even though the code is a POST.
Here is what chrome shows in the network tab.

Related

Java Servlet and JSP cannot view arraylist from session initially

I'm a complete beginner to JSPs and servlets and I would appreciate it if you could help me on this small problem. I have an index.html file that has links to two JSP files: addItem.jsp (user can input item into their todDo list, and submit button links to servlet), and toDoList.jsp (shows the toDO list and redirects via a button to addItem.jsp)
The problem I am having is that when I view the toDoList.jsp before adding an item to the list, the list does not appear. However, I add an item on addItem.jsp, the list shows from the servlet, and now I can view the full list, including the item that was added on toDoList.jsp.
How can I change my code so that I don't have to add something to the list to view it?
toDoList.jsp :
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="javax.servlet.http.HttpSession" %>
<%#page import="java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>My ToDo List:</h1>
<% ArrayList<String> toDoList = new ArrayList<String>();%>
<% if(session.getAttribute("toDoList") != null) { // If the list exists in the session %>
<% toDoList = (ArrayList<String>) session.getAttribute("toDoList");%>
<% for(String toDoItem:toDoList) { %>
<% out.println(toDoItem); %><br>
<% } %>
<% } else {%>
<h3>Your list is empty!</h3>
<% } %>
<form action="addItem.jsp" method="post">
<fieldset>
<input type="submit" value="Add Item">
</fieldset>
</form>
</body>
addItem.jsp :
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Servlet1" method="post">
<fieldset>
<label>New ToDo Item:</label>
<input type="text" name="item"><br>
<input type="submit" value="Submit">
</fieldset>
</form>
</body>
The main Servlet1 code:
#WebServlet(urlPatterns = {"/Servlet1"})
public class Servlet1 extends HttpServlet {
ArrayList<String> toDoList = new ArrayList<String>();
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String item = request.getParameter("item");
toDoList.add(item);
HttpSession session = request.getSession();
boolean is_toDoList_Initilized = false;
if(session.getAttribute("toDoList") == null) {
is_toDoList_Initilized = false;
session.setAttribute("toDoList", toDoList);
} else is_toDoList_Initilized = true;
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Servlet1</title>");
out.println("</head>");
out.println("<body>");
for(String toDoItem:toDoList) {
out.println(toDoItem + "<br>");
}
out.println(is_toDoList_Initilized);
out.println("</body>");
out.println("</html>");
}
}
I think you just need to call that servlet before redirecting your toDoList.jsp from your index page.
Flow will be like
flow 1: index.jsp > servlet1 > toDoList
flow 2: index.jsp > addItem.jsp > servlet1 > toDoList

how to print URL into jsp page while URL has hindi text

i want to print URL into next JSP page while that has hindi language text content as follow :
but URL printed as : https://hi.wikipedia.org/wiki/%E0%A4%B5%E0%A4%BE%E0%A4%B0%E0%A4%BE%E0%A4%A3%E0%A4%B8%E0%A5%80
html code of index.html
<script type="text/javascript">
function GetDynamicTextBox(value){
return '<Label> Enter the URL : </label>' +
'<input name = "habits" type="text" class="urls" value = "' + value + '" />' +
' <input type="button" value="Remove" onclick = "RemoveTextBox(this)" /><br><br>'
}
function AddTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicTextBox("");
document.getElementById("TextBoxContainer").appendChild(div);
}
function RemoveTextBox(div) {
document.getElementById("TextBoxContainer").removeChild(div.parentNode);
}
function RecreateDynamicTextboxes() {
var values = eval('<%=Values%>');
if (values != null) {
var html = "";
for (var i = 0; i < values.length; i++) {
html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";
}
document.getElementById("TextBoxContainer").innerHTML = html;
}
}
window.onload = RecreateDynamicTextboxes;
</script>
<html>
<head>
<title>T-SUMM</title>
</head>
<body>
<center>
<form method="Post" action="./result.jsp">
<table>
<br> <br> <br><br>
<Label> Enter the URL : </label>
<input name='habits' class="urls" id='t2'>
<input id="btnAdd" type="button" value="add another URL" onclick="AddTextBox()" /><br><br>
<div id="TextBoxContainer">
<!--Textboxes will be added here -->
</div>
<input type="submit" name="submit" >
</table>
</form>
<br><br>
</center>
</body>
</html>
JSP page as result.jsp
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>T-SUMM</title>
</head>
<body>
<%
String[] values=request.getParameterValues("habits");
for(int i=0;i<(values.length);i++)
{
out.println("<br><br> "+values[i]);out.println();
}
%>
</body>
</html>
You can simply achieve this from JavaScript.
var originalURL = "https://hi.wikipedia.org/wiki/%E0%A4%B5%E0%A4%BE%E0%A4%B0%E0%A4%BE%E0%A4%A3%E0%A4%B8%E0%A5%80";
console.log(decodeURIComponent(originalURL));
If you need to decode the URL using Java then following code will do but it won't work in your case.
try {
System.out.println(java.net.URLDecoder.decode(URL_TO_DECODE, "UTF-8"));
} catch (Exception e) {
System.out.println(e);
}
Output i'm getting is https://hi.wikipedia.org/wiki/???????
See this answer.
You are nearly there. EncodeURIComponent correctly encodes to UTF-8,
which is what you should always use in a URL today.
The problem is that the submitted query string is getting mutilated on
the way into your server-side script, because getParameter() uses
ISO-8559-1 instead of UTF-8. This stems from Ancient Times before the
web settled on UTF-8 for URI/IRI, but it's rather pathetic that the
Servlet spec hasn't been updated to match reality, or at least provide
a reliable, supported option for it.
(There is request.setCharacterEncoding in Servlet 2.3, but it doesn't
affect query string parsing, and if a single parameter has been read
before, possibly by some other framework element, it won't work at
all.)
So you need to futz around with container-specific methods to get
proper UTF-8, often involving stuff in server.xml. This totally sucks
for distributing web apps that should work anywhere. For Tomcat see
http://wiki.apache.org/tomcat/FAQ/CharacterEncoding
Update 1: Very simple JavaScript function to do your requirement. No need to involve java.
function decodeURL() {
document.getElementById("result").innerHTML = decodeURIComponent(document.getElementById("t1").value);
}
<!-- URL Decoder -->
<center>
Enter a URL : <input name='habits' id='t1'>
<br><br>
<input type="button" value="Decode" onclick="decodeURL()" />
<br><br>
<div id="result">
result will show here..
</div>
</center>
If you want to use above function in complete, get the code below.
<html>
<head>
<title>T-SUMM</title>
</head>
<body>
<!-- URL Decoder -->
<center>
Enter a URL : <input name='habits' id='t1'>
<br><br>
<input type="button" value="Decode" onclick="decodeURL()" />
<br><br>
<div id="result">
result will show here..
</div>
</center>
<script type="text/javascript">
function decodeURL() {
document.getElementById("result").innerHTML = decodeURIComponent(document.getElementById("t1").value);
}
</script>
</body>
</html>
Update 2
function GetDynamicTextBox(value) {
return '<Label> Enter the URL : </label>' +
'<input name = "habits" type="text" class="urls" value = "' + value + '" />' +
' <input type="button" value="Remove" onclick = "RemoveTextBox(this)" /><br><br>'
}
function AddTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicTextBox("");
document.getElementById("TextBoxContainer").appendChild(div);
}
function RemoveTextBox(div) {
document.getElementById("TextBoxContainer").removeChild(div.parentNode);
}
function RecreateDynamicTextboxes() {
var values = eval('');
if (values != null) {
var html = "";
for (var i = 0; i < values.length; i++) {
html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";
}
document.getElementById("TextBoxContainer").innerHTML = html;
}
}
window.onload = RecreateDynamicTextboxes;
function decodeURL() {
var textFields = document.getElementsByName('habits');
document.getElementById("result").innerHTML = "";
for (var x = 0; x < textFields.length; x++) {
document.getElementById("result").innerHTML += "<p>" + decodeURIComponent(textFields[x].value) + "</p>";
}
}
<center>
<form method="Post" action="./result.jsp">
<table>
<br> <br> <br><br>
<Label> Enter the URL : </label>
<input name='habits' class="urls" id='t2'>
<input id="btnAdd" type="button" value="add another URL" onclick="AddTextBox()" /><br><br>
<div id="TextBoxContainer">
<!--Textboxes will be added here -->
</div>
<input type="button" value="Decode" onclick="decodeURL()" />
</table>
</form>
<br>
<div id="result">
result will show here..
</div>
result.jsp page can be edited as follow :
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>T-SUMM</title>
</head>
<body>
<%# page import="java.net.URLDecoder"%>
<% request.setCharacterEncoding("UTF-8");
String[] values=request.getParameterValues("habits");
for(int i=0;i<(values.length);i++)
{
String decoded = URLDecoder.decode(values[i]);
out.println("<br><br> "+decoded);out.println();
}
%>
</body>
</html>

HTML onload script triggered too many times?

I created a simple ASP.Net html page. I would like to put a password check, ONLY when the page is initially loaded, I did it with a script and assigned the tag onload to the body. The problem is, that the password check is triggered every time I press a button. Why does this happens? How can I execute that password check ONLY when you open the page?
Thanks in advance.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="FotoDiClasse.aspx.cs" Inherits="FotoDiClasse" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Choose your sweatshirt</title>
<!-- password control -->
<script>
var password;
var pass1 = "1234";
var firstTime = true;
function checkPassword()
{
if (firstTime)
firstTime = false;
password = prompt("Enter password to access the site", '');
if (password != pass1) window.location = "http://www.google.com";
}
</script>
</head>
<body onload="checkPassword()">
<form id="form1" runat="server">
<div>
<asp:Button ID="CreateButton" runat="server" Text="Create" Width="240px" OnClick="CreateButton_Click" />
<asp:Button ID="SendButton" runat="server" Text="Send" Width="240px" OnClick="SendButton_Click" />
</div>
</form>
</body>
</html>
This "first time" flag doesn't work
It is called PostBack. Every time a Button is pressed the Page performs a Form Post (PostBack). That means the page is reloaded.
With the snippet below you can call a JavaScript function only when the page is first loaded.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "runScript", "alert('This is not a PostBack');", true);
}
}
It happens because every time you press a button your page is loaded and onload() method is called and hence every time that method is called.
To overcome this problem you need to set some sort of function that check whether your page is loaded first time or not to do this you can use cookie to store that session.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="FotoDiClasse.aspx.cs" Inherits="FotoDiClasse" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Choose your sweatshirt</title>
<!-- password control -->
<script>
var password;
var pass1 = "1234";
var firstTime = true;
function checkPassword()
{
if (firstTime)
firstTime = false;
password = prompt("Enter password to access the site", '');
if (password != pass1) window.location = "http://www.google.com";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="CreateButton" runat="server" Text="Create" Width="240px" OnClick="CreateButton_Click" />
<asp:Button ID="SendButton" runat="server" Text="Send" Width="240px" OnClick="SendButton_Click" onblur="checkPassword()"/>
</div>
</form>
</body>
</html>

Read data from a text box in asp.net and display it in another web page

I am new to ASP.net . I am trying to work with basic web forms.
I created code to take first name and last name from user. When user clicks on submit, I need the data to be displayed on another page. Can someone make the code for me that should be written on the output web page?
My Code is :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>My First web page</title>
</head>
<body>
<form id="form1" runat="server" action="WebFormOutput.aspx" >
<div style="position:absolute">
<input type="text" name="txtFirstName" placeholder="Enter Your First Name" />
<input type="text" name="txtLastName" placeholder="Enter Your Last Name"/>
<input id="Submit1" type="submit" value="submit" />
</div>
</form>
</body>
</html>
Thank You
consider the following example...
1.Create a web page named default.aspx(say)like following,
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="HP_Master_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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="txtFN" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label>
<asp:TextBox ID="txtLN" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
and in Default.aspx.cs,like following
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class HP_Master_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["FN"] = txtFN.Text;
Session["LN"] = txtLN.Text;
Response.Redirect("Default2.aspx");
}
}
what i have done is
1.Created a page,and added server controls from tool bar,Label1,Label2,txtFN,txtLN, and a Button
2.Added onclick event to button,and in code behind created two sessions to carry the values entered in the two text boxes,Session["FN"],and Session["LN"],then the page is redirected to another page "Default2.aspx" on button click
3.Default two cantains two server controls LblFN,andlblLN that contain no values initially
4.When the page is loaded as soon as the button in previous page is clicked,in the current pages page load event,the text property of labels lblFN,and lblLN are set with the values stored in the Session variables.the code for this is as per following
default2.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="HP_Master_Default2" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblFN" runat="server" Text=""></asp:Label>
<asp:Label ID="lblLN" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
default2.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class HP_Master_Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblFN.Text = Session["FN"].ToString();
lblLN.Text = Session["LN"].ToString();
}
}

problem with jqmodal inside an update panel

I have a control that I need to display as a page or as a modal dialog.
In the page_load event I look to see if the modal property is set and if so, I register a script to invoke jqmodal. Here is the code:
protected void Page_Load(object sender, EventArgs e)
{
if (this.Modal) // Show as a modal dialog
{
selector.Attributes.Add("class", "jqmWindow");
selector.Attributes.Add("style", "width:1100px;height:600px;");
string script = "<script type=\"text/javascript\">$().ready(function() { $(" + "'#" + selector.ClientID + "').jqm({ modal: true }).jqmShow();});</script>";
//script = "<script type=\"text/javascript\">confirm('hello');</script>";
ScriptManager.RegisterStartupScript(this,this.GetType(),"duh",script,false);
}
}
This control is used on a page that has an update panel.
This all works well in Firefox and IE for the INITAL page load and any refreshes. However when I postback I get problems in IE and FF:
In IE, the div that represents the modal (in this case, selector) is shifted down and to the right about 500 px.
In firefox, the darkened area around the div becomes progressively darker with each postback.
If I remove the update panel from the host page (it's actually in the master page) this code works.
I have tried not executing the above code on postback but that simply disables jqmodal. I'm really stumped If anyone can help with this I would appreciate it.
The problem is that on PostBack, the jQuery div is moved down and to the right. The div after page load looks like this (renders correctly):
DIV class="jqmWindow jqmID1" id=selector style="DISPLAY: block; Z-INDEX: 3000; WIDTH: 1100px; HEIGHT: 600px" _jqm="1" jQuery1238701349279="3">
After Async PostBack it looks like this (renders incorrectly):
DIV class="jqmWindow jqmID2" id=selector style="DISPLAY: block; Z-INDEX: 3000; WIDTH: 1100px; TOP: 146px; HEIGHT: 600px" _jqm="2" jQuery1238701490978="5">
Removing the update panel solves this problem......I don't know that it is the problem.
I created a project with some pages with the just relevent code. The pages are site.master, List.aspx/cs and PartSelector.ascx/cs
// site.master - nothing in codebehind
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>
<!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 src="http://localhost/Scripts/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="http://localhost/Scripts/jqueryUI/ui/ui.core.js" type="text/javascript"></script>
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" > </asp:ScriptManager>
<div>
<asp:updatepanel id="upmaincontent" runat="server" updatemode="conditional">
<contenttemplate>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
// list.aspx
<%# Page Language="C#" MasterPageFile="~/Site.master" CodeFile="List.aspx.cs" Inherits="List" Title="Parts Master List" %>
<%# Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<%# Register Src="~/Controls/PartSelector.ascx" TagName="PartSelector" TagPrefix="sam" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" />
<center>
<div><center><h3><%= "Part Selector" %></h3></center></div>
<div>
<center>
<sam:PartSelector ID="PartSelector1" runat="server" Modal="true" ActiveOnly="false" />
</center>
</div>
</center>
</asp:Content>
// list.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Linq.Expressions;
public partial class List : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PartSelector1.ActivateSelector("");
}
}
}
// PartSelector.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="PartSelector.ascx.cs" Inherits="PartSelector" %>
<link href="http://localhost/Scripts/jqModal/jqModal.css" rel="stylesheet" type="text/css" />
<script src="http://localhost/Scripts/jqModal/jqModal.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
var IsModal = false; // Initialize a variable to indicate if the page is to be displayed inside a jqModal dialaog
$().ready(function() { displayPage(); }); // Execute dispalyPage when the dom is ready
function displayPage() {
confirm('displaypage');
IsModal = <%= this.Modal ? "true" : "false" %> // Set IsModal based on a property in codebehind
if(IsModal)
{
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(displayPageAsync); // handle async postbacks
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler); // clean up before starting an async postback
$("#selector").addClass("jqmWindow"); // add some css to resize the display to fit the modal dialog
$("#selector").css({width:"1100px", height: "600px"});
$("#selector").jqm({ modal: true, onHide: hidejqm }).jqmShow();
}
}
function displayPageAsync(sender, args)
{
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm.get_isInAsyncPostBack() ) { // Prevent displayPage from being called twice on the initial page load
confirm('page loaded, async postback.');
displayPage();
}
}
function beginRequestHandler(sender, args) {
confirm('begin async postback');
$("#selector").jqmHide(); // Hide a dialog from last postback
}
function hidejqm(hash) {
confirm('hidejqm');
hash.w.fadeOut('2000', function() { hash.o.remove(); });
}
</script>
<div id="selector">
<center>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Click here to postback" OnClick="Postback_Click"></asp:LinkButton><br /><br />
<asp:LinkButton ID="CancelButton" runat="server" Text="Cancel" OnClick="CancelButton_Click" CssClass="CommandButton"></asp:LinkButton>
</center>
</div>
// PartSelector.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
public partial class PartSelector : System.Web.UI.UserControl
{
public bool Modal { get; set; }
public void ActivateSelector(string searchString)
{
this.Visible = true;
}
protected void CancelButton_Click(object sender, EventArgs e)
{
this.Visible = false;
}
protected void Postback_Click(object sender, EventArgs e)
{
int x = 1;
}
}