如何在asp.net中从Gridview将计数器重置为零 [英] How to Reset the Counter to Zero from Gridview in asp.net

查看:92
本文介绍了如何在asp.net中从Gridview将计数器重置为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ASP.NET中将计数器重置为零?



亲爱的朋友们,



我有一个名为RESET的ASP.NET按钮和这个按钮下面的网格视图。

当我点击这个重置按钮时,它应该清除网格中的一列,它应该将所有数字设置为0(零)。



在我的数据库中,我有一个名为 Linkcounter 的表,字段为计数器链接

例如:这是我的数据库表

< pre lang =text> Counter Links
12首页
15关于我们
12我们的服务
3联系我们





当我点击重置按钮时,计数器值应重置为0(零)。



请帮忙。



这是我的代码:

 受保护  void  Page_Load( object  sender, EventArgs e)
{

if (!IsPostBack)
{
LoadGridCounterData();
}

}


private void LoadGridCounterData()
{
SqlConnection con = new SqlConnection(_connString);


SqlCommand cmd = new SqlCommand( 选择计数器,链接来自linkcounter,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GVCounter.DataSource = ds;
GVCounter.DataBind();
}



这是面临问题的代码(重置按钮代码):

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

SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
cmd.CommandText = 更新LinkCounter设置计数器= 0;

cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GVCounter.DataSource = ds;
// GVCounter.DataBind();

ScriptManager。 RegisterStartupScript( this this .GetType(), RunCode javascript:alert('成功计数器重置为零'); true );
}

解决方案

您正在加载页面加载数据。事件在页面加载后触发。将数据加载到prerender中,该按钮在按钮之后触发,它将正常工作。


How to reset the counter to zero in ASP.NET?

Dear Friends,

I have a ASP.NET button with the name RESET and a grid view below this button.
When I click on this reset button it should clear one column in the grid and it should set all the numbers to 0 (Zero).

In my database I have table called Linkcounter with fields as Counter and Links.
For example: this is my database table

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



When I click on the reset button the counter values should reset to 0 (Zero).

Please help.

This is my code:

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();
    }


This is code am facing problem (reset button code):

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);
    }

解决方案

You are loading the data in page load. The event fires AFTER page load. Load the data in prerender, which fires AFTER the button, and it will work fine.


这篇关于如何在asp.net中从Gridview将计数器重置为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