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


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


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


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


Sort ListItems in ASP.NET

clock February 11, 2007 12:41 by author gnineth
Sorting of ListItems inside a Dropdownlist or Listbox is not provided by default .net framework. Either you need to sort your items at the database level (using order by) if the lists are binded with a datasource from database, or you need to write your own class which implements the IComparer interface and override the Compare method stub.

I write an example on how you write the class and override the Compare method. The compare method takes as input, 2 objects (x and y) and performs a comparison on the two objects. Passing objects as parameters will help in sorting custom object types as well as datatypes like int string etc., The function returns 0 if the two objects are equal, a number less than zero if x is less than y, and a number greater than zero if x is greater than y.

A class ListItemComparer which implements the IComparer interface and overrides the Compare method.

[code:c#]
// A class which implements the IComparer interface.
public class ListItemComparer : IComparer
{
    public int Compare(object x, object y)
    {
        ListItem li_x = (ListItem)x;
        ListItem li_y = (ListItem)y;
        CaseInsensitiveComparer c = new CaseInsensitiveComparer();
        return c.Compare(li_x.Text, li_y.Text);
    }
}
[/code]


Now, we can have a generalized static method, which takes the Dropdownlist as input and sorts it accordingly.

[code:c#]
public static void SortDropDownList(DropDownList dlList)
{
    ArrayList arl = null;
    if (dlList.Items.Count > 0)
    {
        arl = new ArrayList(dlList.Items.Count);
        foreach (ListItem li in dlList.Items)
            arl.Add(li);
     }
    arl.Sort(new ListItemComparer());
    dlList.Items.Clear();
    for (int i = 0; i < arl.Count; i++)
        dlList.Items.Add(arl[i].ToString());
}
[/code]


Now you can use this method to sort any dropdownlist by passing it to the above method.

E Screw

Be the first to rate this post

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


Enter key on Textbox Control

clock February 11, 2007 10:03 by author gnineth

To enable the enter key on a textbox, to get the functionality of clicking a button, write the javascript. If you have only one button, and by setting the AutoSubmitBehavior to True, you can achieve this, but when you have multiple buttons, when the user press the Enter key, the first focused button is clicked.

With javascript the enter key code can be trapped. The following code will fire the enter key event, on the body.

<body onkeydown="if(event.keyCode == 13){document.getElementById('btnSubmit').click();}">
...
<asp:Button id="btnSubmit" runat="server" Text="Submit"></asp:Button>


If you want to trap the enter key on a textbox use the following

<head>
   <script language="javascript">
       function doEnterKey()
        {
            if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13))
            {
                document.form1.submit();
            }
        }
   </script>
</head>
<body>
   <form id="form1" runat="server">
      <asp:TextBox id="Text1" runate="server" />
   </form>
</body>
</html>

in Page_Load procedure in code-behind file write the following code

submit1.Attributes.Add("onfocus","javascript:doEnterKey();")
Text1.Attributes.Add("onchange","javascript:doEnterKey();")

E Screw

Be the first to rate this post

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


String formats in C#

clock January 30, 2007 06:33 by author gnineth

I have found a good article by Steve Tibbett for string formatting.
Check this link http://www.stevex.org/dottext/articles/158.aspx

E Screw

Be the first to rate this post

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


Trust levels in ASP.NET Application

clock December 27, 2006 02:19 by author gnineth
When you are hosting your application in a shared hosting environment, you need to think about the trust levels that you need to set for your application.

Apart from the security to your web application and the files ASP.NET provides, trust levels are those, which are used to sucure the files and application in the hosting environment.  The trust levels are set in the web.config file and can be set at machine, site and application levels.

What is Trust?
The application runtime, can be set to configure trust levels, which are used to contrain what an application can do. That is, if an application is running on full trust, it means, it allows the native code to execute, can look at a process, to find or corrupt or delete data from other applications. A trust level of minimum means, its a highly restrictive and a full trust means, there are no restrictions at all. The default trust level for asp.net applications is full trust.

In a full trust level application, its even possible to walk through the directories, retrieve a listing of files in specified folder outside of the root where the code executes.

With medium trust level, asp.net places a number of restrictions on the application. The application file access is restricted within the virtual directory where the application resides.

Trust level specification in web.config

<trust level="[Full|High|Medium|Low|Minimal]" originUrl="URL"processRequestInApplicationTrust = "[True|False]" />
For more information on trust level check this link: trust element (ASP.NET Settings Schema)

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