Pages

Thursday, May 26, 2011

Clear All Controls In asp net by javascript


Add This in your server side event

string myScript = String.Format("ClearAllControls();");ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Blank", myScript, true);

Javascript Function
function ClearAllControls()
    {
        var pnlMain = document.getElementById("updPanelMain");
        var pnlMainControls = pnlMain.getElementsByTagName("input");
        var pnlMainddlControls = pnlMain.getElementsByTagName("select");
        var pnlMainControlstextarea = pnlMain.getElementsByTagName("textarea");
       
            for (i = 0; i <=pnlMainControlstextarea.length-1 ; i++)
            {       
             
                 pnlMainControlstextarea[i].disabled = false;
                    pnlMainControlstextarea[i].innerHTML ="";                   
               
            }
            for (i = 0; i <=pnlMainControls.length-1 ; i++)
            {       
                if (pnlMainControls[i].type == "text")
                {
                 pnlMainControls[i].disabled = false;
                    pnlMainControls[i].value ="";                   
                }
            }
            for (i = 0; i <=pnlMainddlControls.length-1 ; i++)
            {
                    if(pnlMainddlControls[i].options[0]!=null || pnlMainddlControls[i].options[0]!=undefined)
                    {
                        pnlMainddlControls[i].disabled = false;
                        pnlMainddlControls[i].options[0].selected = true;
                    }
            }
    }
Get Datetime field format in GridView

<% 
# DataBinder.Eval(Container.DataItem, "last_use_dt", "{0:dd/MM/yyyy}")%>

Auto SrNo in GridView
<%#Container.DataItemIndex+1 %>
-----------------------------------------------------------------------------------------------
Get Max From Data Table

decimal srno = 0;List<decimal> srnoList = DtlCollSecuOther.AsEnumerable().Select(t => t.Field<decimal>("srno")).Distinct().ToList();
srno = srnoList.Max();
 

Wednesday, November 24, 2010

Crystal Report Example

In Aspx file
  <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" PrintMode="ActiveX" DisplayGroupTree="False"></CR:CrystalReportViewer>

In Aspx.cs  file

ReportDocument rptDocument = null;
protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            rptDocument = new ReportDocument();
            Session["rptDocument"] = rptDocument;
        }
        else
        {
            rptDocument = (ReportDocument)Session["rptDocument"];
            if (rptDocument == null)
            {
                rptDocument = new ReportDocument();
                Session["rptDocument"] = rptDocument;

            }
        }
        if (IsPostBack)
        {
            CreateReport();
        }

    }
private void CreateReport()
    {
       
rptDocument = new ReportDocument();
            string Query = string.Empty;
rptDocument.Load(Server.MapPath("report path"));
using (OdbcConnection OCN = new OdbcConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString))
            {
                OCN.Open();

                OdbcCommand ocmd = OCN.CreateCommand();
                OdbcDataAdapter sadpt = new OdbcDataAdapter(ocmd);
dsReport DSTEMP = new dsReport();
                DSTEMP.Clear();
                ocmd.CommandText = Query;
                sadpt.Fill(DSTEMP, "datatable1");
                rptDocument.SetDataSource(DSTEMP);
                rptDocument.SetParameterValue("",””);//parameter nameAnd val
                CrystalReportViewer1.ReportSource = rptDocument;
                CrystalReportViewer1.Visible = true;
            }
    }
    protected void cmdPrint_Click(object sender, EventArgs e)
    {
        rptDocument.PrintToPrinter(1, false, 0, 0);
    }



Saturday, November 20, 2010

In Aspx
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
        <Services>
            <asp:ServiceReference Path="~/WebServices/AutoComplete.asmx" />
        </Services>
    </asp:ScriptManager>
  <asp:TextBox ID="txtName" TabIndex="3" runat="server" CssClass="TextBox"
                        Width="250px" MaxLength="100"></asp:TextBox>
                    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" Enabled="true"
                        EnableCaching="true" MinimumPrefixLength="1" TargetControlID="txtName"
                        ServiceMethod="GetNames" ServicePath="~/WebServices/AutoComplete.asmx">
                    </cc1:AutoCompleteExtender>
In asmx.cs file
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Web.Script.Services;
using System.Data;
[ScriptService()]

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class AutoComplete : System.Web.Services.WebService
{
    [WebMethod]

    [ScriptMethod()]
    public string[] GetNames(string prefixText, int count)
    {

        ArrayList filteredList = new ArrayList();

        MySQLConnection con = new MySQLConnection();
        string qry = "SELECT column_name FROM table_name WHERE column_name LIKE '%" + prefixText + "%' ";
        DataSet ds;
        ds = con.getDataset(qry);

        foreach (DataRow dr in ds.Tables[0].Rows)
        {            if(dr["column_name"].ToString().ToLower().StartsWith(prefixText.ToLower()))
                filteredList.Add(dr["column_name "].ToString());
        }
        con.closeConnection();
       

        return (string[])filteredList.ToArray(typeof(string));
    }

}


Saturday, October 30, 2010

Maxlength for decimal field on textbox


set maxlegth=16 for 13 digit  + 1 .(point) +  2 fraction digit
MaxLength="16" onblur="fnDecMax(this.id,this.getAttribute('MaxLength'));"

javascript function
----------------------

function fnDecMax(id,maxLength)
{

theValue = document.getElementById(id).value;
maxLength = maxLength -1;
rx = /[^0-9.]/;
if(rx.test(theValue))
{
alert("The field can only contain numbers");
return;
}
if(theValue.indexOf(".") != -1)
{
theValue = theValue.substring(0,(theValue.indexOf(".") + 3));
}
lnt = theValue.length;
if(lnt > maxLength )
{
if(theValue.indexOf(".") == -1) {
theValue = theValue.substring(0,maxLength);
}
else
{
theValue = theValue.substring(0,(maxLength+1));
}
lnt = theValue.length;
}
if(lnt > (maxLength-2) && theValue.indexOf(".") == -1)
{
first = theValue.substring(0,(maxLength-2));
second = theValue.substring((maxLength-2));
theValue = first + "." + second;
}
document.getElementById(id).value= theValue;
}

Search This Blog