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, 2022 in Others by pranav
• 2,590 points
7,714 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.

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

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

Related Questions In Others

0 votes
1 answer

Prompt message if Excel column has some value in asp.net c#

I believe it is wise to use ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,600 points
654 views
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,150 points
6,462 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, 2022 in Others by narikkadan
• 63,600 points
568 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, 2022 in Others by narikkadan
• 63,600 points
439 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
911 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, 2022 in Others by narikkadan
• 63,600 points
803 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, 2022 in Blockchain by Soham
• 9,710 points
813 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, 2022 in Other DevOps Questions by Kichu
• 19,040 points
1,987 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, 2022 in Others by rajiv
• 1,620 points
485 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, 2022 in Others by rajiv
• 1,620 points
1,229 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP