473,394 Members | 1,721 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.

get id of dynamically created button

i have created my buttons dynamically as well as thier event handlers as below

Expand|Select|Wrap|Line Numbers
  1. TableRow tr = new TableRow();
  2.             TableCell td1 = new TableCell();
  3.             Label _label = new Label();
  4.  
  5.             _label.Style.Add("align", "left");
  6.             _label.ID = "lbl" + i.ToString();
  7.             _label.Text = lb.Items[i].Text.ToString();
  8.             td1.Controls.Add(_label);
  9.  
  10.             TableCell td2 = new TableCell();
  11.             Button _button = new Button();
  12.             _button.ID = "btn_" + i.ToString();
  13.             _button.Text = "Edit Recipient";
  14.             _button.CommandName = i.ToString();
  15.             _button.Click += new EventHandler(btn_Click);
  16.  
  17.             td2.Controls.Add(_button);
  18.  
  19.             tr.Cells.Add(td1);
  20.             tr.Cells.Add(td2);
  21.  
  22.             recipientTable.Rows.Add(tr);
i want get the id of the button clicked in the below handler of the button
kindly tell me how can i do that
Expand|Select|Wrap|Line Numbers
  1. protected void btn_Click(object sender, EventArgs e)
  2.     {
  3.  
  4.     }
Dec 2 '09 #1

✓ answered by Frinavale

Well, the scope of your button is not correct.

You need to declare the button as part of the page, you cannot declare the button within a function and expect it to work. If you do, then the button will render on the page but the life span of the button is within the function so any events (like the
click event) that the button does may not actually be detected properly.

Another thing is that you have to instantiate the button in the PageInit() event. The reason is because the PageInit event occurs just before the ViewState for the page is loaded. When the ViewState is for the page is loaded, the ViewState for all of the controls on the page is loaded.

The ViewState contains state information about the control. If your dynamic control was a TextBox, the text that the user entered into the TextBox is loaded into the Server Side TextBox object when the ViewState is loaded for this dynamic TextBox happens.

Likewise, if you have a dynamic button and the user clicks the button, the button click event is detected when the ViewState is loaded for the button control.

If you do not instantiate your dynamic controls in the Page Init event then any events or other ViewState data will not properly be loaded and your code wont work as you expect it.

If you need to get the control by ID then you need to give the control an ID after you instantiate it.

Please see this article for more information: http://bytes.com/topic/net/insights/...ntrols-asp-net

The repeater is actually a very good idea because it uses the INamingContainer interface. This interface will properly assign unique IDs to every dynamic control within the control that implements it. That is why Semomaniz's suggestion about using a Repeater is such a good idea.

Another control that implements the INamingContainer is the GridView control.

Both the Repeater and the GridView can be used to specify controls (like TextBoxes, Labels, Buttons, LinkButtons etc) that should be used to display and interact with the data that the GridView/Repeater is bound to. You do this using something referred to as a "template"

This means that all you have to do is create your table. This table will become the Data Source for the GridView or the Repeater. When you use the DataBind() method on the GridView or the Repeater the data will be displayed according to the template that you have outlined in the GridView/Repeater. This is particularly nice because it keeps a clear separation between the User Interface and the Data layers. Using this technique the user interface and data source are not merged together. It makes life a lot easier for maintaining your application in the future.

If your GridView/Repeater has a button in it's template, then each row of data will have a button assigned to it (dynamically). The controls within the template are all given unique IDs because both the GridView and the Repeater implement the INamingInterface. When the user clicks one of these buttons you can determine what row that the button belongs to etc etc.


-Frinny

6 8774
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Dec 2 '09 #2
tlhintoq
3,525 Expert 2GB
Cast the sending object back to a button then get the property of it.

Expand|Select|Wrap|Line Numbers
  1. string ButtonName = ((Button)sender).Name;
Dec 2 '09 #3
semomaniz
210 Expert 100+
as far as i understand you are using a for statement to generate a table with controls and data. I would suggest you use a repeater control to do so as it is much easier to work with.

In you case following code should work for you. Place the code in you button click event.
Expand|Select|Wrap|Line Numbers
  1.  Button bt = (Button)sender;
  2.  lbltest.Text = bt.ID.ToString();   //lbltest is just a label to display the id
  3.  
Dec 2 '09 #4
semomaniz
210 Expert 100+
Expand|Select|Wrap|Line Numbers
  1.  ((Button)sender).Name
