fwstealer
asked on
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);
}
look at the ItemDataBound event. You can use e.Row to build the link reference when the grid is being bound.
Is "lnkbtnChangeReq" defined inside the GridView or outside?
First my question lnkbtnChangeReq is out side GridView or inside grdiview.
1. IF out side.
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
lnkbtnChangeReq is outside of the gridview.
Try my previos two comments. it helps you.
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);
}