473,394 Members | 1,829 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,394 software developers and data experts.

I want to remove user control dynamically.I have added same user control many times.

//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.ConfigurationMa nager.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//----------------


Collapse
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());

}


}
Mar 16 '11 #1
0 1008

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

Similar topics

5
by: Jon B | last post by:
Hi There! How to handle the events of a dynamically added user control? e.g. I have following code... Dim myUserControl as Object = LoadControl("myFirstControl.ascx") myFirstControl fires...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
0
by: jburnage | last post by:
I need help in getting the values from a dynamically added user control when a user submits a form. The controls are added to a placeholder and this all works fine - but its looping through the...
9
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview...
4
by: Jenni.Haughton | last post by:
I have a solution with 2 projects in it. I need to programatically add a user control from one project into the other one (as you cannot declare this normally on the form because it is in a...
0
by: boysenberry | last post by:
Hi. I have a user control which I need to add to a page several times. This works fine and the user control provides the functionality I need. However, I also need to access properties and methods...
1
by: Shraddha | last post by:
Hi, I am adding some ASP.Net user controls (.ascx file) dynamically on the button click. The user control will get added as many times userhits the button. Now on the click of the submit button, I...
3
by: ata | last post by:
Hi folks, Consider the following code: protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; if (row.RowType != DataControlRowType.DataRow)...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.