• UpdatePanel GridView刷新


    前台代码
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        Namespace
    ="System.Web.UI" TagPrefix="asp" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>无标题页</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
                
    <asp:ScriptManager ID="ScriptManager1"     runat="server">    
                  
    </asp:ScriptManager>
              
    <table>
              
              
    <tr>
                
    <td width="44%">
                   
    <asp:UpdatePanel ID="uid"  runat="server">
                
                
    <ContentTemplate>
                    
    &nbsp;<asp:Button ID="Button1" runat="server" Text="异步回送得到数据" OnClick="Button1_Click" />&nbsp;&nbsp;
                        
    <asp:Button ID="Button2" runat="server" Text="整页回送 --刷新页面" OnClick="Button2_Click" /><br />
                        
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="userid" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                            
    <Columns>
                                
    <asp:BoundField DataField="userid" HeaderText="userid" />
                                
    <asp:BoundField DataField="username" HeaderText="username" />
                                
    <asp:CommandField ShowEditButton="True" />
                                
    <asp:CommandField ShowDeleteButton="True" />
                            
    </Columns>
                        
    </asp:GridView>
                        
    <br />
                       
    <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Large" Width="239px"></asp:Label>
                      
    </div>
            
    &nbsp;&nbsp;
                
    </ContentTemplate>
                
    <Triggers>
                    
    <asp:AsyncPostBackTrigger    ControlID="Button1" /> <%-- 注册控件的调用方法--%>
                    
    <asp:AsyncPostBackTrigger ControlID="Button3" /> <%--这两个控件无刷新调用 --%>
                    
    <asp:PostBackTrigger  ControlID="Button2" />
                
    </Triggers>
                  
            
    </asp:UpdatePanel>
            
    &nbsp;&nbsp;
                    
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button3" Text="添加" runat="server" OnClick="Button3_Click" />
                
    </td>
                
    <td  width="4%"></td>
                
    <td style="vertical-align: top;  47%">
                这边是使用UpdatePanel 是弹出一个对话框,UpdatePanel不支持Page.ClientScript.RegisterStartupScript()
                    
    &nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        
    <ContentTemplate>
                            
    <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="弹出对话框" />
                        
    </ContentTemplate>
                        
    <Triggers>
                            
    <asp:AsyncPostBackTrigger    ControlID="Button4" /> <%-- 注册控件的调用方法--%>

                        
    </Triggers>
                    
    </asp:UpdatePanel>

                
                
    </td>
              
    </tr>
              
    </table>

        
    </form>
    </body>
    </html>
    CS 代码
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;


    public partial class _Default : System.Web.UI.Page 
    {
        
    private const string STRCONN = "server=.;database=myTestDB;uid=sa;pwd=sa;";
        
    protected void Page_Load(object sender, EventArgs e)
        {

        }


        
    protected void Button1_Click(object sender, EventArgs e)
        {

            BindData();
        }

        
    /// <summary>
        
    /// 绑定数据
        
    /// </summary>
        private void BindData() 
        {
            SqlConnection conn 
    = new SqlConnection(STRCONN);
            
    string sql1 = "select *  from AutoComplet ";
            SqlDataAdapter myAdapter 
    = new SqlDataAdapter(sql1, conn);
            DataSet ds 
    = new DataSet();
            myAdapter.Fill(ds, 
    "AutoComplet");
            
    //来自web service的dataset,这里随便一个ds就可以;
            this.GridView1.DataSource = ds.Tables["AutoComplet"].DefaultView; ;
            
    this.GridView1.DataBind(); //数据绑定
        }


        
    protected void Button2_Click(object sender, EventArgs e)
        {
            
    this.Label1.Text = "整页回送 --刷新页面";
        }

        
    /// <summary>
        
    /// 删除数据
        
    /// </summary>
        
    /// <param name="sender"></param>
        
    /// <param name="e"></param>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection conn 
    = new SqlConnection(STRCONN);
            
    string sql1 = " delete  AutoComplet where userid= " + GridView1.DataKeys[e.RowIndex].Value.ToString();
            SqlCommand sc 
    = new SqlCommand(sql1,conn);
            sc.Connection.Open();
            sc.ExecuteNonQuery();
            sc.Connection.Close();

            GridView1.EditIndex 
    = -1;
            BindData();
        }

        
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ((BoundField)GridView1.Columns[
    0]).ReadOnly = true//设置数量列为不可编辑状态
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }
        
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex 
    = -1;
            BindData();
        }


        
    /// <summary>
        
    /// 更新数据
        
    /// </summary>
        
    /// <param name="sender"></param>
        
    /// <param name="e"></param>
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            SqlConnection conn 
    = new SqlConnection(STRCONN);
            
    string sql1 = " update  AutoComplet set  username = @username  where userid=@userid ";
            SqlCommand sc 
    = new SqlCommand(sql1, conn);

            sc.Parameters.Add(
    "@userid",SqlDbType.Int,4);
            sc.Parameters[
    "@userid"].Value = GridView1.DataKeys[e.RowIndex].Value.ToString();

            sc.Parameters.Add(
    "@username", SqlDbType.VarChar, 50);
            sc.Parameters[
    "@username"].Value = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();

            sc.Connection.Open();
            sc.ExecuteNonQuery();
            sc.Connection.Close();

            GridView1.EditIndex 
    = -1;
            BindData();
            ScriptManager.RegisterStartupScript(UpdatePanel1, 
    this.GetType(), "alter""alert('更新成功!')"true);
        }

        
    /// <summary>
        
    /// 插入新数据
        
    /// </summary>
        
    /// <param name="sender"></param>
        
    /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            SqlConnection conn 
    = new SqlConnection(STRCONN);
            
    string sql1 = " insert into AutoComplet values (@username) ";
            SqlCommand sc 
    = new SqlCommand(sql1, conn);

            sc.Parameters.Add(
    "@username", SqlDbType.VarChar, 50);
            sc.Parameters[
    "@username"].Value = TextBox1.Text.Trim();

            sc.Connection.Open();
            sc.ExecuteNonQuery();
            sc.Connection.Close();

            GridView1.EditIndex 
    = -1;
            BindData();
        }
        
    protected void Button4_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(UpdatePanel1, 
    this.GetType(), "alter""alert('弹出对话框!')"true);
           
    // 不支持
            
    //Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", "aler('弹出对话框!');", true);
        }
    }
  • 相关阅读:
    3.27 课堂 笔记
    第四周 4-2
    3-26
    Java EE期末项目
    条件查询、SQL、JPQL、HQL比较
    J2EE 第八周(04.23-04.29)
    J2EE 第七周(04.16-04.22)
    J2EE 第六周(04.09-04.15)
    J2EE 第五周(04.02-04.08)
    J2EE 第四周(03.26-04.01)
  • 原文地址:https://www.cnblogs.com/gaoyuchuanIT/p/1662795.html
Copyright © 2020-2023  润新知