Archive

Archive for the ‘ASP.NET’ Category

Get Value Of Session variables from Generic Handlers

March 26, 2011 3 comments

if we are trying to access the value of session variables from Generic Handlers(.ashx files) like we acces from codebehind (.cs file) as the code below


public class MyHandler: IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";

string sessionValue = Convert.ToString(HttpContext.Current.Session["LoginUser"]);
}
}

we got NullReferenceException or Object reference not set to an instance of an object.

Solution

To overcome this exception and to access session variables from the HttpHandler, we should inherit the interface IReadOnlySessionState or IRequiresSessionState
(For this include the name space System.Web.SessionState first).

IReadOnlySessionState provides read-only access to session variables. And

IRequiresSessionState provides read/write access to session variables.

The working code is like this

using System.Web.SessionState;
public class MyHandler: IHttpHandler,IRequiresSessionState {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";

string sessionValue = Convert.ToString(HttpContext.Current.Session["LoginUser"]);
}
}

Categories: ASP.NET

Access HTML Controls From code behind

February 28, 2011 Leave a comment

This week i found an interesting logic to get the value of all HTML controls in a form from code behind..
Suitation
I am working on a dynamic form and the form contains no of input controls with unique name..
On the load evnt of page using innerhtml i build the form on the div inputs

Now on submi button click event of this form i need all values of input controls(NOte: There is no runat=”server” attribute with controls)

string value = Request.Form["mycontrol"];
 

this code returns the value of an input control with id [“mycontrol”] and

string[] controls = Request.Form.AllKeys;

this code returns the id of all input controls in the form

Logic here is

loop through all controols and get their values
protected void btnupdate_Click(object sender, EventArgs e)
{

string[] controls = Request.Form.AllKeys;
for (int i = 0; i < controls.Length; i++)
{
string value = Request.Form[controls[i]]; //contains the value of  input control.Before doing this check the controls[i] contains any portion of control name like this if (controls[i].Contains(“tblindustry”)
}

}

Categories: ASP.NET

Using ASP.NET Handler Using jQuery & Ajax With JSON

February 28, 2011 Leave a comment

For last few days i am working on some new(for me) technologies like
using Handlers in ASP.net web applications ..
Call the Handlers using Jquerry and ajax
Return normal text or html formated data from handlers back to web pages as response for ajax calls..
i spend some time- half of a day to get idea on these things and one thing i found is if we want to return more data (eg: details Of a persons based on some search criteria like his name,email,address..etc ) returning text/html is not a good way..
I got the concept Of JSON at this time and i found a good tutorial with sample code here….Here Is Link

Try these if some one needs these
And i create a code of my own that is available here

Categories: ASP.NET

Manipulating datas From gridview

February 12, 2011 Leave a comment


asp:GridView ID="grdSaleList" runat="server" AutoGenerateColumns="false" EmptyDataText="No Files Found"
DataKeyNames="ListFileId,ListFileName" CellPadding="3" CssClass="grid"
onrowcommand="grdSaleList_RowCommand" onrowdatabound="grdSaleList_RowDataBound"

And the screen shot of gridview is

gridview structre

gridview structre


If(!ispostBack)
{
loadGrid(); //write code to fill grid
}

protected void grdSaleList_RowDataBound(object sender, GridViewRowEventArgs e)
{

try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton imgdel = e.Row.FindControl("lnkDelete") as ImageButton;
imgdel.Attributes.Add("onclick", "javascript:return confirm('Are you want To Delete This File'"+DataBinder.Eval(e.Row.DataItem,"Title")+" ?')");

}
}
catch (Exception ex)
{
divErrorMessage.InnerHtml = ex.Message;
}

}

protected void grdSaleList_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
string id = e.CommandArgument.ToString();
GridViewRow currentRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
string fileName = Convert.ToString(grdSaleList.DataKeys[currentRow.RowIndex].Values[1]);
//gets the second keyvalue .In this case first key value is Id
if (e.CommandName == "ViewFile")
{
downloadFile(fileName);
}
if(e.CommandName=="DeleteFile")
{
DeleteListFile(id, fileName);
}
}
catch (Exception ex)
{
divErrorMessage.InnerHtml = ex.Message;
}
finally
{

}
}

Categories: ASP.NET