Find the row number on click of a gridview row???

Hello everyone,

I need to find the gridview row number on click of a row on Page_load not on any other event..!! How do i do that?Please help me..!!

regards,

Francis P. 


Ferns
0
franferns
3/3/2008 1:34:11 PM
asp.net.presentation-controls 72751 articles. 1 followers. Follow

9 Replies
618 Views

Similar Articles

[PageSpeed] 0

Sorry Francis, but I believe you're out of luck as the Page.Load event is way too early in the Page lifecyle to pick this up. This would most likely be done in the SelectedIndexChanged, RowCommand, or Click event of a Button.


Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
3/3/2008 2:05:36 PM

Hi,
Ya i understand that. Is there any other way i can find out the Row number.I ll tell you my actual requirement.Im implementing the ICallbackEventHandler and implement the RaiseCallbackEvent event tht is where i need the row number of the gridview so that i can retrieve certain details based on the row number.I tried the other way round that is on rowboundData event of the gridview i add onclick attributes and store that in a hidden field but somehow i m not able to figure out y the hidden field value does not show up in the RaiseCallbackEvent..!!! Is there any other way i can do this..??

Thanks n regards,

Francis P.

 

 


Ferns
0
franferns
3/3/2008 2:24:37 PM

Hi:

  Please try this:

       protected string temp;
        public void RaiseCallbackEvent(string eventArgument)
        {
            temp = "At:" + DateTime.Now.ToString() + " you selected row " + eventArgument + ".";
        }

        public string GetCallbackResult()
        {
            return temp;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScript.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");

        }

        <script type="text/javascript">
function callToServer(index)
{
    var param = index;
    var context = "";
    WebForm_DoCallback('__Page',param,handleResultFromServer,context,null,false)

}

function handleResultFromServer(result, context)
{       alert(result);
}
</script>

        <asp:GridView DataSourceID="SqlDataSource1" ID="GridView2" runat="server">
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        <input onclick='<%# "callToServer(" +((GridViewRow)Container).RowIndex + ")"%>' type="button" value="Call to Server" />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
3/5/2008 1:53:41 AM

 Hello Allen,

Well thts exactly wat i was looking for !.Thank you. I have another question.I have this requirement of develpoing a website like say for example www.franferns.com and on navigation to a different page the URL in the address bar should not change it should always be www.franferns.com
I tried the URL Rewriting article but was not able to figure out anything.. Anything you can suggest me .Your any help would be appreciated.

Thanks n Regards

Francis P. 

 


Ferns
0
franferns
3/5/2008 4:49:51 AM

Hi:

  I think url rewriting should work. You can refer to:

http://msdn2.microsoft.com/en-us/library/ms972974.aspx

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
3/5/2008 5:11:29 AM

 

Hi Allen this is regarding the gridview once i click on the cells in the gridview i get an popup with details of the clicked row..!!At times on click nothing happens. Its like it never calls any of the function it ll just be's blank..!!! And then after i refresh starts working..!!!Any clue of wat is it that im goin wrong?

Regards,

Francis P. 


Ferns
0
franferns
3/5/2008 8:05:27 AM

Hi:

  My suggestion is to use AJAX control to do so:

    <asp:ScriptManager ID="ScriptManager2" runat="server">
    </asp:ScriptManager>

    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <cc2:PopupControlExtender  ID="PopupControlExtender1" runat="server" PopupControlID="DetailsView1" TargetControlID="GridView4">
    </cc2:PopupControlExtender>
     <asp:GridView DataKeyNames="theID" AutoGenerateSelectButton="true" ID="GridView4" DataSourceID="SqlDataSource1" runat="server">
    </asp:GridView>
    <asp:DetailsView BackColor="AliceBlue" DataSourceID="SqlDataSource2" ID="DetailsView1" runat="server" Height="50px" Width="125px">
    </asp:DetailsView>
    </ContentTemplate>
    </asp:UpdatePanel>


        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:AConnectionString %>"
            SelectCommand="SELECT * FROM Table_1"
            onselecting="SqlDataSource1_Selecting"></asp:SqlDataSource>


             <asp:SqlDataSource ID="SqlDataSource2" runat="server"
            ConnectionString="<%$ ConnectionStrings:AConnectionString %>"
            SelectCommand="SELECT * FROM Table_1 where theID=@theID"
            onselecting="SqlDataSource1_Selecting">
            <SelectParameters><asp:ControlParameter Name="theID" ControlID="GridView4" />
            </SelectParameters>
            </asp:SqlDataSource>

