【解決方法】asp.net の Gridview からカウンターをゼロにリセットする方法

プログラミングQA


ASP.NETでカウンターをゼロにリセットする方法は?

親愛なる友人、

RESET という名前の ASP.NET ボタンと、このボタンの下にグリッド ビューがあります。
このリセット ボタンをクリックすると、グリッド内の 1 つの列がクリアされ、すべての数値が 0 (ゼロ) に設定されます。

私のデータベースには、というテーブルがあります Linkcounter フィールドを次のように CounterLinks.
例:これは私のデータベーステーブルです

Counter  Links
12       Home
15       About Us
12       Our Services
3        Contact Us

リセット ボタンをクリックすると、カウンターの値が 0 (ゼロ) にリセットされます。

助けてください。

これは私のコードです:

C#
protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LoadGridCounterData();
        }

    }


    private void LoadGridCounterData()
    {
        SqlConnection con = new SqlConnection(_connString);
       
        
        SqlCommand cmd = new SqlCommand("Select Counter,link from linkcounter", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GVCounter.DataSource = ds;
        GVCounter.DataBind();
    }

これは問題に直面しているコードです(リセットボタンコード):

C#
protected void ResetImgBtn_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection con = new SqlConnection(_connString);

        SqlCommand cmd = new SqlCommand();
        DataSet ds = new DataSet();
       cmd.CommandText = "Update LinkCounter Set Counter=0";
       
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        GVCounter.DataSource = ds;
     // GVCounter.DataBind();

        ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Successfully Counters Reset to Zero');", true);
    }

解決策 1

ページ読み込みでデータを読み込んでいます。 イベントは、ページの読み込み後に発生します。 ボタンの後に起動する prerender にデータをロードすると、正常に動作します。

コメント

タイトルとURLをコピーしました