Monday 15 July 2013

asp.net - Finding Controls in an Update Panel -


I have seen some threads on it. If I have a control in an updated panel when the page is loaded, then I can easily get it from the following code:

  Label lbls = (Label) upPreview.FindControl (lbl. ID); Lbls.Text = lbl.id;   

I can not do the following on two different buttons with two different update panels

button 1:

  Labeled lbl = new label (); Lbl.Text = "Something"; Lbl.ID = "Something"; UpPreview.ContentTemplateContainer.Controls.Add (LbL);   

button 2

  labeled lbls = (label) upPreview.FindControl (lbl.id); Lbls.Text = lbl.id; UpForm.ContentTemplateContainer.Controls.Add (lbls);   

Essentially I am creating a label and putting it in an update panel, then clicking on the second button I'm taking it to another update panel. Every time I try this, then it says: Price can not be zero. Parameter Name: Toddler

I also have ControlCollection cbb = upPreview.ContentTemplateContainer.Controls;

The same error has been tried. Any ideas?

Your label lbl is lost during partial postback button s you can use viewstate to protect it during a postback.

Add a single label above your page_load and instantize it to clear it.

  Safe label Lbl = null; Secure Zero Page_load (object sender, eventAgps E) {if (! IsPostBack) // First load of page or reload {lbl = new label (); Lbl.Text = "Something"; Lbl.ID = "Something"; UpPreview.ContentTemplateContainer.Controls.Add (LbL); } See Else {// LLL which is stored in the visual position, if (seestate ["label"]! = Null) lbl = (label) viewstrate ["label"]; Else lbl = null; }}   

Then in your buttonclick events:

  Secure zero button 1_kill (object sender, eventAgps E) {// LLL in ViewState before proceeding Save. ViewState ["label"] = LLL; } Secure Zero Button2_Click (Object Sender, EventArgs e) {if (lbl! = Null) {// Behind the LLL with visual state, and add it to another update panel upForm.ContentTemplateContainer.Controls.Add (lbl); } And {lbl = new label (); LLL Text = "Error: The label was empty in the viewstate."; }}   

This way you keep track of it during the post-back.

No comments:

Post a Comment