Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

get rowid in gridview

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
fwstealer
Asked:
fwstealer
1 Solution
 
Bob HoffmanDeveloperCommented:
look at the ItemDataBound event. You can use e.Row to build the link reference when the grid is being bound.
0
 
prairiedogCommented:
Is "lnkbtnChangeReq" defined inside the GridView or outside?
0
 
jinalCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jinalCommented:
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
 
fwstealerAuthor Commented:
lnkbtnChangeReq is outside of the gridview.
0
 
jinalCommented:
Try my previos two comments. it helps you.
0
 
prairiedogCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now