85476

Accessing label from child control

Question:

I have a control Orders.ascx in this control I have a reference to another control Grid.ascx something like this:

In my Orders.ascx I have :

<asp:Label id="warnings" runat="server" /> <uc:Grids id="uxGrid" runat="server/>

Now in the grid ( Grids.ascx ) I have a drop down control that has the quantities with a event handler method for on index change, when a different quantity is chosen in a row, it posts back and updates the price depending on the quantity. I need for each time it posts back to add a message to the label warnings in Orders.ascx, but my problem is that this label is in the parent control. I tried using FindControl and it DOES find it and I can set the text but it never updates the warnings label.

How can I update that label text from the drop down's on index change control?. It seems that it doesnt inject everytime i select the quantity.

Answer1:

The simplest, cleanest way would be to use an event.

<ol><li>

Create an event argument class, derived from EventArgs, that includes a property for the data you want to pass:

public class PriceChangeEventArgs: EventArgs { public decimal Price { get; set; } } </li> <li>

Set up an event in the UserControl:

public event EventHandler<PriceChangeEventArgs> PriceChanged; </li> <li>

Raise the event when capturing the change in the drop down list index:

protected void DropDownList1_SelectedIndexChanged(object source, EventArg e) { PriceChangeEventArgs args = new PriceChangeEventArgs(); args.Price = Convert.ToDecimal(DropDownList1.SelectedValue); PriceChanged(this, args); } </li> <li>

Handle the event in the parent control page or control:

UserControl1.PriceChanged += new EventHandler<MyEventArgs>(page_PriceChanged); protected void page_PriceChanged(object source, MyEventArgs e) { Label1.Text = e.Price.ToString("C"); } </li> </ol>

This way, you haven't tied your user control in to your page: you can still use the user control other places, and the page doesn't need to know how the user control works, other than that it raises an event.

ETA: Added an important step I omitted: <em>actually raising the event!</em>

Recommend

  • HTTPS + gzip: Is it a security vulnerability if I only gzip non-sensitive files?
  • How to produce a Code 39 that can be reliably read after faxing
  • Dispose not working as expected in DirectorySearcher
  • Fluid video height
  • Weird LEFT OUTER JOIN on Includes eager loading of rails 3
  • Pandas: merge_asof() sum multiple rows / don't duplicate
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • xcode don't localize specific strings
  • Force show.bind execution
  • jQuery ready not fired after rails link_to is clicked
  • Webgrid not refreshing after delete MVC
  • Swift: Switch statement fallthrough behavior
  • Yii2: Config params vs. const/define
  • Disable Enter in editText android
  • NHibernate Validation Localization with S#arp Architecture
  • Algorithm for a smudge tool?
  • How can I send an e-mail from a vbs script
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Accessing IRQ description array within a module and displaying action names
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Join two tables and save into third-sql
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Angular 2 constructor injection vs direct access
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Android Google Maps API OnLocationChanged only called once
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Can't mass-assign protected attributes when import data from csv file
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time