473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to raised event through link button which is child control of repeater

Hello,
I am using this:

Expand|Select|Wrap|Line Numbers
  1. <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="select">
  2.     <ItemTemplate>
  3.     <tr>
  4.     <td>
  5.     <asp:LinkButton ID="link" runat="server" Font-Size="X-Large">
  6.     <%#DataBinder.Eval(Container.DataItem,"F_Name") %>
  7.     </asp:LinkButton><br />
  8.     <asp:Label ID="lab" runat="server">
  9.     <%#DataBinder.Eval(Container.DataItem,"City") %>,<%#DataBinder.Eval(Container.DataItem,"Designation")%><br />________________________
  10.     </asp:Label>
  11.     </td>
  12.     </tr>
  13.  
  14.  
  15.     </ItemTemplate>
  16.     </asp:Repeater>


Following code behind page:
Expand|Select|Wrap|Line Numbers
  1. da_que = "Select Sno, F_Name,City,Designation from tbl_Mem_detail where State='" + DropDownList1.SelectedItem.ToString() + "' and City ='" + DropDownList2.SelectedItem.ToString() + "'";
  2.     da = new SqlDataAdapter(da_que, con);
  3.     da.Fill(ds, "gvdata");
  4.  Repeater1.DataSource = ds.Tables["gvdata"];
  5.    Repeater1.DataBind();



So I want to perform, when I clicked in F_name link, then new window open and I want to carry one value that is sno.

So please help how to do this.

Thank You
Dec 14 '11 #1
2 3141
kadghar
1,295 Expert 1GB
Just open the window and create a SESSION variable with the value you want to carry.

Let the new window read the SESSION variable and you're done.

Perhaps the difficult part here is to actually open a new window via C# code, but here's a little hint:

Expand|Select|Wrap|Line Numbers
  1.            Response.Write("<script>" & vbCrLf)
  2.             Response.Write("parent.location.replace('otherPage.aspx');")
  3.             Response.Write(vbCrLf & "</script>")
It's in VB.Net so just add the semicolons and replace the line break constants.
Dec 14 '11 #2
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" >
  2.     <HeaderTemplate>
  3.         <table>
  4.     </HeaderTemplate>
  5.     <ItemTemplate>
  6.         <tr>
  7.             <td>
  8.                 <asp:Label ID="Sno" runat="server" Style="display: none" Text='<%#DataBinder.Eval(Container.DataItem,"Sno") %>' />
  9.                 <asp:HyperLink ID="fNameLink" runat="server" NavigateUrl='#'
  10.                     Text='<%#DataBinder.Eval(Container.DataItem,"F_Name") %>' />
  11.                 <asp:Label ID="lab" runat="server">
  12.                     <%#DataBinder.Eval(Container.DataItem,"City") %>,<%#DataBinder.Eval(Container.DataItem,"Designation")%><br />________________________
  13.                 </asp:Label>
  14.             </td>
  15.         </tr>
  16.     </ItemTemplate>
  17.     <FooterTemplate>
  18.         </table>
  19.     </FooterTemplate>
  20. </asp:Repeater>
Here is the page code:
(vb.net)
Expand|Select|Wrap|Line Numbers
  1.     Private _dt As System.Data.DataTable
  2.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  3.         If Session("_dt") Is Nothing Then
  4.             RetrieveDataSource()
  5.         Else
  6.             _dt = DirectCast(Session("_dt"), System.Data.DataTable)
  7.         End If
  8.     End Sub
  9.     Private Sub WebForm1_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
  10.         Repeater1.DataSource = _dt
  11.         Repeater1.DataBind()
  12.     End Sub
  13.  
  14.     Protected Sub Repeater1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs)
  15.         Dim sNo As Label = e.Item.FindControl("Sno")
  16.         Dim fnamelink As HyperLink = e.Item.FindControl("fnamelink")
  17.         If sNo IsNot Nothing AndAlso fnamelink IsNot Nothing Then
  18.             fnamelink.Attributes.Add("onclick", "javascript:window.open('WebForm2.aspx?Sno=" + sNo.Text + "','popup','target=_blank,width=200,height=100');")
  19.         End If
  20.     End Sub
  21.  
  22.     Private Sub RetrieveDataSource()
  23.         _dt = New System.Data.DataTable
  24.         _dt.Columns.Add("Sno", GetType(Integer))
  25.         _dt.Columns.Add("City")
  26.         _dt.Columns.Add("F_Name")
  27.         _dt.Columns.Add("Designation")
  28.  
  29.         For i As Integer = 1 To 10
  30.             Dim dr As System.Data.DataRow = _dt.NewRow
  31.             dr("Sno") = i
  32.             dr("City") = "City " + i.ToString
  33.             dr("F_Name") = "Name " + i.ToString
  34.             dr("Designation") = "Designation " + i.ToString
  35.             _dt.Rows.Add(dr)
  36.         Next
  37.         Session("_dt") = _dt
  38.     End Sub
