Disable Postback on button ASP NET c

0 votes

Here's an illustration of what I'm talking about.

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 issue is that the asp:button performs a postback in addition to calling DoWork ()

As a result, I'm reloading my page and re-initializing my panel:(

I'd like the items I've added on the panel to remain there!

All assistance is very welcomed; but, I am not asking for someone to deliver you the solution. Thank you for taking any measures!

Here's a specific example of my issue.

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 an empty panel and a button with this click even handler are visible on the website.

I've tried this as well, and it still doesn't work. It's now removing the first item that was 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);
}
May 27 in Others by pranav
• 2,580 points
34 views

1 answer to this question.

0 votes

If you're dynamically adding controls to the Panel, you'll need to recreate them at each postback and ensure that the controls have the same IDs so that ViewState can populate the data. In most cases, it's ideal to rebuild dynamic material on OnInit, although this can be challenging in particular cases.

The DynamicControlsPlaceHolder is one of my favourite tools since you can add dynamic controls to it and it will persist them automatically without any further coding on the page. All you have to do now is add controls to it, and it will take care of the rest.

Here's how to get there:
http://www.denisbauer.com/Home/DynamicControlsPlaceholder

Use OnClientClick and return false to prevent your button from executing a postback.

OnClientClick="return false;"
answered May 30 by rajiv
• 1,620 points

Related Questions In Others

0 votes
1 answer

How to run C# code on Ubuntu

Hey @Abha, you can create and run ...READ MORE

answered Mar 1, 2019 in Others by Anvi
• 14,170 points
5,207 views
0 votes
1 answer

SEO With Angular and ASP.NET MVC (not SPA)

the best thing you can do is ...READ MORE

answered Feb 17 in Others by narikkadan
• 10,840 points
21 views
0 votes
1 answer

Is the single page app in asp.net MVC 4 examples, "Big Shelf", SEO friendly?

the pages that render on the basics ...READ MORE

answered Feb 20 in Others by narikkadan
• 10,840 points
25 views
0 votes
1 answer

Getting "net::ERR_BLOCKED_BY_CLIENT" error on some AJAX calls

Matching the URLs against a type of ...READ MORE

answered Feb 22 in Others by Aditya
• 7,280 points
66 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
248 views
0 votes
1 answer

Writing SEO friendly url gets infinite loop asp.net

you should know that regex avoid cases ...READ MORE

answered Feb 11 in Others by narikkadan
• 10,840 points
139 views
0 votes
0 answers

How to get Bitcoin value for corresponding USD value in ASP.NET C#?

I want to get the Bitcoin value ...READ MORE

Mar 1 in Blockchain by Soham
• 8,730 points
93 views
0 votes
0 answers

The request was aborted: Could not create SSL/TLS secure channel

When I connect to an  HTTPS server ...READ MORE

May 1 in Other DevOps Questions by Kichu
• 19,040 points
160 views
0 votes
1 answer

What's the strangest corner case you've seen in C# or .NET?

Bankers' Rounding is a term that refers ...READ MORE

answered May 30 in Others by rajiv
• 1,620 points
15 views
0 votes
1 answer

C# code works in Visual Studio 2019 but not in online compiler

Repl.it compiles in Mono C# 4.6.2.0, according ...READ MORE

answered May 30 in Others by rajiv
• 1,620 points
18 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP