473,624 Members | 2,534 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_ItemD ataBound(object sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
if(e.Item.ItemT ype == ListItemType.Fo oter)
{
LinkButton lButton=new
LinkButton();
lButton.Text ="Add Details
here";
lButton.ID ="flagbutton ";
lButton.Command Name="flagButto n";
lButton.Click +=new System.EventHan dler(this.lButt on_Click);
e.Item.Cells[2].Controls.Add(l Button) ;

}
}

//in the ITEM COMMAND EVENT, executing the a logic when teh link button in
the grid gets clciked
private void datagrid1_ItemC ommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
if(String.Compa re(e.CommandNam e,"flagButton", true)==0)
{
Response.Write( "done");
}
}
//Event TO BE EXECUTED BY DYNAMIC CREATED CONTROL lButton
private void lButton_Click(o bject sender, System.EventArg s e)
{
DataGridCommand EventArgs dgcea = (DataGridComman dEventArgs)e;
datagrid1_ItemC ommand(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 1534
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_ItemC ommand?

*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
3578
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 language="JavaScript" type="text/javascript"> function show(that) { if (box.style.visibility=='hidden') { that.style.visibility = 'visible'}; }
1
1972
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 point out the really obvious and stupid thing I'm overlooking here. :) I have a custom control that provides a tasklist similar to what you get in certain parts of Office. On the client it consists of a bunch of nested, named and id'ed divs with...
10
4007
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 application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
3
1965
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 database. if i create system timers dynamically, i cannot reference them from the start button click event handler. the build error is ' <timer name> not declared> ---- however, if i declare the system timers outside runtime code, everything works...
6
2849
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 order for a dynamically created control to persist its state, it needs to be created and loaded into the page control hierarchy before the viewstate is loaded. The proposed solution, and that which I have been using, has been to create all of...
3
2057
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 is said and done, there are several new controls (LinkButtons) on the page. Dynamicly created controls MUST be created in the Page_Load. Now, if I click one of the LinkButtons, the page attempts to reload
4
1967
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 purpose: I need to program a dynamically generated list, kind of like a shoutbox, that stores the messages in a Profile variable. I know, I know, the messages would be lost as soon as the session expires, but that'll be sufficient for this...
1
1846
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(); oRb.AutoPostBack = true; oRb.CheckedChanged += new System.EventHandler(this.clientRadioButton_click); }
15
1379
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 in my code? Thanks Chris Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
0
8614
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8330
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8471
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.