Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

dynamicly created controlls

$
0
0
public partial class topicos : System.Web.UI.Page
{
    static Table tableResult;

    public static class MyClass
    {
        public static int valor { get; set; }
        public static int valor2 { get; set; }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        validar();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            MyClass.valor = 0;
            MyClass.valor2 = 210;
        }

    }

    string strCon = "Data Source=localhost\\SQLEXPRESS; Initial Catalog=Dados;Integrated Security=True";
    protected void submeter_Click(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(imagens.PostedFile.FileName);

        Stream str = imagens.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(str);
        Byte[] size = br.ReadBytes((int)str.Length);
        using (SqlConnection con = new SqlConnection(strCon))
        {
            using (SqlCommand cmd = new SqlCommand())
            {

                cmd.CommandText = "insert into topicos(nome,data,tipo,URL,topico) values(@nome,@data,@tipo,@URL,@topico)";
                cmd.Parameters.AddWithValue("@nome", filename);
                cmd.Parameters.AddWithValue("@data", size);
                cmd.Parameters.AddWithValue("@URL", Convert.ToString(URL.Text));
                cmd.Parameters.AddWithValue("@topico", Convert.ToString(Topico.Text));
                cmd.Parameters.AddWithValue("@tipo", Convert.ToString(tipo.SelectedItem));

                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

        MyClass.valor++;
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + MyClass.valor + "');", true);
    }

    [WebMethod]
    protected void validar()
    {

        using (SqlConnection con = new SqlConnection(strCon))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select top(" + MyClass.valor + ") * from topicos order by id desc";

                cmd.Connection = con;
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                tableResult = new Table();
                tableResult.GridLines = GridLines.Both;
                Unit width = new Unit(115, UnitType.Pixel);
                Unit width2 = new Unit(176, UnitType.Pixel);
                Unit width3 = new Unit(289, UnitType.Pixel);
                while (dr.Read())
                {
                    TableRow ar = new TableRow();
                    TableCell tipo2 = new TableCell();
                    Label tipo = new Label();
                    tipo.Width = width;
                    tipo.ID = "tipo_" + MyClass.valor;
                    tipo.Text = dr["tipo"].ToString();
                    tipo2.Controls.Add(tipo);
                    ar.Cells.Add(tipo2);
                    TableCell URL2 = new TableCell();
                    Label URL = new Label();
                    URL.Width = width2;
                    URL.ID = "URL_"+MyClass.valor;
                    URL.Text = dr["URL"].ToString();
                    URL2.Controls.Add(URL);
                    ar.Cells.Add(URL2);

                    TableCell topico2 = new TableCell();
                    Label topico = new Label();
                    //CheckBox teste1=new CheckBox();
                    //teste1.ID = "aa";
                    //teste1.Text = "ttt";
                    //teste1.Checked = true;
                    topico.Width = width2;
                    topico.ID = "topico_" + MyClass.valor;
                    topico.Text = dr["topico"].ToString();
                    topico2.Controls.Add(topico);
                    //topico2.Controls.Add(teste1);
                    ar.Cells.Add(topico2);


                    TableCell nome2 = new TableCell();
                    Label nome = new Label();
                    nome.Width = width3;
                   // Button eliminar = new Button();
                   // eliminar.Text = "eliminar";
                   // eliminar.ID = "eliminar2";
                   // eliminar.OnClientClick = "DeleteRow(this); return false;";
                   // eliminar.Attributes.Add("onClick", "return ShowMessage()");
                   // eliminar.Attributes.Add("runat", "server");
                   // eliminar.Click += new System.EventHandler(this.ShowMessage); 
                    nome.ID = "nome_" + MyClass.valor;
                    nome.Text = dr["nome"].ToString();
                    nome2.Controls.Add(nome);
                    //nome2.Controls.Add(eliminar);
                    ar.Cells.Add(nome2);


                    TableCell eliminar2 = new TableCell();
                    Button eliminar = new Button();
                    eliminar.Text = "eliminar";
                    eliminar.ID = "eliminar_" + MyClass.valor;
                    eliminar.Attributes.Add("runat", "server");
                    eliminar.Click += new System.EventHandler(this.apagar);
                    eliminar2.Controls.Add(eliminar);
                    ar.Cells.Add(eliminar2);
                    tableResult.Rows.Add(ar);
                    resultHolder.Controls.Add(tableResult);
                }
                con.Close();

            }
        }

    }



    void apagar(Object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('teste');", true);

    }


need to create several buttons that do something at runtime, the buttons are created each time i press "add more button" problem is that dunno why but instead of creating just 1 button program creates 2 buttons with the same name :( can anyone help ? 


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>