Simple Web User Control dynamic parts ...
-
Hi, I am experienced in C#, but new in ASP.NET. I cannot understand Web page life cycle. If I understand correctly, Web page class is destroyed every time you press the button or something. So I store my properties in ViewState. but there is a problem. I use TextBox to get keyword, which I use for database records retrieval. When i retrieve database records I add dynamically new rows to table with hrefs. private void LoadData() { baseTechPartArray mParts = DataBase3.Get_baseTechPartArray_By_Keyword(KeyWord); for (int i = 0; i <= mParts.Count - 1; i++) { TableRow mRow = new TableRow(); TableCell cellPickup = new TableCell(); TableCell cellName = new TableCell(); TableCell cellCode = new TableCell(); mRow.Cells.Add(cellPickup); mRow.Cells.Add(cellName); mRow.Cells.Add(cellCode); ContainerTable.Rows.Add(mRow); LinkButton hrefName = new LinkButton(); hrefName.Text = mParts.Get(i).name; hrefName.Attributes["record_id"] = mParts.Get(i).id.ToString(); LinkButton hrefCode = new LinkButton(); hrefCode.Text = mParts.Get(i).code; hrefCode.Attributes["record_id"] = mParts.Get(i).id.ToString(); ImageButton mPickupButton = new ImageButton(); mPickupButton.BorderWidth = 0; mPickupButton.ImageUrl = "../Resources/pickup.gif"; mPickupButton.Attributes["record_id"] = mParts.Get(i).id.ToString(); cellPickup.Controls.Add(mPickupButton); cellName.Controls.Add(hrefName); cellCode.Controls.Add(hrefCode); mPickupButton.Click += new ImageClickEventHandler(mPickupButton_Click); hrefCode.Click += new EventHandler(hrefCode_Click); hrefName.Click += new EventHandler(hrefName_Click); cellPickup.Attributes["width"] = "1%"; cellCode.Attributes["width"] = "30%"; } } but Click events does not work until I add Load() method to PageLoad or LoadViewState. But then some of table rows are still from first page load, I try to clear them, but it does`nt help. What simple framework or principle should I use 1)to get work simple onevent -> 2)add dynamic elements with their events -> 3)use dynamically added control events?
-
Hi, I am experienced in C#, but new in ASP.NET. I cannot understand Web page life cycle. If I understand correctly, Web page class is destroyed every time you press the button or something. So I store my properties in ViewState. but there is a problem. I use TextBox to get keyword, which I use for database records retrieval. When i retrieve database records I add dynamically new rows to table with hrefs. private void LoadData() { baseTechPartArray mParts = DataBase3.Get_baseTechPartArray_By_Keyword(KeyWord); for (int i = 0; i <= mParts.Count - 1; i++) { TableRow mRow = new TableRow(); TableCell cellPickup = new TableCell(); TableCell cellName = new TableCell(); TableCell cellCode = new TableCell(); mRow.Cells.Add(cellPickup); mRow.Cells.Add(cellName); mRow.Cells.Add(cellCode); ContainerTable.Rows.Add(mRow); LinkButton hrefName = new LinkButton(); hrefName.Text = mParts.Get(i).name; hrefName.Attributes["record_id"] = mParts.Get(i).id.ToString(); LinkButton hrefCode = new LinkButton(); hrefCode.Text = mParts.Get(i).code; hrefCode.Attributes["record_id"] = mParts.Get(i).id.ToString(); ImageButton mPickupButton = new ImageButton(); mPickupButton.BorderWidth = 0; mPickupButton.ImageUrl = "../Resources/pickup.gif"; mPickupButton.Attributes["record_id"] = mParts.Get(i).id.ToString(); cellPickup.Controls.Add(mPickupButton); cellName.Controls.Add(hrefName); cellCode.Controls.Add(hrefCode); mPickupButton.Click += new ImageClickEventHandler(mPickupButton_Click); hrefCode.Click += new EventHandler(hrefCode_Click); hrefName.Click += new EventHandler(hrefName_Click); cellPickup.Attributes["width"] = "1%"; cellCode.Attributes["width"] = "30%"; } } but Click events does not work until I add Load() method to PageLoad or LoadViewState. But then some of table rows are still from first page load, I try to clear them, but it does`nt help. What simple framework or principle should I use 1)to get work simple onevent -> 2)add dynamic elements with their events -> 3)use dynamically added control events?
-
The problem is, that everything show up good, but click events does`nt work. When I click on dynamically added hyperlinks, hyperlink click event does`nt run, but page reloads and everything is gone.
When you are adding controls dynamically to the page, you need to ensure the childcontrol exists on the other call. This you can do either by adding everytime when you call the page. protected void Page_Load(object sender, EventArgs e) { Load(); } then it works fine. let me know whether it works or not? I will on-line only.