简体   繁体   中英

Using Page_Load method in button click

I trying to call Page_Load method after clicking btnSubmit . For my Page_Load I did databinding on dropdownlist box where column caseprogress != 'ongoing' . Then for my btnSubmit_Click I insert some data into another table, update caseprogress = 'completed' and also call of Page_Load method. But my dropdownlist box don't seem to be rebinding. (Unless I refresh the page) I try this method on other page it work but not for this. FYI I don't have any update panel in the page of this. Same go for the other one which is working.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Bind data to Dropdownlist box
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    //Insert / Update data of sql data table

   Page_Load(null, EventArgs.Empty);
}

you can have method to Bind the Dropdownlist

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindData();
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    //Insert / Update data of sql data table

   BindData();
}

private void BindData()
{
    String policeid = (String)Session["policeid"];
    SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
    con.Open();
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter("Select mr.memberreportid From PoliceAccount pa, MemberReport mr Where pa.policeid = '" + policeid + "' And pa.handle = mr.memberreportid And mr.caseprogress = 'ongoing'", con);
    da.Fill(ds);
    ddlMemberReportID.DataSource = ds;
    ddlMemberReportID.DataTextField = "memberreportid";
    ddlMemberReportID.DataValueField = "memberreportid";
    ddlMemberReportID.DataBind();
    con.Close();
}

and remove Page_Load(null, EventArgs.Empty); code line

your problem is probably your call to Page_Load(null, EventArgs.Empty); It will cause a double call to Page_Load, the last one, which you call inside the btn_Click event, will have IsPostback = true and your data will not be bound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM