我在使用动态控件时遇到问题 [英] I am having problem with Dynamic Controls

查看:67
本文介绍了我在使用动态控件时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用动态控件时遇到问题,单击按钮即可添加动态控件.但是我无法删除控件.删除按钮在用户控件中.请帮助我.

//Default.aspx.cs//---------

I am having problem with Dynamic Controls,I am able to add dynamic control on the click of a button.But I am not able to remove the Control.Delete button is in the user control.Please Help me.

//Default.aspx.cs//---------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    SqlCommand cmd,cmd1;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.NumberOfControls = 1;
            this.addcontrol();
        }
        else
        {
            if (null != Request.Form["btnsubmit"])
            {
                this.addcontrol();
            }
            else
            {
                this.NumberOfControls = this.NumberOfControls + 1;
                this.addcontrol();
            }
        }
           }

    public void addcontrol()
    {
        //j = j + 40;
        int count = this.NumberOfControls;
        int count1 = Convert.ToInt32(lbldelete.Text);
        Control Item1;
        for (int i = 1; i <= count; i++)
        {
            Item1 = LoadControl("my.ascx");
            Item1.ID = "Item" + Convert.ToString(i + count1);
            Item1.RemoveUserControl +=      new EventHandler(Item1_RemoveUserControl);
            //pnlContainer.Controls.Add(Item);
            PlaceHolder1.Controls.Add(Item1);
        }
        //this.NumberOfControls = this.NumberOfControls + 1;
    }
    protected int NumberOfControls
    {
        get {
            return (int)ViewState["Numcontrols"];
        }
        set {
            ViewState["Numcontrols"] = value;
        }
    }
    public void Item1_RemoveUserControl(object sender, EventArgs e)
    {
        Button but = sender as Button;
        System.Web.UI.Control Item1 = (System.Web.UI.Control)but.Parent;
        PlaceHolder1.Controls.Remove(Item1);
        lbldelete.Text = (Convert.ToInt32(lbldelete.Text) + 1).ToString();
        this.NumberOfControls = this.NumberOfControls - 1;
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        
            string iclass, iactive;
            
            foreach (my Item in PlaceHolder1.Controls)
            {
                int count = 0;
                iclass = Item.itemclass;
                iactive = Item.active;
                SqlDataReader reader = null ;
                try
                {
                    //reader = null;
                    String retrieve = "Select class from sample1 where class=''" + iclass + "''";
                    cmd1 = new SqlCommand(retrieve, sqlcon); 
                    sqlcon.Open();
                    reader = cmd1.ExecuteReader();
                    while (reader.Read())
                    {
                        count++;
                    }
                }
                finally
                {
                    if (count != 0)
                    {
                       // MessageBox.Show("Class Already there");
                        reader.Close();
                        sqlcon.Close();
                    }
                    else
                    {
                        string sqlstr = "Insert into sample1(class,active) values (''" + iclass + "'',''" + iactive + "'')";
                        cmd = new SqlCommand(sqlstr, sqlcon);
                        sqlcon.Close();
                        sqlcon.Open();
                        cmd.ExecuteNonQuery();
                        Response.Write(iclass + " added ");
                        sqlcon.Close();
                    }
                }
            }
        
        

    }   
    
}





//my.ascx.cs//----------------






//my.ascx.cs//----------------


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;

public partial class my : System.Web.UI.UserControl
{
   public string itemclass
   {
       get
       {
           return txtclass.Text;
       }
       set
       {
           txtclass.Text=value;
       }
   }
   public string active {
       get {
           return rdactive.SelectedValue.ToString();
       }
       set {
           rdactive.SelectedValue = value;
       }
   }
   public event EventHandler RemoveUserControl;

   protected void Delete_Click(object sender, EventArgs e)
   {
       if (this.RemoveUserControl != null)
           this.RemoveUserControl(this, new EventArgs());

   }


}

推荐答案

请确保您没有在此部分中删除错误的项目:

Make sure you are not removing the wrong item in this part:

Button but = sender as Button;
//are you sure Parent is a reference to your added control??
//you should break here with the debugger and check its type (your UserControl) 
System.Web.UI.Control Item1 = (System.Web.UI.Control)but.Parent;
PlaceHolder1.Controls.Remove(Item1);


这篇关于我在使用动态控件时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