Pages

Upload file to server using javsvcript in Asp.net

1)Add Datagrid to a page use Item template 2)Add inline frame in a page 3)Upload.aspx page contain a file control going upload file into server 4)gridupload .aspx on rowdatabound call javascript function browserup1() 5)browserup1() internally call file control & upload file to a server gridupload.aspx
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <script type="text/javascript" language="javascript"> function Browseupl() { var ifUpload; var confirmUpload; ifUpload = ifu.document.form1; ifUpload.myFile.click(); // confirmUpload = confirm ("You are about to upload the file " + ifUpload.myFile.value + " to the server. Do you agree to Upload?"); // if (confirmUpload) // { ifUpload.btnSubmit.click(); // } } </script> </head> <body> <form id="form1" runat="server"> <div> <iframe src="Upload.aspx" frameborder="1" id="ifu" name="ifu" style="display:none;"></iframe> <asp:GridView ID="grdview" runat="server" AutoGenerateColumns="false" OnRowDataBound="grdview_RowDataBound"> <Columns> <asp:BoundField DataField="TntCode" HeaderText="Tenant Code" /> <asp:BoundField DataField="TntName" HeaderText="Tenant Name" /> <asp:BoundField DataField="FrmDt" HeaderText="From Date" /> <asp:BoundField DataField="ToDt" HeaderText="To Date" /> <asp:TemplateField> <ItemTemplate> <asp:ImageButton ID="btnimg" runat="server" CommandName="UPLOAD" ImageUrl="Images/upload_file_icon.gif" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> <div> </div> </form> </body> </html>
gridupload.cs
public partial class gridupload : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Bindgrid(); //grdview.Attributes.Add("onclick", "Browseupl();"); } } private void Bindgrid() { DataTable dt = new DataTable(); DataRow dr; DataColumn dc; dc = new DataColumn("TntCode"); dt.Columns.Add(dc); dc = new DataColumn("TntName"); dt.Columns.Add(dc); dc = new DataColumn("FrmDt"); dt.Columns.Add(dc); dc = new DataColumn("ToDt"); dt.Columns.Add(dc); dr = dt.NewRow(); dr["TntCode"] = "123456"; dr["TntName"] = "Jhon"; dr["FrmDt"] = "06/10/2008"; dr["ToDt"] = "09/10/2008"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["TntCode"] = "452130"; dr["TntName"] = "Peter"; dr["FrmDt"] = "08/10/2008"; dr["ToDt"] = "09/10/2008"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["TntCode"] = "452045"; dr["TntName"] = "James"; dr["FrmDt"] = "04/10/2008"; dr["ToDt"] = "09/10/2008"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["TntCode"] = "785245"; dr["TntName"] = "Ricky"; dr["FrmDt"] = "02/10/2008"; dr["ToDt"] = "09/10/2008"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["TntCode"] = "742569"; dr["TntName"] = "Martin"; dr["FrmDt"] = "07/10/2008"; dr["ToDt"] = "09/10/2008"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["TntCode"] = "124568"; dr["TntName"] = "Bill"; dr["FrmDt"] = "03/10/2008"; dr["ToDt"] = "09/10/2008"; dt.Rows.Add(dr); grdview.DataSource = dt; grdview.DataBind(); } protected void grdview_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[4].Attributes.Add("onclick", "Browseupl();"); } } } }
Upload.aspx
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <script type="text/javascript"> function SubmitForm() { // Simply, submit the form document.form1.submit (); } </script> </head> <body> <form id="form1" runat="server"> <div> <input type="file" runat="server" id="myFile" name="myFile" style="visibility:hidden;" /> <input type="button" runat="server" id="btnSubmit" name="btnSubmit" onclick="javascript:SubmitForm();" style="visibility:hidden;" /> <br /><asp:Label ID="lblMsg" runat="server" ForeColor="red" Font-Size="Medium" Font-Bold="true"></asp:Label> </div> </form> </body> </html>

Difference Between .Net & Java

Java
    1. Java is developed by Sun Microsystems
      Java is a light weight language and can be run on almost all the OS(it require less hardware)
      Java you need to confirm it that all the objects are destroyed before application quits.
      Java has no standard tool is available. Although, many third party IDEs are available
      Java is a programming language designed to be run on many different platforms, and so uses a common language which has to be compiled and run on different platforms (eg. windows, mac and linux).Any OS which is able to install JVMJava can be used to write programs for many different operating systems
      Java interface, a null object reference maps to the VT_NULL VARIANT value
      Java interface, all failure HRESULTs from the underlying COM interface are reported by throwing a com.ca.openroad.COMException containing that HRESULT value
  • .NET
    1. .Net is developed by Microsoft Corporation
      .Net needs a very heavy framework to be installed which have higher Hardware requirements too compared to Java
      .Net garbage collector runs at a certain interval and see is there is any memory occupied by anyobject whose parent is now finished processing (for eg. you closed an application), in this casethe garbage collector automatically removes the reference of that object and free up the memory
      .Net a standard development IDE is available that is Microsoft Visual Studio
      .NET, takes on a different approach, by allowing you to program in any language you choose,but has compilers for many different languages that generates a platform specific code (i.e.Microsoft or Windows).
       .NET can be used to make any programming language into a Windows program.NET COM interoperability layer maps null .NET object references to VT_EMPTY. VT_NULLis mapped to a special object type of class “System.DBNull”.
      .NET COM interoperability layer does something similar, using theInteropServices.COMException class. However, it uses that exception class only as a lastresort. If there is already a .NET exception class that reasonably represents the meaning of aparticular HRESULT, the interoperability layer throws that .NET exception instead of a COMException.
  • State Managment In Asp.net 2.0

    State Management: It maintain the information about the client towards the server is called State Management They are 2 Type 1) Client side State Management : It Store Information at client Computer by embedding information in the Web page I)View State:It use track value of the control .Stores information in hidden field II)Control State:It use in custom control to run view state properly. III)Hidden fileds:It is like a View State.It store information in Html Form IV)Cookies:It useto store state data that must avialiable on multiple pages.It stores state in Browser V)Query String :I t is use send email or instant Message state data with url .It stored data in URL 2) Server side State Managemnent:It store information at server side.Sharing information between web pages without sending data to client I) Session State: The Information available to all pages by logged user during single visit II) Application State: The information is available to all pages regardless of user Note :Session state & Application state are lost when apllication retart to presit use profile propertires

    SqlServer Database BackUp and Restore

    1) Copy 2 files. from SouceSystem Databasename.mdf , Database.ldf from installed sql server folder (path:c:\program files]microsoftSql server\Data\datbasname.mdf) 2) Create a Database in Destination system . 3) Past 2 file in installed sql server location (path:c:\program files]microsoftSql server\Data\datbasname.mdf) 4) Right click on destination database then click to attach datbase/files 5) Select appropriate databasename. mdf file Note:Assume sql server installed in C Drive .Path can vary

    Calling Server side Event handler function Uisng Ajax Update pannel

    1)Add Script manger to a page 2)Add Update panel in your page 3)Add any Server side standard Control . 4)In update panel call trigger tag .Inside trigger tag call asp:AysncPostBackTrigger control tag 5)Provide control id & eventname in above tag 6)Example below is assign Textbox value on buttin click
    pseduocode aspx code //Asume one button control id="bttnAjax" and one textbox in content template of update pannel id=txtAjax"
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
    <Triggers >
    <asp:AsyncPostBackTrigger ControlID="bttnAjax" EventName="click"/>
    </Triggers>
    <ContentTemplate>
    <asp:TextBox ID="txtAjax" runat ="server"/>
    </ContentTemplate>
    </asp:UpdatePanel>
    
    Code Behind
    protected void bttnAjax_Click(object sender, EventArgs e)
    
    {
    
    txtAjax.Text="Welcome To Ajax World "
    
    }
    
    
    
    
    
    
    
    

    Calling code behind function using Ajax

    1)Drag scriptmanger in your web page add attribute EnablePageMethods="true" 2)Create functions in javascript section (method name are similar example)       I)CallPageMethod()      II)CallParmeterPageMethod()      III)onSucceeded(result,userContext,MethodName)      IV))OnFailed(error,user) 3)Create static and return string function in Code behind (method name are similar example)     I)MyFirstPageMethod() add attribute [System.Web.Service.WebMethod()]     II)MyFirstParameterPageMehtod() add attribute[System.web.Service.WebMethod()] 4)Call javascript function on input button callPagemethod use event onclick
    script code
    function  CallPageMethod()
    {
    debugger;
    PageMethods.MyFirstPageMethod(onSucceeded,onFailed);
    
    }
    function  CallParametersPageMethod()
    {
    debugger;
    PageMethods.MyFirstParameterPageMethod("This is a Demo",onSucceeded,onFailed);
    
    }
    
    function onSucceeded(result,userContext,methodName)
    {
    function CallPageMethod()
    {;
    $get('div1').innerText=result;
    }
    function onFailed(error,user)
    {
    debugger;
    alert("An error occurred");
    }
    code behind code [System.Web.Services.WebMethod()] public static string MyFirstPageMethod() { return "Welcome to Ajaax"; }       [System.Web.Services.WebMethod()] public static string MyFirstParameterPageMethod(string sVal) { return "Welcome To ajax world value passed is" + sVal ; }

    Reterving & assigning values of Server control using javascript

    1)Create a function in javascript section of web page 2)Using document object call appropriate control id to retrive & assign value 3)Both control should be server control . 4)Call javascript function using Clientscript.RegisterStartupscript()
    pseudo code Javascript function fun1() { //Assume one Dropdown & TextBox var s = document.getElementById("ddllist").Value;//or .innerText (to get all value in ddl) document.getElementById("txtUser").Value=s//or .innerText; } Server Side Code //call in any function of server side code ClientScript.RegisterScript(this.getType(),"key1","fun1",True);