Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have create 100 buttons dynamically using c#, but when i click on button Page refresh every time, i want to avoid page refresh using C#, plz provide some solution...
Thanks in advance.

What I have tried:

protected void Page_Load(object sender, EventArgs e)
    {
        HtmlTable myTable = new HtmlTable();
        int n = 0;
        for (int i = 0; i < 20; i++)
        {

            HtmlTableRow row = new HtmlTableRow();
            for (int c = 1; c <= 5; c++)
            {
                n = (5 * i) + c;
                HtmlTableCell cell = new HtmlTableCell();
                Button btn = new Button();
                btn.Text = n.ToString();
                btn.ID = n.ToString();
                //btn.BackColor = Color.Transparent;
                btn.Width = 30;
                btn.Height = 25;
                btn.Click += new EventHandler(btn_Click);
                cell.Controls.Add(btn);
                row.Controls.Add(cell);
                myTable.Controls.Add(row);
                cell.Attributes.Add("Class", "htTableCellCss");
            }
            row.Attributes.Add("Class", "htTableRowCss");
        }
        myTable.Attributes.Add("Class", "htTableCss");
        PlaceHolder1.Controls.Add(myTable);
    }

    public void btn_Click(object sender, EventArgs e)
    {
        Response.Write(((sender) as Button).Text);
        Button btn = sender as Button;
        btn.BackColor = Color.Green;
        
    }
Posted
Updated 19-Jun-17 23:52pm
v2

You can never use C# to prevent the browser from refreshing the page, that is just not what C# is meant to do. Instead, you need to use JavaScript and then handle the page refresh event to cancel it.
JavaScript
$("#yourBtn").click(function() { 
   // For example, in the click event of your button
   event.preventDefault(); // Stop the default action, which is to post
});

This way, you can prevent the page from refreshing. However, then to post the content (if there is a requirement) you will need to send that data using Ajax request.

However, if you still want to do that, then you might want to register a script to that button, that in turn (using JavaScript) prevents the page from reloading.
 
Share this answer
 
Put your controls inside an update panel.

Introduction to the UpdatePanel Control[^]
 
Share this answer
 
Use page property Ispostback for that purpose
 
Share this answer
 
protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    // page load code
  }
}
 
Share this answer
 
Comments
Member 13253455 26-Jun-17 15:16pm    
when i'm using

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// page load code
}
}

my buttons become invisible

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900