Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i create tow web page one that has dynamic crest for link-button in repeater and each link button it name is based on name for record stored in column name in database if new data insert into that table in data base on my web page dynamically creat new link-button it is name like the name that stored in data base
When click on link button I want retrieve all information for that name


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Messaging;
using System.Net;
using System.Net.Mail;


namespace exam
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["seniorConnectionString"].ConnectionString);
            con.Open();

            SqlCommand com = new SqlCommand("select*from dbo.[Suporvisor(commtiee)]", con);
             
            Page v = (Page)Context.Handler;

            
            SqlDataReader dr;
            dr = com.ExecuteReader();
            if (dr.HasRows == true)
            {
                dr.Read();
                Label1.Text = dr["S_name"].ToString();
                Label7.Text = dr["SID"].ToString();
                Label2.Text = dr["phone"].ToString();
                Label3.Text = dr["E_mail"].ToString();
                Label4.Text = dr["Department"].ToString();
                Label6.Text = dr["Password"].ToString();
                dr.Close();
            }
            con.Close();

            List<Label> labels = new List<Label>();
            for(int i =0; i <= 6; i++)
            {
                Label label = new Label();
                labels.Add(label);
            }
         


            

            con.Open();
            com.ExecuteNonQuery();
            SqlDataAdapter sda = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            com.ExecuteNonQuery();
            con.Close();
            
        }
        private String button1WasClick = "false";
        public static void SendEmail(String emailbody)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            using (MailMessage mailMessage = new MailMessage(ConfigurationManager.AppSettings["SMTPuser"],Label3.Text))
            {

                mailMessage.Subject = Label1.Text;
                mailMessage.Body = "Hello amal you can log in the system This your ID:18819 username :amal";
                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.EnableSsl = true;
                    smtp.Host = ConfigurationManager.AppSettings["Host"];
                    smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnabelSSL"]);
                    NetworkCredential networkCred = new NetworkCredential(ConfigurationManager.AppSettings["SMTPuser"], ConfigurationManager.AppSettings["SMTPpassword"]);
                    
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = networkCred;
                    smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
                    smtp.Send(mailMessage);
                    ClientScript.RegisterStartupScript(GetType(), "alter", "alter(Email sent);", true);
                }
            }


 




        }
        private bool button2WasClick = false;
        protected void Button2_Click(object sender, EventArgs e)
        {
            button2WasClick = true;
            Response.Write("Email masseg is sent Succesfully");
            using (MailMessage mailMessage = new MailMessage(ConfigurationManager.AppSettings["SMTPuser"], "monaxshroog@gamil.com"))
            {

                mailMessage.Subject = Label1.Text;
                mailMessage.Body = "Hello amal your request to be a suporvisor for senior project is not agread by Coordenator for mor daital :<br/>";
                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.EnableSsl = true;
                    smtp.Host = ConfigurationManager.AppSettings["Host"];
                    smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnabelSSL"]);
                    NetworkCredential networkCred = new NetworkCredential(ConfigurationManager.AppSettings["SMTPuser"], ConfigurationManager.AppSettings["SMTPpassword"]);

                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = networkCred;
                    smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
                    smtp.Send(mailMessage);
                    ClientScript.RegisterStartupScript(GetType(), "alter", "alter(Email sent);", true);
                }
            }
        }
    }
}


What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace exam
{
    public partial class RegistrationTransaction : System.Web.UI.Page
    {

        SqlConnection cnn = new SqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {

            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["seniorConnectionString"].ConnectionString;
            if (cnn.State == ConnectionState.Closed)
            {
                cnn.Open();
            }
            SqlCommand cm = new SqlCommand("select S_name from dbo.[Suporvisor(commtiee)] ", cnn);
            cm.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet dt = new DataSet();
            da.Fill(dt);
            Repeater Repeater1 = new Repeater();
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            if (cnn.State == ConnectionState.Open)
            {
                cnn.Close();
            }
            foreach (RepeaterItem repeatItem in Repeater1.Items)
            {
                // if condition to add HeaderTemplate Dynamically only Once
                if (repeatItem.ItemIndex == 0)
                {
                    RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
                    HtmlGenericControl hTag = new HtmlGenericControl("h4");
                    hTag.InnerHtml = "Suporvisor Rigestration Transaction";
                    repeatItem.Controls.Add(hTag);
                }

                // Add ItemTemplate DataItems Dynamically
                //SqlDataReader dr = Int32.Parse;
                RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
                LinkButton lnk = new LinkButton();
                lnk.ID = "lnk_itemname";

                lnk.Text = String.Format("{0}", (dt.Tables[0].Rows[repeatItem.ItemIndex]["S_name"]));
                lnk.Click += new EventHandler(click);
                HttpContext H = HttpContext.Current;
                String name = H.Request[lnk.Text];
                 
                repeatItem.Controls.Add(lnk);
                // Add SeparatorTemplate Dynamically
                repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
                LiteralControl ltrlHR = new LiteralControl();
                ltrlHR.Text = "<hr />";
                repeatItem.Controls.Add(ltrlHR); SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["seniorConnectionString"].ConnectionString);
                con.Open();

                SqlCommand com = new SqlCommand("select*from dbo.[Suporvisor(commtiee)]", con);

                SqlDataReader dr;
                dr = com.ExecuteReader();
                if (dr.HasRows == true)
                {
                    while (dr.Read()) 
                        if (lnk.Text.Equals("S_name"))
                        {
                            Server.Transfer("~/SuporvisorRegistTran.aspx");

                        }

                    Server.Transfer("~/SuporvisorRegistTran.aspx");

                }


            }

            // Add Repeater Control as Child Control

            // of Panel Control

            Panel1.Controls.Add(Repeater1);





            PlaceHolder1.Controls.Add(Panel1);




        }
        
        
        protected void click (object sender , EventArgs e)
        {
            
            
            Response.Redirect("~/SuporvisorRegistTran.aspx");
        }

         

         
    }
}
Posted
Updated 27-Feb-20 5:30am
Comments
Richard MacCutchan 27-Feb-20 10:18am    
Why not use one of the View classes that can dynamically show the columns you want?
Member 14757261 27-Feb-20 10:21am    
Can you write example on my code
Richard MacCutchan 27-Feb-20 10:25am    
Take a look at some of the examples at codeproject:datagridview webform - Google Search[^].
Member 14757261 27-Feb-20 10:50am    
Thank you , I just wanna see what is wrong on my code
Richard MacCutchan 27-Feb-20 11:05am    
It is not clear what your problem is. Your question mentions using Buttons, but your code is using Labels.

1 solution

Here is an exaple of how to create a repeater: Repeater Class (System.Web.UI.WebControls) | Microsoft Docs[^]. You just need to add the text from your database in the relevant parts.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900