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


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