I have HTTP Handler FaqsJson.ashx which is creating a JSON utilizing the StringBuilder.
In this Handler Object I am calling a StoredProceedure and Im passing a single Variable FactTypeID. For now this FactTypeID is hard-coded.
From within this Handler Object I need help to be able to iterate through some other Object or Server Variable
in order to determine the Source Page which called the Object.
Also, I need to be able to call this HttpHandler from the JQuery Functions of several .aspx Pages:
Im not clear how to check from which JQuery Source Page this Handler Object was called.
I would appreciate any help as Im completely unfamiliar with HttpHandler Object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
namespace DaVincisApp1
{
/// <summary>
/// Summary description for FaqsJson
/// </summary>
public class FaqsJson : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Int32 FactTypeID = 6;
string query = "dav_getFactsByFactType " + FactTypeID;
string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand(query, myConnection);
try
{
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();
StringBuilder strFaqs = new StringBuilder();
int start = 0;
strFaqs.Append("[");
while (reader.Read())
{
if (start == 0)
strFaqs.Append("{");
else
strFaqs.Append(",{");
strFaqs.Append(string.Format("\"Question\":\"{0}\",", reader[0].ToString()));
strFaqs.Append(string.Format("\"Answer\":\"{0}\"", reader[1].ToString()));
strFaqs.Append("}");
start++;
}
strFaqs.Append("]");
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Write(strFaqs.ToString());
context.Response.End();
reader.Close();
myCommand.Dispose();
myConnection.Close();
}
catch (Exception ex)
{
string exception = ex.Message;
// Logg the exception here
}
finally
{
myConnection.Close();
myCommand.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Here is one of my Source Pages PaintFaqs.aspx which should pass a Parameter = 1 to the Handler
Another Source Page Glossary.aspx should pass a Parameter = 6.
I would then check that Parameter within the Handler Object to set FactTypeID
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
CodeBehind="PaintFaqs.aspx.cs" Inherits="DaVincisApp1.PaintFaqs" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
function ToggleFAQ(title) {
//$("#" + title).toggle("slow");
Aobj = document.getElementById(title);
DIVobj = document.getElementById(title + "-link-div");
LINKobj = document.getElementById(title + "-link-a");
current_state = Aobj.style.display;
if (current_state != "inline") {
Aobj.style.display = "inline";
Aobj.style.visibility = "visible";
DIVobj.className = "question-clicked";
LINKobj.className = "questionLink-clicked";
}
else {
Aobj.style.display = "none";
Aobj.style.visibility = "hidden";
DIVobj.className = "faqquestion";
LINKobj.className = "questionLink";
}
}
$(document).ready(function () {
$.getJSON('FaqsJson.ashx', function (datas) {
var str = "";
var index = 1;
$.each(datas, function () {
str += "<div id=\"A" + index + "-link-div\" class=\"faqquestion\">" +
"<a id=\"A" + index + "-link-a\" href=\"javascript://\" class=\"questionLink\" onclick=\"ToggleFAQ('A" + index + "');\">" +
this['Question'] +
"</a>" +
"</div>";
str += "<div id=\"A" + index + "\" style=\"display: none;\"> " +
"<div class=\"faqanswer\">" +
"<div class=\"answerbox\">" +
this['Answer'] +
"</div>" +
"</div>" +
"</div>";
index++;
});
$("#mid-featureleft-client .controlbox").html(str);
});
});
</script>
<div id="top-feature-client">
<div class="contentheader">
<img alt="image1" src="images/Facts/FaqHeader2.png" style="height: 245px; width: 848px" />
</div>
<div style="clear: both;">
</div>
</div>
<div id="mid-feature-client">
<div id="mid-featureleft-client">
<div class="contentheader">
<h1>
General Painting Facts</h1>
<img height="16px" width="552px" src="Images/Columns/hr_red1.png" alt="" />
</div>
<div class="controlbox">
<br />
</div>
<div style="clear: both;">
</div>
</div>
<div style="clear: both;">
</div>
</div>
Pass your page specific
FactTypeIdusing query string. For example,And at the handler side,