473,320 Members | 1,746 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,320 software developers and data experts.

Why does my sharepoint web part event handler lose the sender value on postback?

I have a web part which is going to be a part of pair of connected web parts. For simplicity, I am just describing the consumer web part.

This web part has 10 link buttons on it. And they are rendered in the Render method instead ofCreateChildControls as this webpart will be receiving values based on input from the provider web part. Each Link Button has a text which is decided dynamically based on the input from provider web part.

When I click on any of the Link Buttons, the event handler is triggered but the text on the Link Button shows up as the one set in CreateChildControls. When I trace the code, I see that the CreateChildControls gets called before the event handler (and i think that resets my Link Buttons). How do I get the event handler to show me the dynamic text instead?

Here is the code...

Expand|Select|Wrap|Line Numbers
  1.     public class consWebPart : Microsoft.SharePoint.WebPartPages.WebPart
  2.     {
  3.         private bool _error = false;
  4.         private LinkButton[] lkDocument = null;
  5.  
  6.         public consWebPart()
  7.         {
  8.             this.ExportMode = WebPartExportMode.All;
  9.         }
  10.  
  11.         protected override void CreateChildControls()
  12.         {
  13.             if (!_error)
  14.             {
  15.                 try
  16.                 {
  17.                     base.CreateChildControls();
  18.  
  19.                     lkDocument = new LinkButton[101];
  20.                     for (int i = 0; i < 10; i++)
  21.                     {
  22.                         lkDocument[i] = new LinkButton();
  23.                         lkDocument[i].ID = "lkDocument" + i;
  24.                         lkDocument[i].Text = "Initial Text";
  25.                         lkDocument[i].Style.Add("margin", "10 10 10 10px");
  26.                         this.Controls.Add(lkDocument[i]);
  27.                         lkDocument[i].Click += new EventHandler(lkDocument_Click);
  28.                     }
  29.                 }
  30.                 catch (Exception ex)
  31.                 {
  32.                     HandleException(ex);
  33.                 }
  34.             }
  35.         }
  36.  
  37.         protected override void Render(HtmlTextWriter writer)
  38.         {
  39.             writer.Write("<table><tr>");
  40.             for (int i = 0; i < 10; i++)
  41.             {
  42.                 writer.Write("<tr>");
  43.                 lkDocument[i].Text = "LinkButton" + i;
  44.                 writer.Write("<td>");
  45.                 lkDocument[i].RenderControl(writer);
  46.                 writer.Write("</td>");
  47.                 writer.Write("</tr>");
  48.             }
  49.             writer.Write("</table>");
  50.         }
  51.  
  52.         protected void lkDocument_Click(object sender, EventArgs e)
  53.         {
  54.             string strsender = sender.ToString();
  55.             LinkButton lk = (LinkButton)sender;
  56.         }
  57.  
  58.         protected override void OnLoad(EventArgs e)
  59.         {
  60.             if (!_error)
  61.             {
  62.                 try
  63.                 {
  64.                     base.OnLoad(e);
  65.                     this.EnsureChildControls();
  66.                 }
  67.                 catch (Exception ex)
  68.                 {
  69.                     HandleException(ex);
  70.                 }
  71.             }
  72.         }
  73.  
  74.         private void HandleException(Exception ex)
  75.         {
  76.             this._error = true;
  77.             this.Controls.Clear();
  78.             this.Controls.Add(new LiteralControl(ex.Message));
  79.         }
  80.     }
Jun 2 '10 #1
1 3503
I too have a problem with event handlers on my custom WebParts.
My problem is:
I create a button object in the CreateChildControls method,
I give the button an ID value,
I initialize the onClick EventHandler like so:

MyButton.Click += new EventHandler(MyButton_Click);

Once I press that button, the event is correctly executed,
but from then on the event is activates (after the CreateChildControls is activated)
on every post back of the page !!

How do I keep the event from occuring when the button is not pressed ???

The WebPart is written in c# Framework 3.5
and is added to a sharePoint page.

Thank you.
Sep 27 '10 #2

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

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
5
by: kw | last post by:
My aspx dynamically loads an ascx into a placeholder. The ascx has an event. When I click on the submit linkbutton in the ascx, the event does not fire. But if I click it a second time, it...
2
by: Carlos | last post by:
Hi all, I just tried to use a static variable within the scope of the page_load event handler, but apparently it did not work all I wanted to do is to evaluate the postback condition and...
13
by: Richard W | last post by:
I have a very simple web page (ASP.NET) that I am trying to build. On the web page is a checkbox that enables or disables other controls based upon the checked status. However, .NET fails to...
4
by: Me | last post by:
My form has a dynamically populated context menu, and each has the same event handler for the click event. The event need to write the text of the clicked menu item into a database, so I need to...
1
by: kaczmar2 | last post by:
I have an ASP.NET page where controls are created dynamically, and I have an issue where one event handler creates another set of controls, and then adds event handlers to those controls. The...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
7
by: Andrus | last post by:
I noticed that DataGridView CellValidated() and other event handler first parameter is object: Grid.CellValidated+=new DataGridViewCellEventHandler(Grid_CellValidated); .... void...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.