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

Be the first to rate this post

  • Currently 0/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

Be the first to rate this post

  • Currently 0/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

Be the first to rate this post

  • Currently 0/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


Using GeoIPService of WebServiceX

clock February 14, 2007 17:00 by author
WebserviceX.net provides a wide range of webservices, that can be used freely in development. GeoIPService is one webservice available with them that can be used to display the users country information that he/she was browsing from. 

A small example on how to use the webservice (GeoIPService) in asp.net development. Create a new Project in Visual Studio. Right click on the project in the Solution Explorer and Click Add Web reference. In the Add Web Reference dialog box, enter the following url in the URL: box and click Go button.

http://www.webservicex.net/geoipservice.asmx?wsdl

If the webservice is found, the information will be displayed in the respective boxes. Click on Add Reference button. By default the web service will be added with the following namespace net.webservicex.www

The required webserive files will be added automatically in the App_WebReferences folder created under the root of your application. You will see the geoipservice.discomap and geoipservice.wsdl added under the net >> webservicex >> www folder.

Note: geoipservice.discomap and geoipservice.wsdl are the discovery and webservice definition language files, that provides the necessary methods and classes.

Now , we use the classes and methods provided by the webservice in our page to display the country information like name etc..

Here is a sample listing on how to use in the aspx page (C# code)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using net.webservicex.www;
using System.Text;

public
partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      // set the current country name
      StringBuilder _stringToDisplay = new StringBuilder();
      GeoIPService _gipService = new GeoIPService();
      GeoIP _geoIP = _gipService.GetGeoIP(Request.UserHostAddress.ToString());
      _stringToDisplay.Append(_geoIP.CountryName);
      _stringToDisplay.Append(
" [" + _geoIP.CountryCode + "] ");
      
      
lblCountryName.Text = _stringToDisplay.ToString();
   }
}

A sample of this implementation can be seen in my home page http://www.g9th.com in the top right corner.

-Electronic Screw

Be the first to rate this post

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


Search

Calendar

<<  August 2008  >>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

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