1

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.

2 Answers 2

1

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") %>)' ... />
Sign up to request clarification or add additional context in comments.

Comments

1

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() + "');");

2 Comments

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.
maybe i'll give it a try, it sounds more straightforward. thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.