Solved

get rowid in gridview

Posted on 2009-07-14
7
561 Views
Last Modified: 2012-05-07
I have a gridview and want to capture the rowid so I can pass it as a parameter in a query string. Below is my code thus far and it is only working for the first row and not the others rows.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

            CellPadding="4" DataKeyNames="ReqId" DataSourceID="SqlDataSource1" 

            ForeColor="#333333" GridLines="None" AllowPaging="True" 

            AllowSorting="True" 

            OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 

            onpageindexchanged="GridView1_PageIndexChanged" 

            >

            <RowStyle BackColor="#EFF3FB" />

            <Columns>

                <asp:CommandField ShowSelectButton="True" />

                <asp:BoundField DataField="ReqId" HeaderText="Requisition Id" 

                    SortExpression="ReqId" Visible="true" />
 
 

<asp:LinkButton ID="lnkbtnChangeReq" runat="server" 

                onclick="lnkbtnChangeReq_Click">Change Requisition</asp:LinkButton>
 
 

protected void lnkbtnChangeReq_Click(object sender, EventArgs e)

    {

        //GridView1

        string rowId = GridView1.DataKeys[0].Value.ToString();

        //try to prevent url tampering

        Random random = new Random();

        int num = random.Next(1000); //random number that means nothing

        string strq = "m"; //modify for close of req

        //build querystring

        string url = "updaterequisition.aspx?";

        url += "rid=" + rowId + "&";

        url += "svd=" + num + "&";

        url += "sr=" + strq;

        Response.Redirect(url);

    }

Open in new window

0
Comment
Question by:fwstealer
7 Comments
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24850814
look at the ItemDataBound event. You can use e.Row to build the link reference when the grid is being bound.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24850842
Is "lnkbtnChangeReq" defined inside the GridView or outside?
0
 
LVL 15

Expert Comment

by:jinal
ID: 24850959
First my question lnkbtnChangeReq is out side GridView or inside grdiview.

1. IF out side.




protected void lnkbtnChangeReq_Click(object sender, EventArgs e)

    {

        //GridView1

        string rowId = GridView1.DataKeys[GridView1.SelectedRow.RowIndex].Value.ToString();

        //try to prevent url tampering

        Random random = new Random();

        int num = random.Next(1000); //random number that means nothing

        string strq = "m"; //modify for close of req

        //build querystring

        string url = "updaterequisition.aspx?";

        url += "rid=" + rowId + "&";

        url += "svd=" + num + "&";

        url += "sr=" + strq;

        Response.Redirect(url);

    }

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Accepted Solution

by:
jinal earned 500 total points
ID: 24850981
It is better to check SelectedRow is null or not.

protected void lnkbtnChangeReq_Click(object sender, EventArgs e)

    {

        //GridView1

        if(GridView1.SelectedRow != null)

        {

        string rowId = GridView1.DataKeys[GridView1.SelectedRow.RowIndex].Value.ToString();

        //try to prevent url tampering

        Random random = new Random();

        int num = random.Next(1000); //random number that means nothing

        string strq = "m"; //modify for close of req

        //build querystring

        string url = "updaterequisition.aspx?";

        url += "rid=" + rowId + "&";

        url += "svd=" + num + "&";

        url += "sr=" + strq;

        Response.Redirect(url);

       }

    }

Open in new window

0
 

Author Comment

by:fwstealer
ID: 24851227
lnkbtnChangeReq is outside of the gridview.
0
 
LVL 15

Expert Comment

by:jinal
ID: 24851239
Try my previos two comments. it helps you.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24851289
Here is what you need to do:

protected void lnkbtnChangeReq_Click(object sender, EventArgs e)

    {

        //GridView1

        GridViewRow row = (GridViewRow)(sender.NamingContainer);

        string rowId = GridView1.DataKeys[row.RowIndex].Value.ToString();

        //try to prevent url tampering

        Random random = new Random();

        int num = random.Next(1000); //random number that means nothing

        string strq = "m"; //modify for close of req

        //build querystring

        string url = "updaterequisition.aspx?";

        url += "rid=" + rowId + "&";

        url += "svd=" + num + "&";

        url += "sr=" + strq;

        Response.Redirect(url);

    }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now