简体   繁体   中英

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. 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.

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.

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. In RowDataBound event every row that is created is accessible with data. Bind javascript to link button instead of html as you did and pass Reference number to the javascript function from RowDataBound event.

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

                } 
         }          
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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