You must Sign In to post a response.
  • Category: ASP.NET

    Problem with refresh

    hi friends,
    i have problem with refresh button.when execute code button event will fire.after i'am refresh the button again firing.how to avoid button click at the time of refreshing the page.
  • #763162
    Hi Sadiq.

    Button click will make page refresh and page_Load event gets fired.
    But are saying when you execute your code button event is firing.
    Until unless you press button it wont fire the button_click event.
    If you want any particular code to get execute only once, then keep that code in IspostBack property like below.

    if(!IsPostBack)
    {
    //Put the logic to be executed only once for first request.
    }

    Sridhar Thota.
    Editor: DNS Forum.

  • #763163
    Hello Sadiq,

    If you are using Button click event then your page load event will also be executed by default. If you want such type of execution as per your need like you want to avoid page load event when your button click event is fired than you can use postback event like this.

    Sample Code :

    protected void Page_Load(object sender, EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    string ipaddress = null;
    ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (ipaddress == "" || ipaddress == null)
    ipaddress = Request.ServerVariables["REMOTE_ADDR"];
    }
    }

    This code will get the ipaddress of client system only once when the page loads the first time. The will not be executed everytime.

    Suppose if your are using Button click event then also page load event will be executed but it will not get the ipaddress again.

    Hope you understand.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763164
    thank you for responding.what is the remote address.and working like that but no result. please check my code

    protected void Page_Load(object sender, EventArgs e)
    {

    if (!Page.IsPostBack)
    {
    Get_ProductDetails(int.Parse(Request.QueryString["ID"].ToString()));
    string ipaddress = null;
    ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (ipaddress == "" || ipaddress == null)
    ipaddress = Request.ServerVariables["REMOTE_ADDR"];

    }
    }

    private void Get_ProductDetails(int id)
    {
    UserInfo obj = new UserInfo();
    UserBusiness obj1 = new UserBusiness();
    DataSet ds = obj1.Get_Product_Details(id);
    HiddenField1.Value = ds.Tables[0].Rows[0]["Id"].ToString();
    Image1.ImageUrl = "Admin/ProductImages/" + ds.Tables[0].Rows[0]["Image"].ToString();
    Label1.Text = ds.Tables[0].Rows[0]["ProductName"].ToString();
    Label2.Text = ds.Tables[0].Rows[0]["ModelNo"].ToString();
    Label3.Text = ds.Tables[0].Rows[0]["Description"].ToString();

    }

    protected void btnenquiry_list_Click(object sender, EventArgs e)
    {

    //SqlConnection con = new SqlConnection("Data Source=23.229.20.11;Initial Catalog=Jaf_Leddb;User ID=jafled;Password=zxc@1234");
    //SqlDataAdapter da = new SqlDataAdapter("select Image,ProductName,ModelNo,Description from Product_Reg where id='" + int.Parse(Request.QueryString["ID"].ToString()) + "'", con);
    //DataSet ds = new DataSet();
    //da.Fill(ds, "Product_Reg");

    Product_reg pr = new Product_reg();
    List<Product_reg> list = new List<Product_reg>();

    if (Session["Products"] == null)
    {
    list.Add(new Product_reg
    {
    Id = int.Parse(HiddenField1.Value),
    Name = Label1.Text,
    Modelno = Label2.Text,
    });
    Session["Products"] = list;


    }

    else if (Session["Products"] != null)
    {
    list = (List<Product_reg>)Session["Products"];
    list.Add(new Product_reg
    {
    Id = int.Parse(HiddenField1.Value),
    Name = Label1.Text,
    Modelno = Label2.Text,
    });

    Session["Products"] = list;
    gvProduct.DataSource = Session["Products"];
    gvProduct.DataBind();

    }
    gvProduct.DataSource = Session["Products"];
    gvProduct.DataBind();

    }

  • #763365
    1. use the !IsPostBack in the page load

    2. Use the update panel and do ajax call

    By Nathan
    Direction is important than speed


Sign In to post your comments