5

here is an example of what I am doing

Page Load { //Adds items to a panel (not an updatepanel just a normal panel control) } protected void btnNexMod_Click(object sender, EventArgs e) { // Calls DoWork() and Appends more items to the same panel } 

My problem is that the asp:button is doing a postback as well as calling DoWork()

Therefore, re-calling my page load, re-initializing my panel :(

I want my items that I have added to the panel to stay there!

All help appreciated, not looking for a hand you the answer kind-of deal. Any steps are appreciated thanks!

Here is an exact example of my problem.

protected void Page_Load(object sender, EventArgs e) { CheckBox chkbox = new CheckBox(); chkbox.Text = "hey"; chkbox.ID = "chk" + "hey"; // Add our checkbox to the panel Panel1.Controls.Add(chkbox); } protected void Button1_Click(object sender, EventArgs e) { CheckBox chkbox = new CheckBox(); chkbox.Text = "hey"; chkbox.ID = "chk" + "hey"; // Add our checkbox to the panel Panel1.Controls.Add(chkbox); } 

Only thing on the page is a empty panel and a button with this click even handler.

I have also tried this and it still doesn't work. Now its clearing the initial item appended to the panel.

if (!Page.IsPostBack) // to avoid reloading your control on postback { CheckBox chkbox = new CheckBox(); chkbox.Text = "Initial"; chkbox.ID = "chk" + "Initial"; // Add our checkbox to the panel Panel1.Controls.Add(chkbox); } 
2
  • Page.IsPostBack is your friend. Commented Nov 2, 2011 at 20:06
  • Nothing I can do here with the viewstate on the panel? Commented Nov 2, 2011 at 21:25

7 Answers 7

10

If you're adding controls to the Panel dynamically, then you'll have to recreate the controls at every postback, and make sure to assign the same IDs to the controls so that ViewState can populate the values. It's usually best to recreate dynamic content during OnInit, but this can be difficult in some situations.

One of my favorite tools is the DynamicControlsPlaceHolder, because you can add dynamic controls to it and it will persist them automagically, without any additional coding required on the page. Just add controls to it, and it will do the rest.

Here's the link:
http://www.denisbauer.com/Home/DynamicControlsPlaceholder

As for preventing your button from performing a postback, use OnClientClick and return false.

OnClientClick="return false;" 
Sign up to request clarification or add additional context in comments.

4 Comments

Hey James! Thanks so much I got this working and you saved me a a TONE of head aches! Just curious, have you ever tested this out on lets say something with 500 post backs a day? I'm wondering if this abuses the viewstate, thus leaving lots of overload. Thanks again m8!
Hey @James - Is that DynamicControlsPlaceholder tool still relevant/necessary in 2014? Just curious because I'm wrestling with Dynamic controls and would love any help if it's still usable.
@ChrisEmerson: It really depends on what you're building. If you're doing Web Forms development, there are definitely still some applications where a control like this is very useful. I would advise using JavaScript if you can, but for those niche areas where JavaScript is not an option this control can be a lifesaver.
By adding the OnClientClick="return false;" in your HTML, the OnClick event will be defective.
4

You could use

<asp:LinkButton OnClientClick="javascript:addItemsToPanel();return false;" 

thus using a javascript function to add them. That's how I've got around that problem.

2 Comments

Use which sorry. All thats showing up in your post is "You could use"
Wouldnt that still be client side? I need the function to be severside
3

You can also try this:

Page Load { if (!this.IsPostBack) // to avoid reloading your control on postback { //Adds items to a panel (not an updatepanel just a normal panel control) } } 

1 Comment

I tried this in the page load, but it still does not give me the desired results. See my edit please
3

you can do like this...

ASPX code: <asp:LinkButton ID="someID" runat="server" Text="clicky"></asp:LinkButton> 

Code behind:

public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { someID.Attributes.Add("onClick", "return false;"); } } 

What renders as HTML is:

<a onclick="return false;" id="someID" href="javascript:__doPostBack('someID','')">clicky</a> 

2 Comments

Thanks I will give this a whirl later on tonight as well. Currently I am using the accepted answer, but it looks like DynamicPlaceHolder is saving with the view state ...
thanks for the reply. Although, I don't think this can be edited fire a c# server-side function as well as do something like "return false"... can it?
0

You are correct, you will have to add the new items to the Panel after a PostBack. That is the nature of the .NET pipeline.

If you use a data bound control, like a Repeater, to display the panel contents, then the button click handler just needs to rebind the control and it will all work out correctly.

4 Comments

Thanks for the reply. Is there c# command that I can call that
you need to place the Repeater control in your Panel, and bind it to your data by setting the DataSource and calling .DataBind() on the repeater. 4GuysFromRolla have several good articles on using the Repeater control.
Not using a repeater control. I'm getting two variables from a datbase VIA sql. Name and ID. I think append checkboxes to panel using those names and ID's
You should do this with a data bound control if at all possible. There is a CheckBoxList control that you could look into.
0

Changing the asp:Button to a asp:LinkButton solved my issue.

Comments

0

I think, it is better use the Ajax Update panel. And put your button in to that.

 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button1" /> </ContentTemplate> </asp:UpdatePanel> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.