Where do I instantiate an UpdatePanel in my ASP.NET Codebehind?
Background
I have the fol开发者_高级运维lowing code that is supposed to refresh data when a button called refresh
is clicked.
public static void refreshBaan()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand comm = new SqlCommand("sp_start_job", conn);
SqlParameter jobname = new SqlParameter("job_name", "EBS");
SqlParameter servername = new SqlParameter("server_name", "bnvmsql0a610912");
comm.Parameters.Add(jobname);
comm.Parameters.Add(servername);
comm.CommandType = CommandType.StoredProcedure;
conn.Open();
comm.ExecuteReader();
conn.Close(); `
}
I have an Updatepanel that I want to show when clicked, so naturally I want to put in Updatpanel1.visible = true;
in the above statement, but it doesn't let me.
Question
How can I refresh the data using an updatepanel? Where do I need to instantiate that update panel?
You can only access instance members of a class in non-static methods.
I would suggest creating a click handler specifically for the button such as:
protected void refresh_Click(object sender, EventArgs e)
{
updatePanel1.Visible = true;
refreshBaan();
}
where the button declaration looks something like:
<asp:Button ID="refresh" runat="server" OnClick="refresh_Click" />
What do you mean, it doesn't work? The code doesn't compile?? Static methods don't have access to non-static page level properties which would include the UpdatePanel. Change the method to be non-static and you should be ok.
This might help: http://ajax.net-tutorials.com/controls/updatepanel-control/
精彩评论