All posts tagged ASP.NET

‘HttpUtility’ does not exist in the current context

Last couple days working I developed a social networking share control for asp.net

http://www.smartwebsource.info/smartshare/Default.aspx

Where i use BitlyApi to shorten my url to send twitter.
There i use BitlyApi class in my App_Code folder . As it is web project so HttpUtility is found from System.Web.dll.
I try to make my SmartSocialShare class library to a dll so i make a new class library .
And paste all my Class file from my App_Code folder in the new Class library project . When i try to build the dll it show this error.

‘HttpUtility’ does not exist in the current context

Then i add System.Web.dll as a reference in my class library then it make my dll without error.
Hopes this will help you to solve this error.

Programmatically adding Javascript File to User Control in .net

Today i am devoloping a share book mark plugin for asp.net. There i need to add the css and java script dynamically in the page . As i try to make my user control independent and do not want depend on master page or any other page.
After searching on Google i found a good solution in http://stackoverflow.com. From there i use this code which may help you.

Bellow is the code behind of my user control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Control_ucPopUpShareButton : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            HtmlGenericControl jsJquery = new HtmlGenericControl("script");
            jsJquery.Attributes["type"] = "text/javascript";
            jsJquery.Attributes["src"] = "js/jquery-1.4.2.min.js";
            Page.Header.Controls.Add(jsJquery);



            HtmlLink stylesLink = new HtmlLink();
            stylesLink.Attributes["rel"] = "stylesheet";
            stylesLink.Attributes["type"] = "text/css";
            stylesLink.Href = "css/BookmarkShare.css";
            Page.Header.Controls.Add(stylesLink);


        }
    }
}

 

And then see you source view that the java script and css is added in your page where you add the user control.
See bellow picture of my browser source view.
Source view of browser to show css and js add in your page head.

Get URL of ASP.Net Page

Some time we need to get the current page location . Today i will show how to do that.

This function can be used to retrieve the current page’s name, i.e default.aspx, hello.aspx or whatever.

 
public string GetCurrentPagename()
{

      string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);    
string sRet = oInfo.Name;  
return  sRet ;
}

If you want to need other things to retrive from url like bellow image then just call that.

How to Loop through GridView Rows? GridViewRow

We need to loop through or navigate the GridView rows from outsite the GridView control’s event.

For example, you need to navigate all the rows in a gridview control and find a child control placed inside it. The sample code below helps you to find a Checkbox placed inside a GridView control.

C# code

 
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)gvrow.FindControl("CheckBox1");
if (CheckBox1.Checked)
{
// we write this code for find a label value
   Label lblLookFor = (Label)gvrow.FindControl("lblLookFor");
string lookfor = lblLookFor.Text;
//Do your operations here.
}
}

Get all files in Directory and Sub directory using Asp.net

Get all files in Directory and Sub directory using Asp.net is easy task we have to use this name space using System.IO;

And Call the GetFiles() function to get all files from the directory we use MapPath(“.”) to get the root folder and show all files in side it. If you want to show a perticular folder’s file then change (“.”) to (“foldername”) then it will show all the files in side the folder.


  public void GetFiles()
 {
      string    path =    MapPath(".");
        if (File.Exists(path))

        {
            // File path
            ProcessFile(path);
        }

 else if (Directory.Exists(path))

{
            // This path is a directory

            ProcessDirectory(path);

        }

    }
  
    // Process all files in the directory passed in, recurse on any directories

    // that are found, and process the files they contain.

    public void ProcessDirectory(string targetDirectory)
{
        // Process the list of files found in the directory.

        string[] fileEntries = Directory.GetFiles(targetDirectory);

        foreach (string fileName in fileEntries)

            ProcessFile(fileName);

         // Recurse into subdirectories of this directory.

        string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);

        foreach (string subdirectory in subdirectoryEntries)

            ProcessDirectory(subdirectory);

    }

    // Insert logic for processing found files here.

    public void ProcessFile(string path)

    {

        FileInfo fi = new FileInfo(path);

        Response.Write("Path: " + path + "<br/>");
     

    }

 

The above GetFiles() method print this

Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\ASP\About.aspx
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\ASP\About.aspx.cs
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\ASP\About.aspx.designer.cs
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\Contact.aspx.cs
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\Contact.aspx.designer.cs
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\Default.aspx
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\Default.aspx.cs