Reputation: 47
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.
Upvotes: 0
Views: 3987
Reputation: 148120
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 + "')");
}
}
}
Upvotes: 2