Friday, November 8, 2013

HyperLink in Reapeater Control ASP.NET

Add a HyperLink to a  Reapeater Control  in ASP.NET


Place a Repeater control on a form.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
        <table>
        <th>ID</th>
           <th>name</th>
              <th>marks</th>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
        <td>            <asp:Label ID="Label1" runat="server" Text='<%#Eval("rollno")%>'></asp:Label></td>

          <td> <%#Eval("name") %></td>
         <td>  <%#Eval("marks") %></td>
         <td>
             <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl ='<%#DataBinder.Eval(Container.DataItem,"rollno","show.aspx?id={0}")%>'>Go</asp:HyperLink></td>
       </tr> </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>

</html>


Now Create a connect class


public class Connect
{
    SqlConnection con;
public Connect()
    {
     con   = new SqlConnection("Data Source=.;Initial Catalog=[DatabaseName];Integrated Security=True");
      
}

    public DataTable Select()
    {
        SqlDataAdapter ad = new SqlDataAdapter("select * from stud", con);
        DataSet ds = new DataSet();
        ad.Fill(ds, "stud");
        return ds.Tables[0];
    }
}


In the code behind

public partial class repeater : System.Web.UI.Page
{
    Connect c = new Connect();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }

    private void bind()
    {
        
        Repeater1 .DataSource =c.Select();
        DataBind();
    }
}

Create another Webform named show.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1> </h1>
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

Just retrieve the Query string aand display it

public partial class show : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "RollNo: " + Request.QueryString["id"];
    }
}




If you want you can provide more styles to repeater. Enjoy!!!

0 comments:

Post a Comment