C#.net を使用してアプリケーションを作成しています。
データベースのデータが更新されたらすぐにページを更新しようとしています。
データベースから 1 つの値を 1 つのラベルに格納し、同じ値を別のラベルに格納するたびに関数を呼び出し、その後 2 つのラベルを比較しています。
コードは
私のwebpage.aspx.csは
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
SqlDataAdapter adp = new SqlDataAdapter("SELECT name from Master where id= 10"), con);
DataSet ds = new DataSet();
adp.Fill(ds);
Label2.Text = Convert.ToString(ds1);
}
protected String CodeBehind()
{
SqlDataAdapter adp = new SqlDataAdapter("SELECT name from Master where id= 10"), con);
DataSet ds = new DataSet();
adp.Fill(ds);
String ds2 = Convert.ToString(ds1);
return (ds2);
}
protected int compare()
{
string abc = Label2.Text;
string abcd = Label3.Text;
if (abc == abcd)
{
return(0);
}
else
{
return (1);
}
}
}
私のフロントエンドのJavaScriptコードは
<script type="text/JavaScript">
function AutoRefresh() {
document.getElementById('<%= Label3.ClientID %>').innerHTML = '<%= CodeBehind() %>';
var res = '<%= compare() %>'
if ( res == 1) {
alert("same");
setTimeout("location.reload(true);", 60000);
}
else {
alert("different")
abx();
}
}
function abx() {
AutoRefresh();
}
しかし、コードビハインド関数を呼び出すと、バックエンドコード全体が実行されるため、両方のラベルの値が同時に更新されるという問題があります。
毎回コードビハインド関数を呼び出し、label2 の値を一度だけ更新したいだけです。
どうすればそれができるか教えてください。