Learn and download ASP.NET  AJAX:

http://www.asp.net/ajax/

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
3/5/2008 9:38:36 AM

 Hi,

I cannot use that because the site im hosting does not support AJAX.!! Any idea as to y the previous solution tht implements ICallBack interface acts in tht way?

Regards,

Francis P. 


Ferns
0
franferns
3/5/2008 10:01:25 AM

Hi:

  OK please try this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication5.WebForm3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
          <script type="text/javascript">
function callToServer(index)
{
    var param = index;
    var context = "";
    WebForm_DoCallback('__Page',param,handleResultFromServer,context,null,false)

}

function handleResultFromServer(result, context)

 document.getElementById("detailsview").innerHTML=result;
 
}
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div id="detailsview">
    </div>
    <div>
              <asp:GridView DataKeyNames="theID" DataSourceID="SqlDataSource1" ID="GridView2" runat="server">
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        <input onclick='<%# "callToServer(" +((GridViewRow)Container).RowIndex + ")"%>' type="button" value="Call to Server" />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
        <asp:DetailsView DataSourceID="SqlDataSource2" ID="DetailsView1" runat="server" Height="50px" Width="125px">
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:AConnectionString %>"
            SelectCommand="SELECT * FROM [Table_1]"></asp:SqlDataSource>
           
             
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
            ConnectionString="<%$ ConnectionStrings:AConnectionString %>"
            SelectCommand="SELECT * FROM [Table_1] where theID=@theID">
            <SelectParameters>
            <asp:ControlParameter Name="theID" ControlID="GridView2" PropertyName="SelectedValue" />
            </SelectParameters></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication5
{
    public partial class WebForm3 : System.Web.UI.Page,ICallbackEventHandler
    {
      
        protected string temp;
        public void RaiseCallbackEvent(string eventArgument)
        {
            this.GridView2.SelectedIndex = Convert.ToInt32(eventArgument);
            this.DetailsView1.DataBind();
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite =
            new HtmlTextWriter(stringWrite);
            DetailsView1.RenderControl(htmlWrite);
            temp = stringWrite.ToString();
        }

        public string GetCallbackResult()
        {
            return temp;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScript.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");

        }

    }
}

  BTW if you have new problem please start a new thread.

Thanks


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
3/6/2008 2:14:16 AM
Reply:

Similar Artilces:

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Gridview row click event fires, when clicking on controls within row.
I have a gridview control and I have added  a row click event which causes a post back. e.Row.Attributes.Add("onClick", this.Page.ClientScript.GetPostBackEventReference(this, "Select$" + e.Row.RowIndex.ToString()));  When the user clicks the row, i bind some data to a checkbox list, showing a uers a list of options they have chosen. All good so far. I also have a edit button for each row. When the row is in edit mode i display a checkbox and textbox. Problem is when i click on the textbox or the checkbox, it causes the row click event to fire. Any ida...

How to find a outer gridview row when a button in inner gridview row is clicked
Hi, I have a gridview within a gridview. In the inner gridview, I have a column of imagebuttons. I cannot figure out how to find out the row of the outer gridview when I click on the imagebutton in the inner gridview. I modifies the code below to get rid of some of the unnecessary stuff. <asp:GridView ID="SitePrepMainGV" runat="server" DataKeyNames="SitePrepItemID" DataSourceID="SitePrepMasterDS" OnRowDataBound="SitePrepMainGV_RowDataBound" OnRowEditing="SitePrepMainGV_RowEditing" OnRowCreated="SitePrepMainGV_RowCreated"> <Columns> <asp:BoundField DataField="S...

Help! Find row on which a button was pressed in the gridview control in .NET 2.0
How do you get the exact row (and column data from that row)  on which a button was pressed within a gridview. In .NET v1 using the datagrid control the followingconstruction was available:objArgs.Item.Cells(n).Textusing the datagrid DataGridCommandEventArgs parameter. choices in gridview are     Sub cmdBackTestSummary(ByVal objSender As Object, ByVal objArgs As GridViewCommandEventArgs) ' objArgs.commandname ' objargs.commandarguement ' objargs.commandsource     End Sub but no objArgs.Item.Cells(n) So how do you get the exact row (and c...

gridview row numbers or row count
In my gridview I want to display the number for each row, so this column doesn't have any relation with any datasource. I want it to display the number of row that it represents, so if I delete a row it still has count and represents the right number of rows that I have in my gridview. Any suggestions are welcome also. I think this thread might have what you're looking for: http://forums.asp.net/p/992655/1292440.aspxMike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/...

