I do not recommend using Response.Write to insert JavaScript into the page from your C# or VB.NET code because the JavaScript gets inserted into an invalid place in the HTML that is generated (before everything). While most browsers fix this problem, it is not always fixed the way that you expect and strange things can happen.
Instead I recommend a different approach.
Add a Label to the repeater whose text will be bound to the "Sno" value and that will have a style of "display:none" so that it is not visible on the screen.
In the Repeater's ItemDataBound event, retrieve the "Sno" value from the Label and add an "onclick" attribute to the LinkButton that will execute JavaScript that will open the new window...passing it the "Sno" value via query string.
I created a quick application to demonstrate what I'm talking about.
The application has 2 pages in it:
- WebForm1.aspx : where the repeater with the links in it exits
- WebForm2.aspx : the page that is called when one of the links in the repeater is clicked
In my repeater I used regular hyperlinks instead of LinkButtons since I didn't want to postback to the server when the link is clicked (I just want to open the new page).
Here is the Repeater asp code markup in my WebForm1.aspx file:
- <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" >
-
<HeaderTemplate>
-
<table>
-
</HeaderTemplate>
-
<ItemTemplate>
-
<tr>
-
<td>
-
<asp:Label ID="Sno" runat="server" Style="display: none" Text='<%#DataBinder.Eval(Container.DataItem,"Sno") %>' />
-
<asp:HyperLink ID="fNameLink" runat="server" NavigateUrl='#'
-
Text='<%#DataBinder.Eval(Container.DataItem,"F_Name") %>' />
-
<asp:Label ID="lab" runat="server">
-
<%#DataBinder.Eval(Container.DataItem,"City") %>,<%#DataBinder.Eval(Container.DataItem,"Designation")%><br />________________________
-
</asp:Label>
-
</td>
-
</tr>
-
</ItemTemplate>
-
<FooterTemplate>
-
</table>
-
</FooterTemplate>
-
</asp:Repeater>
Here is the page code:
(vb.net)
-
Private _dt As System.Data.DataTable
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
If Session("_dt") Is Nothing Then
-
RetrieveDataSource()
-
Else
-
_dt = DirectCast(Session("_dt"), System.Data.DataTable)
-
End If
-
End Sub
-
Private Sub WebForm1_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
-
Repeater1.DataSource = _dt
-
Repeater1.DataBind()
-
End Sub
-
-
Protected Sub Repeater1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs)
-
Dim sNo As Label = e.Item.FindControl("Sno")
-
Dim fnamelink As HyperLink = e.Item.FindControl("fnamelink")
-
If sNo IsNot Nothing AndAlso fnamelink IsNot Nothing Then
-
fnamelink.Attributes.Add("onclick", "javascript:window.open('WebForm2.aspx?Sno=" + sNo.Text + "','popup','target=_blank,width=200,height=100');")
-
End If
-
End Sub
-
-
Private Sub RetrieveDataSource()
-
_dt = New System.Data.DataTable
-
_dt.Columns.Add("Sno", GetType(Integer))
-
_dt.Columns.Add("City")
-
_dt.Columns.Add("F_Name")
-
_dt.Columns.Add("Designation")
-
-
For i As Integer = 1 To 10
-
Dim dr As System.Data.DataRow = _dt.NewRow
-
dr("Sno") = i
-
dr("City") = "City " + i.ToString
-
dr("F_Name") = "Name " + i.ToString
-
dr("Designation") = "Designation " + i.ToString
-
_dt.Rows.Add(dr)
-
Next
-
Session("_dt") = _dt
-
End Sub
(C#)
- System.Data.DataTable _dt;
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
if (Session["_dt"] == null) { RetrieveDataSource(); }
-
else { _dt = (System.Data.DataTable)Session["_dt"]; }
-
}
-
-
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
-
{
-
Label sNo = (Label)e.Item.FindControl("Sno");
-
HyperLink fnamelink = (HyperLink)e.Item.FindControl("fnamelink");
-
if (sNo != null && fnamelink != null)
-
{
-
fnamelink.Attributes.Add("onclick", "javascript:window.open('WebForm2.aspx?Sno=" + sNo.Text + "','popup','target=_blank,width=200,height=100');");
-
}
-
}
-
-
void Page_PreRender(object sender, EventArgs e)
-
{
-
Repeater1.DataSource = _dt;
-
Repeater1.DataBind();
-
}
-
-
private void RetrieveDataSource()
-
{
-
_dt = new System.Data.DataTable();
-
_dt.Columns.Add("Sno", typeof(int));
-
_dt.Columns.Add("City");
-
_dt.Columns.Add("F_Name");
-
_dt.Columns.Add("Designation");
-
-
for (int i = 1; i < 10; i++)
-
{
-
System.Data.DataRow dr = _dt.NewRow();
-
-
dr["Sno"] = i;
-
dr["City"] = "City " + i.ToString();
-
dr["F_Name"] = "Name " + i.ToString();
-
dr["Designation"] = "Designation " + i.ToString();
-
_dt.Rows.Add(dr);
-
}
-
Session["_dt"] = _dt;
-
}
My WebForm2.aspx page is very simple. It displays the Sno selected. Here is the ASP markup for the page:
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
<title></title>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div>
-
You selected Sno:
-
<asp:Literal ID="snoSelected" runat="server" />
-
</div>
-
</form>
-
</body>
-
</html>
Here is the code behind for the page.
(VB.NET)
-
Public Class WebForm2
-
Inherits System.Web.UI.Page
-
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
snoSelected.Text = Request.QueryString("Sno")
-
End Sub
-
-
End Class
(C#)
-
namespace WebApplication2
-
{
-
public partial class WebForm2 : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
snoSelected.Text = Request.QueryString["Sno"];
-
}
-
}
-
}
-Frinny