开发者

Getting gridview column value as parameter for javascript function

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="Content开发者_Python百科Name" 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.


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") %>)' ... />


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