(C#)
Expand|Select|Wrap|Line Numbers
  1.   System.Data.DataTable _dt;
  2.         protected void Page_Load(object sender, EventArgs e)
  3.         {
  4.             if (Session["_dt"] == null) { RetrieveDataSource(); }
  5.             else { _dt = (System.Data.DataTable)Session["_dt"]; }
  6.         }
  7.  
  8.         protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
  9.         {
  10.             Label sNo = (Label)e.Item.FindControl("Sno");
  11.             HyperLink fnamelink = (HyperLink)e.Item.FindControl("fnamelink");
  12.             if (sNo != null && fnamelink != null)
  13.             {
  14.                 fnamelink.Attributes.Add("onclick", "javascript:window.open('WebForm2.aspx?Sno=" + sNo.Text + "','popup','target=_blank,width=200,height=100');");
  15.             }
  16.         }
  17.  
  18.         void Page_PreRender(object sender, EventArgs e)
  19.         {
  20.             Repeater1.DataSource = _dt;
  21.             Repeater1.DataBind();
  22.         }
  23.  
  24.         private void RetrieveDataSource()
  25.         {
  26.             _dt = new System.Data.DataTable();
  27.             _dt.Columns.Add("Sno", typeof(int));
  28.             _dt.Columns.Add("City");
  29.             _dt.Columns.Add("F_Name");
  30.             _dt.Columns.Add("Designation");
  31.  
  32.             for (int i = 1; i < 10; i++)
  33.             {
  34.                 System.Data.DataRow dr = _dt.NewRow();
  35.  
  36.                 dr["Sno"] = i;
  37.                 dr["City"] = "City " + i.ToString();
  38.                 dr["F_Name"] = "Name " + i.ToString();
  39.                 dr["Designation"] = "Designation " + i.ToString();
  40.                 _dt.Rows.Add(dr);
  41.             }
  42.             Session["_dt"] = _dt;
  43.         }

My WebForm2.aspx page is very simple. It displays the Sno selected. Here is the ASP markup for the page:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.     <title></title>
  5. </head>
  6. <body>
  7.     <form id="form1" runat="server">
  8.     <div>
  9.         You selected Sno:
  10.         <asp:Literal ID="snoSelected" runat="server" />
  11.     </div>
  12.     </form>
  13. </body>
  14. </html>
Here is the code behind for the page.
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Public Class WebForm2
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         snoSelected.Text = Request.QueryString("Sno")
  6.     End Sub
  7.  
  8. End Class
(C#)
Expand|Select|Wrap|Line Numbers
  1. namespace WebApplication2
  2. {
  3.     public partial class WebForm2 : System.Web.UI.Page
  4.     {
  5.         protected void Page_Load(object sender, EventArgs e)
  6.         {
  7.             snoSelected.Text = Request.QueryString["Sno"];
  8.         }
  9.     }
  10. }
-Frinny
Dec 16 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: red | last post by:
mouse events when the mouse is on a "child control" hi everyone; my problem: I have a userControl in this usercontrol, I have a child control (a button) when the mouse moves over the...
1
by: h | last post by:
I have a custom control that consist of one or more custom child controls (I made a custom List control, where each list item is a group of a text and button controls). When I resize one "child"...
0
by: Normie Smith | last post by:
Hello... I have a strange situation happening, and I thought maybe I could get some assistance. I have created a simple user control (.ascx) file that's part of my default.aspx page. The user...
2
by: Niclas Lindblom | last post by:
Hi, I have a datagrid with linkbuttons. I would like to catch the click event when a link button has been clicked and use the string from the text value of the link button in a clientside Java...
1
by: Mauritsius | last post by:
I have a simple page where I would like to modify a repeater (bounded to a dataset) if a button (outside the repeater) is clicked or not. I tried to solve this with a button click event that...
1
by: GTDriver | last post by:
I'm trying to determine how to create a page with data from the database and I want to use the link button to have the user click on. Once the user clicks on the link button I want to transfer...
1
by: Jonah Olsson | last post by:
Hello, I'm trying to build an "add-on" to an already existing custom web user control. The old control collects some user data and saves it to a database. The new control should collect some...
4
by: Kurt Schroeder | last post by:
I am trying to add a link button to a calendar. this is a simple example: Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles...
3
by: Shimon Sim | last post by:
I put linkbutton in a repeater header. I attached event handler in makeup as onclick="btnSort_Click". Made btnSort_Click method public. It doesn't fire if I click on it. I tried to attach it in...
1
by: Imran Aziz | last post by:
Hello All, I have an asp:linkbutton in a repeater control, what I want to do is when someone clicks the link button I should get a value for the link button, and accordingly do some action. How...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.