Tuesday, October 13, 2009

ASP.NET With JQuery

Hello Frnds,
Here I post you reference link of article which give good example of how to implement asp.net with jquery.

I know some of the people are aware of it but still I post link of that article here.
http://www.dotnetcurry.com/BrowseArticles.aspx?CatID=63

Kinjal Shah
(Asp.Net Developer)

Thursday, October 1, 2009

Ajax Style File Upload

Hello Friends,
I need to implement file upload functionality using ajax. So after doing some RND on it then I will come with solutions.

Here I post live sample of ajax style file upload.

ASPX File:-uploadtest.aspx


   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="uploadtest.aspx.cs" Inherits="uploadtest" %>

   2:   

   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   4:  <html xmlns="http://www.w3.org/1999/xhtml">

   5:  <head runat="server">

   6:      <title></title>

   7:   

   8:      <script language="javascript" type="text/javascript">

   9:         

  10:          function onerror(args, context) {

  11:              document.getElementById("<%=message.ClientID %>").innerHTML = "Upload Failed";

  12:          }

  13:          function results(args, context) {

  14:              document.getElementById("<%=message.ClientID %>").innerHTML = "File has been uploaded successfully";

  15:          }

  16:          function Button1_onclick() {        

  17:              arguments = document.getElementById("path").value + "/" + document.getElementById("File1").value;            

  18:              <%= cbref %>

  19:          }

  20:      </script>

  21:  </head>

  22:  <body>

  23:      <form id="form1" runat="server">

  24:      <div>

  25:          <input type="text" id="path" value="" />

  26:          <input id="File1" type="file" />

  27:          <input id="Button1" type="button" value="upload" onclick="return Button1_onclick()" />

  28:      </div>

  29:      <span id="message" runat="server"></span>

  30:      <script language="javascript" type="text/javascript">

  31:          document.getElementById("<%=message.ClientID %>").innerHTML = "";

  32:      </script>

  33:      </form>

  34:  </body>

  35:  </html>



CS File:-uploadtest.aspx.cs


   1:  using System;

   2:  using System.Collections.Generic;

   3:  using System.Linq;

   4:  using System.Web;

   5:  using System.Web.UI;

   6:  using System.Web.UI.WebControls;

   7:  using System.Web.UI.HtmlControls;

   8:  using System.Net;

   9:   

  10:  public partial class uploadtest : System.Web.UI.Page, ICallbackEventHandler

  11:  {

  12:      protected string cbref = string.Empty;

  13:      protected void Page_Load(object sender, EventArgs e)

  14:      {

  15:   

  16:          ClientScriptManager csm = Page.ClientScript;

  17:          cbref = csm.GetCallbackEventReference(this, "arguments", "results", null, "onerror", true);

  18:      }

  19:   

  20:   

  21:      #region ICallbackEventHandler Members

  22:   

  23:      public string GetCallbackResult()

  24:      {

  25:          return cbref;

  26:      }

  27:   

  28:      public void RaiseCallbackEvent(string eventArgument)

  29:      {

  30:          System.Net.WebClient wc = new System.Net.WebClient();

  31:          wc.UploadFile("http://localhost:2830/Amadeus/uploadfile.aspx", "POST", eventArgument);

  32:      }

  33:   

  34:      #endregion

  35:  }



Now here is an uploadfile.aspx.cs where file upload start.


   1:  using System;

   2:  using System.Collections.Generic;

   3:  using System.Linq;

   4:  using System.Web;

   5:  using System.Web.UI;

   6:  using System.Web.UI.WebControls;

   7:  using System.Web.UI.HtmlControls;

   8:   

   9:  public partial class uploadfile : System.Web.UI.Page

  10:  {

  11:      protected void Page_Load(object sender, EventArgs e)

  12:      {

  13:          HttpPostedFile myfile = Request.Files[0];

  14:          string path = Server.MapPath("~/upload");

  15:          if (!System.IO.Directory.Exists(path))

  16:          {

  17:              System.IO.Directory.CreateDirectory(path);

  18:          }

  19:          string filename = myfile.FileName;

  20:          string fullpath = System.IO.Path.Combine(path, filename);

  21:          myfile.SaveAs(fullpath);

  22:      }

  23:  }



I hopes this will help you :)
Happy Programming...!

Reference : - http://www.dotnetfunda.com/articles/article484-ajax-style-fileupload.aspx

Regards,
Kinjal Shah

Wednesday, September 23, 2009

Create Custom control

Hello frnds,
I think most of people who need to make textbox value as a mandatory then they are use required field validator in each page. But at my point of view this is not good and so that I came with proper solution. I have create on class file in which I inherited textbox web server control and made that textbox control value with mandatory.

Here I post sample example of it and I hopes it will help you also.

Class File : TextControl.CS
Whenever you add any class file then it will prompt you to add app_code folder. So for that click on no.


   1:  using System;

   2:  using System.Collections.Generic;

   3:  using System.Linq;

   4:  using System.Web;

   5:  using System.Web.UI.WebControls;

   6:  /// <summary>

   7:  /// Summary description for TextControl

   8:  /// </summary>

   9:  /// 

  10:   

  11:   

  12:  namespace ControlValidator

  13:  {

  14:      

  15:      public class TextControl:TextBox 

  16:      {

  17:          public bool Required { get; set; }

  18:          public string ErrorMessage { get; set; }

  19:          

  20:          private RequiredFieldValidator RequiredFieldValidator;

  21:          protected override void OnInit(EventArgs e)

  22:          {

  23:              if (this.Required)

  24:              {                

  25:                  RequiredFieldValidator = new RequiredFieldValidator();

  26:                  RequiredFieldValidator.ControlToValidate = this.ID;

  27:                  RequiredFieldValidator.ErrorMessage = this.ErrorMessage;

  28:                  Controls.Add(RequiredFieldValidator);

  29:              }

  30:          }

  31:   

  32:          protected override void Render(System.Web.UI.HtmlTextWriter writer)

  33:          {

  34:              base.Render(writer);

  35:              if (this.Required)

  36:              {

  37:                  RequiredFieldValidator.RenderControl(writer);

  38:              }

  39:          }

  40:      }

  41:      

  42:      

  43:  }






After that u need to create dll file for this class file using following command.
I hopes you current prompt is you working webapp path.

C:\Documents and Settings\kinjal.SOLUTIONS.000\My Documents\Downloads\MyCustomControls\MyCustomControls>
csc /out:bin\ControlValidator.dll /target:library /r:system.dll TextControl.cs

After that you add default.aspx file
File Name : Default.aspx


   1:  <%@ Register Namespace="ControlValidator" Assembly="ControlValidator" TagPrefix="Control" %>

   2:   

   3:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

   4:   

   5:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   6:  <html xmlns="http://www.w3.org/1999/xhtml">

   7:  <head runat="server">

   8:      <title></title>

   9:  </head>

  10:  <body>

  11:      <form id="form1" runat="server">

  12:      <div>

  13:          <Control:TextControl runat="server" ID="txtName" Required="true" ErrorMessage="Name Is Required"></Control:TextControl>

  14:          <br />

  15:          <asp:Button ID="btnOk" runat="server" Text="Click me" />

  16:      </div>

  17:      </form>

  18:  </body>

  19:  </html>





Happy Programming and coding...!