The asp.net community is large and creative. One of the more creative aspects is the development of the ajaxtoolkit available from asp.net website. The toolkit is a collection of class objects for the client side that can be accessed from the server side. That’s a pretty remarkable feat. Spectacular as it is, it may not have all the functionality you need. It is still sometimes useful to write your own script.
Or, in my case, merely adapt some of the great public domain script from the Yahoo API. In my earlier example, I used the YUI to create a asynchronous drag & drop component for a web page. This time around, I needed a couple of popups. The ajaxtoolkit already has a component called the modalpopupextender, which is very nice. But not as nice if you want to have more than one on the same page, each with its own ajax components. For some reason, I wasn’t able to get the postback function to work with two popup panels on the page, and I couldn’t show nested ajax components in either one. So, I adapted more of the YUI to fill the need.
In particular, I used both the YUI overlay manager to manage two popups on the page. And, within the popups, I used the YUI tab control as well as the ajaxtoolkit calendar extender and mask. Finally, I used the xmlhttprequest object to post the client side controls to the server. Curiously, the server side controls wouldn’t wouldn’t do a full post with the script objects on the page, so I had to call that through javascript also.
So without more ado, here is the javascript on the aspx page:
-
-
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
-
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/dragdrop/dragdrop-min.js"></script>
-
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/container/container-min.js"></script>
-
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/utilities/utilities.js"></script>
-
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/button/button-min.js"></script>
-
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/connection/connection-min.js"></script>
-
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/element/element-beta-min.js"></script>
-
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/tabview/tabview-min.js"></script>
-
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
-
-
<script type="text/javascript" language="javascript">
-
//Begin Yahoo modalpopup ajax controls - unfortunately, cannot display ajaxtoolbox tools in the popup.
-
YAHOO.namespace("container");
-
function init() {
-
-
-
// Build panel1 based on markup
-
YAHOO.container.panelRecurrance = new YAHOO.widget.Panel("panelRecurrance", { xy:[250,250],
-
visible:false,
-
width:"420px"
-
} );
-
YAHOO.container.panelRecurrance.render();
-
// Build panel2 based on markup
-
YAHOO.container.panelLocation = new YAHOO.widget.Panel("panelLocation", { xy:[350,350],
-
visible:false,
-
width:"400px"
-
} );
-
YAHOO.container.panelLocation.render();
-
YAHOO.container.manager = new YAHOO.widget.OverlayManager();
-
YAHOO.container.manager.register([YAHOO.container.panelRecurrance,
-
YAHOO.container.panelLocation]);
-
-
YAHOO.util.Event.addListener("recurButton", "click", YAHOO.container.panelRecurrance.show, YAHOO.container.panelRecurrance, true);
-
YAHOO.util.Event.addListener("recurButton", "click", YAHOO.container.panelRecurrance.focus, YAHOO.container.panelRecurrance, true);
-
YAHOO.util.Event.addListener("popRecurOk", "click", YAHOO.container.panelRecurrance.hide, YAHOO.container.panelRecurrance, true);
-
YAHOO.util.Event.addListener("popRecurCancel", "click", YAHOO.container.panelRecurrance.hide, YAHOO.container.panelRecurrance, true);
-
//YAHOO.util.Event.addListener("focus1", "click", YAHOO.container.panelRecurrance.focus, YAHOO.container.panelRecurrance, true);
-
-
YAHOO.util.Event.addListener(‘<%= AddLocation.ClientID %>’, "click", YAHOO.container.panelLocation.show, YAHOO.container.panelLocation, true);
-
YAHOO.util.Event.addListener(‘<%= AddLocation.ClientID %>’, "click", YAHOO.container.panelLocation.focus, YAHOO.container.panelLocation, true);
-
//YAHOO.util.Event.addListener("hide2", "click", YAHOO.container.panelLocation.hide, YAHOO.container.panelLocation, true);
-
//YAHOO.util.Event.addListener("focus2", "click", YAHOO.container.panelLocation.focus, YAHOO.container.panelLocation, true);
-
-
}
-
YAHOO.util.Event.onDOMReady(init);
-
-
//Yahoo tabView Section
-
var tabView = new YAHOO.widget.TabView(‘recurTab’);
-
-
</script>
-
<!– ************ End Yahoo! ajax API section ***************–>
-
Below is the javascript for my xmlhttprequest object. The id parameter indicates the control - either popup or the server control to post - that calls the object. Note the use of clientId server tags to get the client markup Id for asp.net server controls. Also note the manner in which I was able to catch the select values for radio buttons, a checkbox list, and a couple of dropdown lists.
-
-
<script language="javascript" type="text/javascript" >
-
-
//////////////////////////////////////////////////////////////////////////////
-
// XMLHttpRequest
-
//////////////////////////////////////////////////////////////////////////////
-
-
var bodyList;
-
var sendStr;
-
var xmlHttp;
-
var requestURL = parent.document.URL;
-
-
function send_data(id)
-
{
-
if (id.length > 0)
-
{
-
//alert(id);
-
//items = document.getElementById(id).getElementsByTagName("li");
-
bodyList="";
-
/*
-
for (i=0;i<items.length;i=i+1) {
-
bodyList += items[i].id + ",";
-
}
-
*/
-
-
-
sendStr = "Input="+id;
-
sendStr += "&StartDate="+document.getElementById(‘<%= _startDate.ClientID %>’).value;
-
sendStr += "&StartTime="+document.getElementById(‘<%= _startTime.ClientID %>’).value;
-
sendStr += "&EndDate="+document.getElementById(‘<%= _endDate.ClientID %>’).value;
-
sendStr += "&EndNumber="+document.getElementById(‘<%= _endNumber.ClientID %>’).value;
-
sendStr += "&DayOption="+document.getElementById(‘<%= _dailyButton.ClientID %>’).checked;
-
sendStr += "&WeekDaysOption="+document.getElementById(‘<%= _dayWeekdays.ClientID %>’).checked;
-
sendStr += "&WeekOption="+document.getElementById(‘<%= _weeklyButton.ClientID %>’).checked;
-
sendStr += "&MonthDayOption="+document.getElementById(‘<%= _monthlyDayButton.ClientID %>’).checked;
-
sendStr += "&MonthWeekOption="+document.getElementById(‘<%= _monthWeekButton.ClientID %>’).checked;
-
sendStr += "&RandomOption="+document.getElementById(‘<%= _randomButton.ClientID %>’).checked;
-
sendStr += "&DayInterval="+document.getElementById(‘<%= _dayInterval.ClientID %>’).value;
-
sendStr += "&WeekInterval="+document.getElementById(‘<%= _weekInterval.ClientID %>’).value;
-
//alert(’<%= _weekDay.ClientID %>’);
-
var containerRef = document.getElementById(‘<%= _weekDay.ClientID %>’);
-
var inputRefArray = containerRef.getElementsByTagName(‘input’);
-
var weekdays="";
-
for (var i=0; i<inputRefArray.length; i++)
-
{
-
var inputRef = inputRefArray[i];
-
-
if ( inputRef.type.substr(0, 8) == ‘checkbox’ )
-
{
-
if ( inputRef.checked == true )
-
weekdays += i+",";
-
}
-
}
-
sendStr +="&WeekDay="+weekdays;
-
sendStr += "&MonthDay="+document.getElementById(‘<%= _monthDay.ClientID %>’).value;
-
sendStr += "&MonthDayInterval="+document.getElementById(‘<%= _monthDayInterval.ClientID %>’).value;
-
sendStr += "&MonthWeek="+document.getElementById(‘<%= _monthWeek.ClientID %>’).selectedIndex;
-
sendStr += "&MonthWeekDay="+document.getElementById(‘<%= _monthWeekDay.ClientID %>’).selectedIndex;
-
sendStr += "&MonthWeekInterval="+document.getElementById(‘<%= _monthWeekInterval.ClientID %>’).value;
-
xmlHttp = GetXmlHttpObject(stateChangeHandler);
-
//Send the xmlHttp get to the specified url
-
-
-
xmlHttp.open("POST",requestURL,false);
-
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-
xmlHttp.setRequestHeader("Content-length", sendStr.length);
-
xmlHttp.setRequestHeader("Post", "true");
-
xmlHttp.send(sendStr);
-
-
if (id == "bNext")
-
{
-
__doPostBack(‘__Page’, ‘MyCustomArgument’);
-
}
-
}
-
}
-
-
//stateChangeHandler will fire when the state
-
//has changed, i.e. data is received back
-
//This is non-blocking (asynchronous)
-
function stateChangeHandler()
-
{
-
//readyState of 4 or ‘complete’ represents that data has been returned
-
if (xmlHttp.readyState == 4 || xmlHttp.readyState == ‘complete’)
-
{
-
//Gather the results from the callback
-
var str = xmlHttp.responseText;
-
-
//Populate the innerHTML of the div with the results
-
//document.getElementById(’ul1′).innerHTML = str;
-
}
-
-
if (xmlHttp.readyState == 1)
-
{
-
//Populate the innerHTML of the div with the results
-
//document.getElementById(’ul1′).innerHTML = ‘LOADING…’;
-
}
-
}
-
-
// XMLHttp send GET request
-
function xmlHttp_Get(xmlhttp, url)
-
{
-
xmlhttp.open(‘GET’, url, true);
-
xmlhttp.send(null);
-
}
-
-
// XMLHttp send POST request
-
function xmlHttp_Post(xmlhttp, url) {
-
-
xmlhttp.open(‘POST’, url, true);
-
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-
xmlhttp.setRequestHeader("Content-length", params.length);
-
xmlhttp.setRequestHeader("Connection", "close");
-
}
-
-
-
function GetXmlHttpObject(handler)
-
{
-
var objXmlHttp;
-
-
try
-
{
-
// Firefox, Opera 8.0+, Safari
-
objXmlHttp=new XMLHttpRequest();
-
objXmlHttp.onload = handler;
-
objXmlHttp.onerror = handler;
-
}
-
catch (e)
-
{
-
// Internet Explorer6+ try
-
try
-
{
-
objXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
-
objXmlHttp.onreadystatechange = handler;
-
}
-
catch (e)
-
{
-
try
-
{
-
objXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
-
objXmlHttp.onreadystatechange = handler;
-
}
-
catch (e)
-
{
-
alert("Your browser does not support AJAX!");
-
return false;
-
}
-
}
-
}
-
return objXmlHttp;
-
}
-
</script>
-
Notice the use of __doPostBack(’__Page’, ‘MyCustomArgument’) to force a full postback to the server. You can study the Yahoo examples for the html script. Finally, the code for the server-side page_load
-
-
this.bNext.Attributes.Add("onClick", "JavaScript:send_data(’bNext’);");
-
if (Request.Headers["Post"] != null)
-
{
-
string panel = string.Empty;
-
panel = Request.Form["Input"];
-
if (panel == "panelRecurrance")
-
{
-
StartDate = Request.Form["StartDate"];
-
StartTime = Request.Form["StartTime"];
-
EndDate = Request.Form["EndDate"];
-
EndNumber = Request.Form["EndNumber"];
-
DayOption = bool.Parse(Request.Form["DayOption"]);
-
WeekDaysOption = bool.Parse(Request.Form["WeekDaysOption"]);
-
WeekOption = bool.Parse(Request.Form["WeekOption"]);
-
MonthDayOption = bool.Parse(Request.Form["MonthDayOption"]);
-
MonthWeekOption = bool.Parse(Request.Form["MonthWeekOption"]);
-
RandomOption = bool.Parse(Request.Form["RandomOption"]);
-
DayInterval = Request.Form["DayInterval"];
-
WeekInterval = Request.Form["WeekInterval"];
-
WeekDay = Request.Form["WeekDay"];
-
MonthDay = Request.Form["MonthDay"];
-
MonthDayInterval = Request.Form["MonthDayInterval"];
-
MonthWeek = Request.Form["MonthWeek"];
-
MonthWeekDay = Request.Form["MonthWeekDay"];
-
MonthWeekInterval = Request.Form["MonthWeekInterval"];
-
_presenter.UpdateSchedule();
-
}
-
else if (panel == "panelLocation")
-
{
-
CommandEventArgs args = new CommandEventArgs("Post", "");
-
CommandButton_Click(this, args);
-
}
-
else
-
{
-
string returnPath = Request.Url.AbsolutePath + Request.Url.Query;
-
HttpContext.Current.Session["CurrentView"] = Views.GridView;
-
//HttpContext.Current.Response.Redirect(returnPath);
-
}
-
}
-
MultiView1.ActiveViewIndex = (int)HttpContext.Current.Session["CurrentView"];
-
Post a Comment