473,498 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Event handlers for Dynamic created Controls in DatGri

I have a link button "lButton" created dynamically at the item databound
event of the data grid "datagrid1" in the footer.so i want to execute the
item command event of the datagrid on clicking the dynamicaly created link
button, but when at runtime i click the dymanic created control it disapears
from the datagrid footer and then it doesnt fire the itemcommand event of the
datagrid.

I am pasting below the pieces of my code for your better understanding.
//creating the dynamic control "lButton" at the footer in the ITEM DATABOUND
EVENT and assigning th events to it.
private void datagrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Footer)
{
LinkButton lButton=new
LinkButton();
lButton.Text ="Add Details
here";
lButton.ID ="flagbutton";
lButton.CommandName="flagButton";
lButton.Click +=new System.EventHandler(this.lButton_Click);
e.Item.Cells[2].Controls.Add(lButton) ;

}
}

//in the ITEM COMMAND EVENT, executing the a logic when teh link button in
the grid gets clciked
private void datagrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(String.Compare(e.CommandName,"flagButton",true) ==0)
{
Response.Write("done");
}
}
//Event TO BE EXECUTED BY DYNAMIC CREATED CONTROL lButton
private void lButton_Click(object sender, System.EventArgs e)
{
DataGridCommandEventArgs dgcea = (DataGridCommandEventArgs)e;
datagrid1_ItemCommand(sender, dgcea);
}

so could anyone of you help me out in fixing the problem . and the reson for
this type of cause.

Your expert advice in sorting out thsi issue woudl be highly appreciated.

Rgds
Shiju

Nov 19 '05 #1
2 1528
You need to populate the datagrid on postback so that the button is
recreated,

If the button is not recreated then the event will not be wired up

Nov 19 '05 #2
A few things come to mind:

*Why do you need to dynamically add the linkbutton to the datagrid?
Why can't you place the linkbutton into the datagrid at design time and
then simply set its visibility based on the situtation? Handling
events for controls added at runtime can be tricky as you need to pay
close attention to the sequence of page and control events.

*Is there anywhere in your code or tags where you specify that the
onItemCommand event for the datagrid will be handled by your procedure
datagrid1_ItemCommand?

*Is the viewstate on your datagrid enabled?

*If you intend to trap and process the click event for your linkbutton
in the datagrid's ItemCommand event, why are you assigning a different
event handler (lButton_Click) to handle it?

*At what point in the page are you binding data to the datagrid and how
often? Are you rebinding with each postback?

Bill E.
Hollywood, FL

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3556
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
1
1961
by: Lamont Adams | last post by:
Hi all, I've created numerous custom controls of varying complexity, but I've been on this problem for a day and a half, and I can't figure this mystery out. I hope one of you kind folks can...
10
3980
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
3
1948
by: Kris Palmer | last post by:
hi, can somebody explain this problem? it's driving me crazy! i have a requirement to dynamically create a variable quantity of timers with associated start button based on the contents of a...
6
2833
by: hlubocky | last post by:
I thought I had a good grasp of the problem related to dynamically creating controls, but it appears that as my application grew in complexity, the problem has resurfaced. As I understand it, in...
3
2047
by: matt.grande | last post by:
Please, I REALLY need help with this one. I've been banging my head about it for three days now. Here's the situation. I have a form that runs some queries and functions and such. When all...
4
1962
by: Spectre1337 | last post by:
Hello, I'm having huge difficulties solving what should be a relatively trivial problem. The following is a gross simplification (obviously it's not that simple in reality) but it will serve its...
1
1835
by: Dica | last post by:
i've got a script that loops through a dataset and creates dynamic web controls and event handlers: while (oDr.Read()){ RadioButton oRb = new RadioButton(); oRb.ID = oDr.ToString();...
15
1366
by: Chris | last post by:
Hi, When choosing a value in the dropdownlist, i try to put that selectedvalue in the label, but i always get the first value 'a'. EnableViewState is by default set to true, so ... what's wrong...
0
7004
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
7167
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
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
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
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.