从 GridView TemplateField 调用 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9631776/
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-10-26 07:17:17  来源:igfitidea点击:

Call Javascript from GridView TemplateField

c#javascriptasp.net

提问by vml19

I have a GridView, I have coded like the below

我有一个 GridView,我编码如下

<asp:TemplateField HeaderText="Item">
 <ItemTemplate>                                            
   <asp:Button ID="btnItem" OnClientClick="javascript:SearchReqsult(<%#Eval("Id") %>);"
 CssClass="Save" runat="server" /> 
</ItemTemplate>
</asp:TemplateField>

I face a run time error which says that my button control is formatted correctly I suppose the issue with the following

我面临一个运行时错误,它说我的按钮控件格式正确我想问题与以下

OnClientClick="javascript:SearchReqsult(<%#Eval("Id") %>);"

Any idea?

任何的想法?

采纳答案by Vishal Patel

Try the following code

试试下面的代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
         Button btn = (Button)e.Row.FindControl("btnItem");
         btn.Attributes.Add("OnClick","SearchReqsult(this.id);");
    } 
 }

回答by Pankaj

Try with String.Format Class

尝试使用 String.Format 类

<asp:Button ID="btnCheque" runat="server" 
OnClientClick=<%#String.Format("return SearchReqsult('{0}');", Eval("ID")) %> />

回答by Arion

Can't you do this is databound. Maybe something like this:

你不能这样做是数据绑定的。也许是这样的:

protected void Page_Load(object sender, EventArgs e)
{
    yourGridView.RowDataBound += (s, ev) =>
            {
               if (ev.Row.RowType == DataControlRowType.DataRow)
               {
                   var btn= (Button) ev.Row.FindControl("btnCheque");
                   btn.OnClientClick = "javascript:SearchReqsult(" + 
                                     DataBinder.Eval(ev.Row.DataItem, "Id") + ");";
               }
            };
}

回答by Neelam

Enclose your 'id' field in single quotes.

用单引号将您的 'id' 字段括起来。

just replace

只需更换

 btn.OnClientClick = "javascript:SearchReqsult(" + 
                                 DataBinder.Eval(ev.Row.DataItem, "Id") + ");";

with

 btn.OnClientClick = "javascript:SearchReqsult(" + 
                                 DataBinder.Eval(ev.Row.DataItem, 'Id') + ");";

your code will come as

您的代码将作为

protected void Page_Load(object sender, EventArgs e)
{
yourGridView.RowDataBound += (s, ev) =>
        {
           if (ev.Row.RowType == DataControlRowType.DataRow)
           {
               var btn= (Button) ev.Row.FindControl("btnCheque");
               btn.OnClientClick = "javascript:SearchReqsult(" + 
                                 DataBinder.Eval(ev.Row.DataItem, 'Id') + ");";
           }
        };
}

回答by Kaf

This is what you need to do

这是你需要做的

CODE BEHIND:

背后的代码:

protected void GridView_RowCreated(object sender,GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (((DataRowView)e.Row.DataItem) !=null)
            ((Button)e.Row.FindControl("btnCheque")).Attributes.Add("onclick","SearchReqsult(" + ((DataRowView)e.Row.DataItem)["productId"].ToString() + ")");

    }
}

HTML:

HTML:

<asp:Button ID="btnCheque" CssClass="Save" runat="server" Text="My Button" /> 

回答by Coder

Try enclosing javascript function call in single quotes:

尝试用单引号括起 javascript 函数调用:

OnClientClick='javascript:SearchReqsult(<%#Eval("Id") %>);'

OnClientClick='javascript:SearchReqsult(<%#Eval("Id") %>);'

Maybe the problem is with missing/wrong quotes.

也许问题在于缺少/错误的引号。

回答by Sandip Patel

Call javascript function from Template Field

从模板字段调用 javascript 函数

<asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
    <asp:LinkButton ID="lnkDelete" OnClientClick=<%#String.Format("return dis_status('{0}');", Eval("Status")) %> runat="server" CommandArgument='<%# Eval("AgtId")%>' Text='<%# Eval("Status")%>' CommandName="cmdDelete" />
</ItemTemplate>

回答by Kaushik Patel

Put Labe l and bind Id in that and make it visible false.

放置标签 l 并在其中绑定 Id 并使其可见为 false。

On GridView RowDatabound event add attrinute to control and assign javascript on button onclientclick event

在 GridView RowDatabound 事件上添加属性来控制和分配 javascript onclientclick 事件按钮

回答by PraveenVenu

Try with single quotes instead of double and string.Format()

尝试使用单引号而不是双引号和 string.Format()

<asp:Button ID="btnCheque" OnClientClick=<%# string.Format("javascript:SearchReqsult('{0}');return false;",Eval("id")) %> CssClass="Save" runat="server" />