Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Archive for the ‘JavaScript’ Category

Print in ASP.NET 2.0

Posted by Ramani Sandeep on May 19, 2009

Option 1 : Using ASP.NET (C# / VB )

One of the most common functionality in any ASP.NET application is to print forms and controls. There are a lot of options to print forms using client scripts. In the article, we will see how to print controls in ASP.NET 2.0 using both server side code and javascript.

Click here to read more…..

Option 2 : Using Javascript

In the article, we will see how to print controls in ASP.NET 2.0 using javascript.


Option 3 : Printing a GridView with Paging


This is a WebControl that provides an easy way to prepare an ASP.NET GridView to be paged and printed in the browser.


Click here to read more…..

Posted in ASP.NET, JavaScript | Tagged: , , | 3 Comments »

JavaScript Access to Page Controls after Ajax Update (via Update Panel)

Posted by Ramani Sandeep on April 7, 2009

Several times, we are in a situation where we need to do some work the moment the update panel finishes its update (ASP.NET 2.0 AJAX). There are situations like showing an error if any during the update, showing a success message after a proper update, setting up scroll bars, changing some UI component, updating some other part of UI, etc. The list can get longer as per the developer’s requirement. I had tried to show how we can get control to the page once the update is finished. We can tweak things via JavaScript the way we need to out there in the handler.

Click Here for more Details

Posted in ASP.NET Ajax, JavaScript | Tagged: , | Leave a Comment »

How to refresh an UpdatePanel from javascript

Posted by Ramani Sandeep on April 7, 2009

I thought that updating an UpdatePanel from javascript was one of the most frequently asked question about ASP.NET Ajax, but even looking around a lot I didn’t find a clean and polished solution for what should be a common task.

after few minutes of Googl-ing i found a useful link. View it for more details

Click Here for more details…

Posted in ASP.NET Ajax, JavaScript | Tagged: | Leave a Comment »

Text Box Characters Counter

Posted by Ramani Sandeep on February 26, 2009

In almost all the web projects, we require a multiline TextBox control. It was annoying to find that maxLength property doesn’t work on multiline textboxes.

There are various ways to solve this issue, here i am using javascript to restrict user once he reach maxlimit.

<script type=”text/javascript” language=”javascript”>

function validatelimit(obj, maxchar)
{

if(this.id) obj = this;

var remaningChar = maxchar – obj.value.length;
document.getElementById(‘<%= Label1.ClientID %>’).innerHTML = remaningChar;

if( remaningChar <= 0)
{
obj.value = obj.value.substring(maxchar,0);
return false;

}
else
{return true;}

}

</script>

<asp:TextBox ID=”TextBox1″ runat=”server” TextMode=”MultiLine” onkeyup=”return validatelimit(this,500)” Height=”400px”></asp:TextBox>
<asp:Label ID=”Label1″ runat=”server” Text=”500″></asp:Label>

Other useful links :

http://forums.asp.net/t/1251514.aspx

Hope this will Help u.

Posted in JavaScript | Tagged: , | Leave a Comment »

How to get appSetting values in .aspx file

Posted by Ramani Sandeep on February 16, 2009

Hi,

U can use app settings in aspx similar to connectionstrings in webconfig file

In your web config file

<appSettings>
<add key ="myKey" value ="myValue"/>
</appSettings>

In your aspx file

<asp:TextBox ID = "txtBox1" runat = "server" Text = "<%$appSettings:myKey %>" />

Similarly you can access appsettings values in javascript like this:

var my1 = ‘<%=ConfigurationManager.AppSettings["myKey"].ToString() %>’;

alert(my1);

Hope it helps

Posted in ASP.NET, JavaScript | Tagged: , , | 5 Comments »

Displaying Alert Message Boxes from your .aspx page

Posted by Ramani Sandeep on December 17, 2008

Step 1 : Create a General class and place the following Code in it

public static void CreateMessageAlert(System.Web.UI.Page senderPage,
string alertMsg, string alertKey)
{
ScriptManager.RegisterStartupScript(senderPage, senderPage.GetType(), alertKey, “alert(‘” + alertMsg + “‘);”,true);
}

Step 2 : Call the created method from where ever you wish to send the alert from


string alertmessage = “Thank You for visiting DotNetSpider.com”;

YourClassName.CreateMessageAlert(this,alertmessage,”alertKey”);

Where YourClassName is nothing but the name of the class file where your
CreateMessageAlert method resides and alertmessage is where you assign the
string you wish to display.

Posted in ASP.NET, C# 2.0, JavaScript | Tagged: , , | 1 Comment »

Hide an ASP.NET Label control after a few seconds

Posted by Ramani Sandeep on November 7, 2008

I needed a way to hide an ASP.NET Label control after a few seconds.

here is the code to do that :

<form id=”form1″ runat=”server”>
<div>
<asp:Label runat=”server” ID=”label1″ Text=”First”></asp:Label>
</div>
</form>

<script type=”text/javascript”>
self.setInterval(“changeValue()”,4000);

function changeValue()
{
document.getElementById(“label1″).innerText = “”;

}
</script>

Posted in ASP.NET, JavaScript | Tagged: , , , | 2 Comments »

Selecting All CheckBoxes in GridView

Posted by Ramani Sandeep on July 19, 2008

This article will contain code that is used to Check / Uncheck checkbox in Gridview.

javascript on the page is :

<script type="text/javascript">

    var TotalChkBx;

    var Counter;

    window.onload = function()

    {

        //Get total no. of CheckBoxes in side the GridView.

        TotalChkBx = parseInt('<%= this.gvCheckboxes.Rows.Count %>');

        //Get total no. of checked CheckBoxes in side the GridView.

        Counter = 0;

    }

    function HeaderClick(CheckBox)

    {

        //Get target base & child control.

        var TargetBaseControl = document.getElementById('<%= this.gvCheckboxes.ClientID %>');

        var TargetChildControl = "chkBxSelect";

 

        //Get all the control of the type INPUT in the base control.

        var Inputs = TargetBaseControl.getElementsByTagName("input");

 

        //Checked/Unchecked all the checkBoxes in side the GridView.

        for(var n = 0; n < Inputs.length; ++n)

            if(Inputs[n].type == 'checkbox' && Inputs[n].id.indexOf(TargetChildControl,0) >= 0)

                Inputs[n].checked = CheckBox.checked;

        //Reset Counter

        Counter = CheckBox.checked ? TotalChkBx : 0;

    }

    function ChildClick(CheckBox, HCheckBox)

    {

        //get target base & child control.

        var HeaderCheckBox = document.getElementById(HCheckBox);

 

        //Modifiy Counter;

        if(CheckBox.checked && Counter < TotalChkBx)

            Counter++;

        else if(Counter > 0)

            Counter--;

 

        //Change state of the header CheckBox.

        if(Counter < TotalChkBx)

            HeaderCheckBox.checked = false;

        else if(Counter == TotalChkBx)

            HeaderCheckBox.checked = true;

    }

</script>

Html Code on the Page is :

<asp:GridView ID="gvCheckboxes" runat="server" AutoGenerateColumns="False" OnRowCreated="gvCheckboxes_RowCreated">

    <Columns>

        <asp:BoundField HeaderText="S.N." DataField="sno">

            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

        </asp:BoundField>

        <asp:TemplateField HeaderText="Select">

            <ItemTemplate>

                <asp:CheckBox ID="chkBxSelect" runat="server" />

            </ItemTemplate>

            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

            <HeaderTemplate>

                <asp:CheckBox ID="chkBxHeader" onclick="javascript:HeaderClick(this);" runat="server" />

            </HeaderTemplate>

        </asp:TemplateField>

        <asp:TemplateField>

            <ItemTemplate>

                <asp:CheckBox ID="chkBx" runat="server" />

            </ItemTemplate>

            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

        </asp:TemplateField>

    </Columns>

    <RowStyle BackColor="Moccasin" />

    <AlternatingRowStyle BackColor="NavajoWhite" />

    <HeaderStyle BackColor="DarkOrange" Font-Bold="True" ForeColor="White" />

</asp:GridView>

Code Behind code :

protected void Page_Load(object sender, EventArgs e)

{

    if (!IsPostBack)

    {

        BindGridView();

    }

}

 

protected void BindGridView()

{

    gvCheckboxes.DataSource = GetDataSource();

    gvCheckboxes.DataBind();

}

 

protected DataTable GetDataSource()

{

    DataTable dTable = new DataTable();

    DataRow dRow = null;

    Random rnd = new Random();

    dTable.Columns.Add("sno");

 

    for (int n = 0; n < 10; ++n)

    {

        dRow = dTable.NewRow();

 

        dRow["sno"] = n + ".";

 

        dTable.Rows.Add(dRow);

        dTable.AcceptChanges();

    }

 

    return dTable;

}

 

protected void gvCheckboxes_RowCreated(object sender, GridViewRowEventArgs e)

{

    if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState ==

 

    DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))

    {

        CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkBxSelect");

        CheckBox chkBxHeader = (CheckBox)this.gvCheckboxes.HeaderRow.FindControl("chkBxHeader");

 

        chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(this,'{0}');",

 

        chkBxHeader.ClientID);

    }

}

Posted in ASP.NET, C# 2.0, JavaScript | Tagged: , | 2 Comments »

 
Follow

Get every new post delivered to your Inbox.

Join 317 other followers