I used the Gridview in my website.
I want to get the unique id of that row in which we clicked anywhere in the GridView.
Here is my code.
Source code:
C# code:
But its only gives the id =0...I want the id from database on which we clicked row in GridView.
I want to get the unique id of that row in which we clicked anywhere in the GridView.
Here is my code.
Source code:
Code:
<Columns>
<%--<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="select" runat="server" ForeColor="Black" Visible="false" Text="SingleClick" CommandName ="SingleClick" CommandArgument='<%#Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-VerticalAlign="Top">
<ItemTemplate ><table><tr><td onclick="javascript:__doPostBack('GridView1$ctl02$ctl00','')" >
  <asp:LinkButton ID="Label2" runat="server" CssClass="underline" Text='<%#Eval("title") %>' Font-Bold="True" Font-Size="Medium" ForeColor="#333399" /><br />
<asp:LinkButton ID="Label3" runat="server" CssClass="underline" Text='<%#Eval("location") %>' ForeColor="Green" /> <br />
<asp:Label ID="Label5" runat="server" Text='<%#Eval("Description") %>' CssClass="lighttext1"/> <br />
Date:- <asp:Label ID="Label4" runat="server" Text='<%#Eval("date") %>' />
<br /></td></tr></table>
</ItemTemplate>
<ItemStyle VerticalAlign="Top"></ItemStyle>
</asp:TemplateField>
C# code:
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
_singleClickButton = (LinkButton)e.Row.FindControl("select");
_singleClickButton.ID = e.Row.DataItem("id").ToString();
// Get the javascript which is assigned to this LinkButton
string _jsSingle =ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
// Add this javascript to the onclick Attribute of the row
e.Row.Attributes["onclick"] = _jsSingle;
// _singleClickButton = (LinkButton)e.CommandSource;
_singleClickButton.CommandArgument = ((GridViewRow)sender).RowIndex.ToString();
}
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.className='highlight';");
e.Row.Attributes.Add("onmouseout", "this.className='normal';");
//e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference( DirectCast(sender, System.Web.UI.Control), "Select$" + e.Row.RowIndex.ToString());
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SingleClick")
{
Response.Redirect("showjobs.aspx?id=" + e.CommandArgument.ToString());
}
}
Comment