Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取gridview列值作为javascript函数的参数_C#_Javascript_Gridview_Popup - Fatal编程技术网

C# 获取gridview列值作为javascript函数的参数

C# 获取gridview列值作为javascript函数的参数,c#,javascript,gridview,popup,C#,Javascript,Gridview,Popup,我有一个gridview,它有一定数量的列,我想从中获取值的ID列当前设置为visible=false。另一列是TemplateField列(LinkButton),当用户单击该按钮时,它将从ID列获取值,并将该值传递给我的一个javascript函数 网络表单: <script language=javascript> function openContent(contentID) { window.open('myConten

我有一个gridview,它有一定数量的列,我想从中获取值的ID列当前设置为visible=false。另一列是TemplateField列(LinkButton),当用户单击该按钮时,它将从ID列获取值,并将该值传递给我的一个javascript函数

网络表单:

<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" />
不是这样,我正在尝试根据用户选择的项目获取intID,因此当用户单击linkbutton时,它将使用javascript打开一个弹出窗口,ID将用作querystring。

您可以使用a的属性并使用正确的参数构造对javascript方法的正确调用。类似于

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

谢谢大家的帮助,但我似乎从发布的另一个问题中找到了解决方案

只需更换此:

((LinkButton)e.Row.FindControl("lnkContent")).Attributes.Add("onclick", "return openContent('" + intID + "');");
使用此选项(需要指定DataKeyName):


您还可以通过设置OnClientClick属性(正如我在回答中提到的)来完成同样的事情。这还有一个额外的好处,就是将元素声明放在一个地方,而不是在标记中声明某些功能,在.aspx文件中绑定某些功能。虽然用任何一种方法都会奏效。也许我会尝试一下,这听起来更简单。再次感谢
((LinkButton)e.Row.FindControl("lnkContent")).Attributes.Add("onclick", "return openContent('" + intID + "');");
((LinkButton)e.Row.FindControl("lnkContent")).Attributes.Add("onclick", "return openContent('" + ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString() + "');");