简体   繁体   English

传递GridView模板字段

[英]Pass GridView Template Field <asp:LinkButton control data to javascript function in my .aspx page

I have a GridView Template field containing an ASP:Label field which has a unique reference number per Row. 我有一个GridView模板字段,其中包含一个ASP:Label字段,每个行都有一个唯一的引用号。 I also have an open Javascript function assigned to that control which opens a URL in a new window and displays the document of the same reference number. 我还有一个分配给该控件的开放Javascript函数,它在新窗口中打开一个URL并显示相同参考编号的文档。

I don;t know how to obtain the reference number when the user clicks the LinkButton on a particular row, and pass it to my Javascript function before the window opens. 当用户单击特定行上的LinkBut​​ton时,我不知道如何获取引用号,并在窗口打开之前将其传递给我的Javascript函数。

Code: 码:

enter code here

function openPreview() 
{
     var url = "quotereport.aspx?view=esq&&ref=REFNUMBER"; window.open(url);
}

<asp:TemplateField HeaderText="" ShowHeader="False">
    <ItemTemplate>
    <asp:LinkButton ID="lbNewWindow" runat="server" OnClientClick="openPreview    ()">Window</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Any help would be much appreciated. 任何帮助将非常感激。

You will have to add RowDataBound event to the grid. 您必须将RowDataBound事件添加到网格中。 In RowDataBound event every row that is created is accessible with data. 在RowDataBound事件中,可以使用数据访问创建的每一行。 Bind javascript to link button instead of html as you did and pass Reference number to the javascript function from RowDataBound event. 将javascript绑定到链接按钮而不是html,并将参考编号从RowDataBound事件传递给javascript函数。

    protected void gvListing_RowDataBound(object sender, GridViewRowEventArgs e)
    {           
        if (e.Row.RowType == DataControlRowType.DataRow)
        {                
          System.Web.UI.WebControls.LinkButton lbNewWindow= new System.Web.UI.WebControls.LinkButton();    
                lbNewWindow = (System.Web.UI.WebControls.LinkButton)e.Row.FindControl("lbNewWindow");                    
                if (lbNewWindow!= null)
                {
                    string YourRefNumber = DataBinder.Eval("e.Row.DataItem", "ColumnNameInDataSource").ToString();
                    lbNewWindow.Attributes.Add("onclick","openPreview('"+ YourRefNumber + "')");

                } 
         }          
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM