views:

498

answers:

2

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.

A: 

You could just use the OnClientClick property of a LinkButton and construct a proper call to your JavaScript method with the correct argument. Something along the lines of

<asp:LinkButton ID="lnkContent" runat="server" ...
    OnClientClick='openContent(<%#Eval("contentID") %>)' ... />
R0MANARMY
A: 

thanks for the help guys but it seems that I found the solution from one of the other questions posted.

just need to replace this:

((LinkButton)e.Row.FindControl("lnkContent")).Attributes.Add("onclick", "return openContent('" + intID + "');");

with this (need to specify the DataKeyNames):

((LinkButton)e.Row.FindControl("lnkContent")).Attributes.Add("onclick", "return openContent('" + ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString() + "');");
newName
You can also accomplish the same thing by setting the OnClientClick property (as I mentioned in my answer). That also has the added benefit of having your element declaration in one place instead of some functionality being declared in markup and some bound in the .aspx file. Though doing it either way will work.
R0MANARMY
maybe i'll give it a try, it sounds more straightforward. thanks again
newName