473,406 Members | 2,849 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,406 software developers and data experts.

creating button in asp.net

Hi,
the below code is for to create dynamically buttons in asp.net. my problem is at the first time i visited the page its working but if i visited the second time that buttons is not clearing. how to clear that?

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections.Generic;
  11.  
  12. public partial class Default2 : System.Web.UI.Page
  13. {
  14.  
  15. static Button[] btn_arr = new Button[20];
  16. static int btn_count;
  17.  
  18. protected void Page_Load(object sender, EventArgs e)
  19.  {
  20.    try
  21.    {
  22.       if (btn_arr[0] is Button) {
  23.        //for each button saved in our array, recreate it
  24.        foreach (Button button in btn_arr) {
  25.          add_button(button);
  26.        }
  27.       }
  28.     }
  29.     catch (Exception ex)
  30.     {
  31.       lblStatus.Text += ex.Message.ToString();
  32.     }
  33.  }
  34.  
  35.  protected void add_button(Button button)
  36.  {
  37.   try
  38.   {
  39.       TableRow tr = new TableRow();
  40.       TableCell tc = new TableCell();
  41.    tc.Controls.Add(button);
  42.    tr.Cells.Add(tc);
  43.    ctrlTable.Rows.Add(tr);
  44.    //pnlMain.Controls.Add(new LiteralControl("<br>"));
  45.   }
  46.   catch (Exception ex)
  47.   {
  48.    lblStatus.Text += ex.Message.ToString();
  49.   }
  50.  }
  51.  
  52.  protected void btnSubmit_Click(object sender, EventArgs e)
  53.  {
  54.   try
  55.   {
  56.    //create a new instance of the control
  57.    Button new_button = new Button();
  58.    new_button.ID = txtID.Text;
  59.    new_button.ForeColor = System.Drawing.Color.FromName(txtForeColor.Text);
  60.    new_button.Text = txtText.Text;
  61.    //add button to button array
  62.    btn_arr[btn_count++] = new_button;
  63.    //call our add function
  64.    add_button(new_button);
  65.    lblStatus.Text += "Created button " + new_button.ID + " and of color " + new_button.ForeColor;
  66.   }
  67.   catch (Exception ex)
  68.   {
  69.    lblStatus.Text += ex.Message.ToString();
  70.   }
  71.  
  72.  }
  73.  
  74. }
May 19 '08 #1
1 1136
kenobewan
4,871 Expert 4TB
Sounds like you are having trouble with postbacks. When you don't want code to execute in the page load everytime there is a postback, you need to preserve it using not page.ispostback method. HTH.
May 19 '08 #2

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

Similar topics

2
by: PK | last post by:
Hello, I am looking for help on the following. I'm trying to create a custom browser toolbar button that will do a few things. One that I'm trying to do at the moment is just simply return the...
1
by: dave | last post by:
I first started using HCW.exe to compile .rtf filew created with MS Word a couple of weeks ago. I used the file | new menu then selected New project in the dialog box and everything worked as...
24
by: jonathon | last post by:
Hi all, I have a web app with a popup window for entering data. I don't want to access the web every time this window is opened, as most of the app is AJAX. But I can't figure out how to open...
2
by: Anand Sagar | last post by:
I have a Panel1 and button1 on my webform. At runtime, I create 2 textboxes. I do it at the Page_Load event. I put the code within the " If Not isPostBack" For the button click event, I will do...
2
by: DaWoE | last post by:
Hi all, I'm fairly new to ASP.NET. What i want to do is creat a online registration form. On the first step is getting the users details and the number of people he wants to register. Based on...
1
by: thechaosengine | last post by:
Hi all, Can anyone tell me what I need to do to create a 3 stage rolloever button out of an asp.net image control. I don't know too much about javascript or creating user controls. I'm really...
10
by: Tor Inge Rislaa | last post by:
Creating Control Array How to create an array of buttons, with common procedures based on the index of the control. How would this Example from VB 6.0 be in VB.NET? Private Sub...
2
by: epigram | last post by:
I'm dynamically creating a number of radio buttons on my aspx page based upon data read from a db. Each radio button has autopostback turned on. I'm experiencing two problems. 1) I am reading...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.