Getting gridview column value as parameter for javascript function
Posted
by newName
on Stack Overflow
See other posts from Stack Overflow
or by newName
Published on 2010-03-31T04:11:12Z
Indexed on
2010/03/31
4:13 UTC
Read the original article
Hit count: 557
I have a gridview with certain number of columns and the ID column where I want to get the value from is currently set to visible = false. The other column is a TemplateField column (LinkButton) and as the user clicks on the button it will grab the value from the ID column and pass the value to one of my javascript function.
WebForm:
<script language=javascript>
function openContent(contentID)
{
window.open('myContentPage.aspx?contentID=' + contentID , 'View Content','left=300,top=300,toolbar=no,scrollbars=yes,width=1000,height=500');
return false;
}
</script>
<asp:GridView ID="gvCourse" runat="server" AutoGenerateColumns="False" OnRowCommand="gvCourse_RowCommand"
OnRowDataBound="gvCourse_RowDataBound" BorderStyle="None" GridLines="None">
<RowStyle BorderStyle="None" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkContent" runat="server"
CommandName="View" CommandArgument='<%#Eval("contentID") %>' Text='<%#Eval("contentName") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="contentID" HeaderText="contentID" ReadOnly="True"
SortExpression="contentID" Visible="False" />
<asp:BoundField DataField="ContentName" HeaderText="ContentName"
ReadOnly="True" SortExpression="ContentName" Visible="False" />
</Columns>
<AlternatingRowStyle BorderStyle="None" />
Code behind:
protected void gvCourse_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "View")
{
intID = Convert.ToInt32(e.CommandArgument);
}
}
protected void gvCourse_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.FindControl("lnkContent")).Attributes.Add("onclick", "return openContent('" + intID + "');");
}
}
right not i'm trying to get the intID based on user selected item so when the user clicks on the linkbutton it will open a popup window using javascript and the ID will be used as querystring.
© Stack Overflow or respective owner