Does it really work tried in VS and couldnt find the Name property for the sender. How ever i could get the Id by

Expand|Select|Wrap|Line Numbers
  1.  ((Button)sender).ID  
Dec 2 '09 #5
Frinavale
9,735 Expert Mod 8TB
Well, the scope of your button is not correct.

You need to declare the button as part of the page, you cannot declare the button within a function and expect it to work. If you do, then the button will render on the page but the life span of the button is within the function so any events (like the
click event) that the button does may not actually be detected properly.

Another thing is that you have to instantiate the button in the PageInit() event. The reason is because the PageInit event occurs just before the ViewState for the page is loaded. When the ViewState is for the page is loaded, the ViewState for all of the controls on the page is loaded.

The ViewState contains state information about the control. If your dynamic control was a TextBox, the text that the user entered into the TextBox is loaded into the Server Side TextBox object when the ViewState is loaded for this dynamic TextBox happens.

Likewise, if you have a dynamic button and the user clicks the button, the button click event is detected when the ViewState is loaded for the button control.

If you do not instantiate your dynamic controls in the Page Init event then any events or other ViewState data will not properly be loaded and your code wont work as you expect it.

If you need to get the control by ID then you need to give the control an ID after you instantiate it.

Please see this article for more information: http://bytes.com/topic/net/insights/...ntrols-asp-net

The repeater is actually a very good idea because it uses the INamingContainer interface. This interface will properly assign unique IDs to every dynamic control within the control that implements it. That is why Semomaniz's suggestion about using a Repeater is such a good idea.

Another control that implements the INamingContainer is the GridView control.

Both the Repeater and the GridView can be used to specify controls (like TextBoxes, Labels, Buttons, LinkButtons etc) that should be used to display and interact with the data that the GridView/Repeater is bound to. You do this using something referred to as a "template"

This means that all you have to do is create your table. This table will become the Data Source for the GridView or the Repeater. When you use the DataBind() method on the GridView or the Repeater the data will be displayed according to the template that you have outlined in the GridView/Repeater. This is particularly nice because it keeps a clear separation between the User Interface and the Data layers. Using this technique the user interface and data source are not merged together. It makes life a lot easier for maintaining your application in the future.

If your GridView/Repeater has a button in it's template, then each row of data will have a button assigned to it (dynamically). The controls within the template are all given unique IDs because both the GridView and the Repeater implement the INamingInterface. When the user clicks one of these buttons you can determine what row that the button belongs to etc etc.


-Frinny
Dec 2 '09 #6
thanx everybody for the replies.
Dec 3 '09 #7

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

Similar topics

1
by: teddy | last post by:
I am trying to create a bookstore WebApplication using c#. I can display the books (obtained from sql server) on a table in a WebForm. At the end of each row, I would like to add "add to cart"...
3
by: What-a-Tool | last post by:
"btnExit is not a member of frmMain" message in the AddHandler Line. What do I need to do other than the AddHandler? 'Create Exit button btnExit.Location = New Point(425, 565) ...
3
by: Gary Kahrau | last post by:
If I create a form dynamically, is there some way I can point to a dll function/ sub when the dynamic button is pushed? Or do something like this. dim DynSub as string DynSub = "DoItSub" ...
7
by: rsaffy | last post by:
I am having trouble with my dynamically created button's event handling. I read that the buttons need to be recreated on every trip to the server, but how exactly do you do that when the datagrid...
0
by: aparnaa | last post by:
hi i am developing a web page in asp.net(VB) here i have dynamicaaly created a button. i have created this button in a procedure so that i can call it at varied places. however, i am not able to...
2
by: jasinx | last post by:
When I run this code it creates a button on an .aspx page. When I click the button it seems to fire the events but I'm unable to capture them in the function in the ascx codebehind. Any help? Thank...
4
smiley22
by: smiley22 | last post by:
i would just like to ask something. i created a table using innerhtml. i placed a button in each rows. for example if the table had 10 rows, with 10 buttons in each how will i determine...
6
by: likhin M | last post by:
Hi everyone, I am developing a webpage using only javascript.And I am dynamically creating buttons inside javascript as follows document.write('<button name=""...
0
by: Parimaladevi | last post by:
hi, i want to know how to add a eventhandler for a button control that is created dynamically inside Template Column of a Datagrid in Silverlight application. Click event is not fired and it...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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,...

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.