Disable sending to ASP.NET C # button

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 does the DoWork() and also calls DoWork()

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

I want my objects that I added to the panel to stay there!

All help was appreciated without looking for the hand you answered, like a deal. Any steps are welcome thank you!

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); } 

The only thing on the page is an empty panel and a button with this click, even a handler.

I tried this too and it still doesn't work. Now its cleaning of the initial element is added 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); } 
+4
source share
7 answers

If you add controls to the Panel dynamically, you will need to recreate the controls with each callback and remember to assign the same identifiers to the controls so that the ViewState can populate the values. It is usually best to recreate dynamic content during OnInit , but in some situations this can be difficult.

One of my favorite tools is DynamicControlsPlaceHolder , because you can add dynamic controls to it, and it will be saved automatically, without the need for additional encoding on the page. Just add controls to it and it will do the rest.

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

As well as OnClientClick your button from OnClientClick back, use OnClientClick and return false.

 OnClientClick="return false;" 
+9
source

you can use

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

using, thus, the javascript function to add them. This is how I came across this problem.

+4
source

You can also try the following:

 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) } } 
+3
source

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 makes HTML like:

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

You are right, you will have to add new elements to the Panel after PostBack. This is the nature of the .NET pipeline.

If you use a data-bound control, such as Repeater, to display the contents of the panel, then the button click handler just needs to re-configure the control and everything will work correctly.

0
source

Change asp: Asp: LinkButton button solved my problem.

0
source

I think it's better to use the Ajax Update panel. And insert your button into it.

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

Source: https://habr.com/ru/post/1379372/


All Articles