Click here to Skip to main content
15,886,110 members
Articles / Web Development / ASP.NET
Tip/Trick

How to handle a selected row in GridView on the client-side

Rate me:
Please Sign up or sign in to vote.
2.00/5 (2 votes)
7 May 2010CPOL 14.7K   1   1
1. Create hidden field onto your page like following:2. Then add script block at your aspx page:function select(rowIndex) { document.getElementById('').value = rowIndex; }3. In...
1. Create hidden field onto your page like following:

<input id="inp_row_index" type="hidden" runat="server" />


2. Then add script block at your aspx page:

function select(rowIndex) {
        document.getElementById('<%=inp_row_index.ClientID %>').value = rowIndex;
        <%=Page.GetPostBackEventReference(inp_row_index) %>
    }


3. In RowCreated event handler add following code snipet:

if (e.Row.RowType == DataControlRowType.DataRow)
            { 
                e.Row.Attributes.Add("onclick", String.Format("select({0});",e.Row.RowIndex));
            }

4. At the end add to code-behind:

protected void Page_Load(object sender, EventArgs e)
        {
            inp_row_index.ServerChange += new EventHandler(inp_row_index_ServerChange);
        }

        void inp_row_index_ServerChange(object sender, EventArgs e)
        {
            int index = int.Parse(inp_row_index.Value);
            gvTemplates.SelectedIndex = index;
            Response.Write("Selected Index:"+index.ToString());
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Belarus Belarus
Software developer with over 3 years of extensive experience in analysis, design and development. Recently, most interested in refactoring and design patterns applied to .NET Framework.

Core technologies I am using: OOP, OOD, DDD, TDD, N-tier applications, enterprise development.

Certificates:
Brainbench: .NET Framework 3.5 Fundamentals, Data Modeling Concepts, Web Design Concepts, C#

Microsoft: Exam 70-526: TS: Microsoft .NET Framework 2.0 - Windows-Based Client Development

INTUIT.RU: Development of Web-application ASP. NET Using Visual Studio. NET, Web applications in ASP.NET, Microsoft .NET Framework Distributed Applications Development.

Specialties
NET Framework : 2.0, 3.5, 4.0
Languages and technologies: C#, ASP.NET MVC 2, ASP.NET MVC 3, WCF, ASP.NET 4.0, Web Services, ADO.NET, LINQ, Entity Framework, NHibernate 3.0, JavaScript, HTML, CSS, XML, Ajax
RDBMS : SQL Server 2005, 2008, 2008 R2, MS Access.
Reporting: MS SQL Reporting Services, Crystal Reports

Comments and Discussions

 
GeneralReason for my vote of 3 Still needs some clear explanation Pin
Phaneendra Varanasi16-Jan-12 5:45
Phaneendra Varanasi16-Jan-12 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.