Electronic Screw

.NET :: Do[t]he NET

Passing parameters to javascript methods from controls in GridView

clock January 27, 2008 22:44 by author gnineth

Sometimes we may need to pass values (server side) to the client functions (javascript) on the GridView controls. In such case, we can attach the javascript functions to the GridView controls from code-behind using the RowDataBound event of the GridView.

I have a GridView that is bound to an ObjectDataSource, and I have converted one of the column into a TemplateField, so that I can access the controls in the underlying templates.

[code:html]
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CountryId"
        DataSourceID="ObjectDataSource1">
        <Columns>
            <asp:TemplateField HeaderText="TestColumn">
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" Text='Click Me'></asp:Button>
                </ItemTemplate>
            </asp:TemplateField>

[/code]



To attach a javascript function the Button1, we add the attributes in the RowDataBound event ( I use RowDataBound event because, you can access the underlying DataItem of the row, so that we can pass the database values to the javascript function. You can also use the RowCreatedEvent of the GridView to add the attributes.)

[code:c#]
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string paramValue = "somevalue";
            Button btnTest = (Button)e.Row.FindControl("Button1");
            if (btnTest != null)
                btnTest.Attributes.Add("onClick", "javascript:methodName('" + paramValue + "');");
        }
    }
[/code]


This way you can pass parameters from code-behind to javascript methods for the controls inside a GridView. My next post will be on how to access the underlying data in the RowDataBound event which helps in manipulating and customizing the way data is presented

E Screw

Currently rated 3.3 by 3 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Saving Master-Child entries using LINQ

clock January 26, 2008 16:08 by author gnineth

LINQ provides an easy way to save the master-child data and you need not worry about the PK/FK relations or manage them manually. Let us see how to save the Country/City information using LINQ

Create an instance of the DataContext you are working on. DataContext will be created when you create a new Linq To Sql file (.dbml). Drag the tables Country and City onto the designer and the relation will be created automatically. You can also see the filed names of the corresponding tables shown as properties the classes.

[code:c#]
ExampleDataContext db = new ExampleDataContext();
[/code]


Create the new Country and City

[code:c#]

// create a new country
Country country = new Country();
country.Name = "myCountry";

// create the cities
City city1 = new City();
city1.Name = "myCity1";

City city2 = new City();
city2.Name = "myCity2";

// relate the cities with the new country created
// because of the PK/FK relation, you will find the cities collection as a property of the Country which is automatically created by LINQ for you
country.Cities.Add(city1);
country.Cities.Add(city2);

// add the country to the database and save the changes
db.Countries.Add(country);
db.SubmitChanges();
[/code]


E Screw

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Working with ASP.NET AJAX ModalPopupExtender

clock January 25, 2008 22:20 by author gnineth

Recently I wrote an article about using ModalPopupExtender at www.aspalliance.com 

Check the article here www.aspalliance.com/1509

E Screw

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Preventing Duplicate Record Insertion on Page Refresh

clock November 12, 2007 18:48 by author gnineth

I have found a good article on preventing the duplicate record insertion on page refresh by Terri Mortan. Check the article here http://aspalliance.com/687

E Screw

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Page generation time using Stopwatch class

clock July 22, 2007 08:27 by author gnineth
Using the Stopwatch class (available from .NET 2.0) we can display the amount of time taken to process the page. Stopwatch gets you the most accurate time elapsed in milliseconds or ticks and the code below displays the time taken for the entire page life cycle to be processed.

Stopwatch class is available in System.Diagnostics namespace.

[code:c#]public partial class TestPage : System.Web.UI.Page {      // when the page is instantiated we start our stopwatch      System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();        protected void Page_Load (object sender, EventArgs e)  {            // your page load code      }      // override the Render method      protected override void Render (HtmlTextWriter writer)  {            // get the total time elapsed. this will be in milliseconds            // and I converted it to seconds            float _seconds = ((float)sw.ElapsedMilliseconds/1000);            // display the information in a label            this.lblPageGeneratedIn.Text = "Page generated in " + _seconds.ToString() + " seconds";            base.Render(writer);      } class="kwd">}[/code]

E Screw

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Microsoft Silverlight

clock May 3, 2007 04:43 by author gnineth
Microsoft Silverlight is a cross-browser, cross-platform plugin for delivering the next generation of .NET based media experiences and rich interactive applications for the Web. Silverlight offers a flexible programming model that supports JavaScript, .NET, and other languages.

To get started, tutorials, downloads visit : http://silverlight.net/GetStarted/

For complete source code used in the get started video visit : http://delay.members.winisp.net/SilverlightAirlinesDemo/SilverlightAirlinesDemo.zip

A demo of the Silverlight Airlines can be viewed at : http://delay.members.winisp.net/SilverlightAirlinesDemo/

Source: http://blogs.msdn.com/delay/archive/2007/05/01/the-web-just-got-even-better-silverlight-announced-at-mix07.aspx

Get Started!
E Screw

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Filters and Transitions

clock May 2, 2007 15:10 by author gnineth
I have found a good article on using the filters and transitions for use in web pages using the CSS and JavaScript. Check this link http://msdn2.microsoft.com/en-us/library/ms532847.aspx


E Screw

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Filters and Transitions

clock May 2, 2007 10:10 by author gnineth

I have found a good article on using the filters and transitions for use in web pages using the CSS and JavaScript. Check this link http://msdn2.microsoft.com/en-us/library/ms532847.aspx

E Screw

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Get DayNames in a given Culture

clock March 5, 2007 05:14 by author gnineth
Sometimes we may need to have the day names in a week for a given culture. With ASP.NET CultureInfo class you can loop through the DateTimeFormat.DayNames and get them. Here is the example of it

[code:c#]
using System.Globalization;

CultureInfo ci = new CultureInfo("ar-ae");   // gets the day names of arabic, arab emirates
foreach (string day in ci.DateTimeFormat.DayNames)
  Response.Write(day + "<br/>");
[/code]


Depending on the culture you want, you can get the CultureInfo reference for that corresponding culture. The DateTimeFormat also provides other values like month names, first day of week etc.,

- Electronic Screw

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Creating a CascadingDropDown using ASP.NET AJAX

clock February 12, 2007 05:32 by author gnineth

I have seen in many forums people asking questions as “how to populate the cascadingdropdown with values from database". Although there was a sample provided for the same in the ASP.NET AJAX ControlToolkit, most of the learners (novice) following the walkthrough given at http://ajax.asp.net/ajaxtoolkit/Walkthrough/CCDWithDB.aspx get struck at the CarsTableAdapters used in the webservice.

So, I have posted an article Creating a CascadingDropDown Using AJAX at aspalliance.com which explains the step-by-step procedure on how to create CascadingDropDown using database values with the help of Microsoft SQL Server 2005 Express Edition.

E Screw

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Search

Calendar

<<  November 2008  >>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Archive

Tags

Categories


Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2008

    Sign in