How to determine the row number when clicking a delete button I added to a template column from a GridView control???
Hello all,I am working using the codebehinde the page with VB.NET 2005 (web application).  I have a gridview where I added a template column, inside that templated column I added a delete button.  I would like to know how could I determine which row number corresponds to the clicked button so I can delete the current record.This is what I have: Protected Sub LinkButtonDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Try Dim cmd As SqlCommand Dim objConnection As SqlConnection objConnection = New SqlConnecti...

Adding row (one at a time) in gridview on a button click. And cells of added row should contain controls like DROPDOWN LIST or TEXT BOX.
Dear Friends, I am in great need for the solution/hints/guidance of my requirement, if anyone can help with.  Let me explain what I need."I need a Gridview control on a webpage having following 5 columns/fields.     Column 1: It just has a "+" button in its header only (other cells for this column is empty)    Column 2: The Column header here is "FIELD" and  it should contain dropdown list (which is populated from some database)    Column 3: The Column header here is "Operator" and  it should ...

Data Controls insert rows into gridview then insert rows from gridview into SQL Table
Hello there, I'm still new at performing miracles with code . Currently I am having trouble with my latest coding adventure I have a web application which records jobs and labour assigned to jobs. The issue in particular I'm having is the way in which labour is to be assigned. I want to use standard controls (Textbox, Dropdownlist) to record the values into a gridview (ie,put multiple rows into the gridview using the controls). Then, when all the inserting is complete, the user clicks a submit button to insert all the rows from the gridview into a database table. The reason for...

The row number and offset of each row in the page should have a matching entry in row number table
Is the proper correction for the following to recreate the table BCPout/IN or table to table insert, or just drop and recreate the index? Row number is 0. Is there a way to correct just the data page? Table Corrupt: The row number and offset of each row in the page should have a matching entry in row number table; check this page (page#=772121 row#=0 offset in row number table=0) Table Corrupt: The row number and offset of each row in the page should have a matching entry in row number table; check this page (page#=772121 row#=0 offset in row number table=0) Table Corrupt: The row...

How to set the row number and get the row number for ("selected row) in Datagrid
I face a problem that i can't get the selected row of the datagrid in both server side script (VB.net) and client side script (javascript) As i can't use the button column / hyperlink column in the datagrid. Thus , i need to handle the change the background color of the selected row and also to get the selected row number for Update/Delete. Thanks in advance. This depends on how do you perform the selection. Do you want to implement chaning of the SelectedRow at the client side without any postback? If you have no issues with postback, then you can do s little trick here: 1. ...

GridView update rows by clicking or doubleclicking a row
I have created a Gridview control and programmatically set a row into edit mode when a user clicks on a row by implementing the rowcommand event. If the user changes any of the column values I catch the Textchanged event and the database gets updated with the new values. I would like to update the rows even if there is no change in any of the columns - what event should I implement? Thanks. Hey, What is the reason for updating database if you actually do not change anything? And using TextChanged event to invoke postback everytime you type in a letter is even worst. Use C...

does anyone know how to do that, when I click the last row of the gridview, the gridview will automatically add an empty new row for me.
does anyone know how to do that, when I click the last row of the gridview, the gridview will automatically add an empty new row for me.   thanks hi there,the only I could think of for now is is enbale footer rowthen hide it onpageload so when last row click set footer.visible to true  ...

How do I find out which row has been clicked on in a GridView
Hi all, I have a GridView but I do not have or want a SELECT button. Instead I have a link that when clicked on will navigate to a new page but I want to pass over three pieces of data contained within the row where the link was clicked. The problem is I can't work out how to find out which row was clicked on (remember I'm not using a SELECT button). Please help. Thanks is the link a BoundField/ItemTemplate or command field? It's a LinkButton within an ItemTemplate. It may help u plz check outCorrect me if i m wrong. <asp:GridView ID="dbgrid_patients&quo...

Get row info when an imagebutton on that row is clicked in a gridview
This is my gridview:<asp:GridView ID="GridView1" runat="server>   <Columns>      <asp:BoundField ...>      ...      <asp:TemplateField>         <ItemTemplate>            <asp:ImageButton ID="Button1" ImageUrl=... runat="server" OnClick="Button1_Click" />         </ItemTemplate>    ...

Web resources about - Find the row number on click of a gridview row??? - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 1/22/2016 11:20:52 PM