vb.net 将 GridView 列值传递给 Javascript 函数 - OnClientClick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29631847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:01:18  来源:igfitidea点击:

Pass GridView column value to Javascript function - OnClientClick

javascriptasp.netvb.net

提问by w0051977

I have a grid view like this:

我有一个像这样的网格视图:

<asp:SqlDataSource ID="SqlPersistentOffenders" runat="server"></asp:SqlDataSource>
    <asp:GridView ID="GVPersistentOffenders" runat="server" DataSourceID="SqlPersistentOffenders" AllowPaging="true" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="PersonID" Visible="false">
            <ItemTemplate>
                 <asp:Label ID="LabCreUser" runat="server" Text='<%# Bind("creUser")%>'></asp:Label>                       
            </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="ButtonSendEmail" runat="server" CommandName="SendEmail" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" Text="Send Email" OnClientClick="GetEmailAddress()"/>                     
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The JavaScript looks like this:

JavaScript 看起来像这样:

<script type="text/javascript">
        function GetEmailAddress(creuser) {
           //Do something
}
</script>

How can I pass the value in LabCreUser for the row selected to the Javasript function?

如何将 LabCreUser 中所选行的值传递给 Javasript 函数?

I have looked here: Passing variables to javascript in onclientclickand also a few other questions. Nothing I have tried has worked.

我看过这里:Passing variables to javascript in onclientclick以及其他一些问题。我尝试过的一切都没有奏效。

回答by Enrique Zavaleta

You can pass the parameter to the JS function this way

你可以通过这种方式将参数传递给 JS 函数

OnClientClick=<%# "GetEmailAddress('" + Eval("creUser") + "')" %>

this will became in

这将成为

OnClientClick="GetEmailAddress('yourValue')"

回答by Marwah Abdelaal

you can simply replace the <asp:button>with an html button, and use onclickto call your javascript function, and if you need a server side function add runat="server"

您可以简单地将 替换为<asp:button>html 按钮,并用于onclick调用您的 javascript 函数,如果您需要服务器端函数,请添加runat="server"

Hope this will help

希望这会有所帮助