0

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.

3
  • Do you definitely have to use javascript? If not,you could fire the redirect on the ItemCommand event picking out the DataKeyValue (your unique reference number per row)? Commented May 16, 2012 at 10:53
  • Javascript is the only way i've found to open a new window without having to click my linkbutton twice! Commented May 16, 2012 at 21:59
  • :D Well you can do a one click open window without javascript but seeing as you've got a solution, we won't need to worry about that! Commented May 17, 2012 at 8:10

1 Answer 1

2

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

                } 
         }          
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.