webform 中Gridview绑定sql数据库,如何实时刷新?
功能介绍:Gridview是绑定sql中“学生信息”表的,现在我可以通过新建→插入来影响表中的内容,但是插入之后页面中Gridview中数据没有显示。
请问:如何单击“插入”按钮就可以实时展示表中内容?
页面形式:
布局代码:
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
DataKeyNames="学号" DataSourceID="SqlDataSource1">
SortExpression="学号" />
ConnectionString=""
SelectCommand="SELECT * FROM [学生信息]">
Visible="False" />
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication15
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = "server=.;database=学生信息管理系统;uid=sa;pwd=***";
SqlConnection conn;
conn = new SqlConnection(str);
conn.Open();
if (conn.State == ConnectionState.Open)
{
Label1.Text = "连接成功";
}
/* SqlCommand cmd = new SqlCommand("select * from dbo.学生信息", conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds, "cs");
GridView1.DataSource = ds.Tables[0]; */
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label2.Visible = true; Label3.Visible = true; Label4.Visible = true; Label5.Visible = true; Label6.Visible = true;
TextBox1.Visible = true; TextBox2.Visible = true; TextBox3.Visible = true; TextBox4.Visible = true; TextBox5.Visible = true;
Button2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
string str = "server=.;database=学生信息管理系统;uid=sa;pwd=***";
SqlConnection conn;
conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd= new SqlCommand("Insert into 学生信息 (学号,姓名,年级,名次,性别) values( '"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"','"+TextBox5.Text+"')", conn);
cmd.ExecuteNonQuery();
}
}
}
------解决思路----------------------
在你插入完信息的这个操作后面再绑定GridView一次,
相当于每次操作后都绑定它一次就OK了
------解决思路----------------------
ajax定时器 SignalR websocket..
------解决思路----------------------
重新绑定数据……
------解决思路----------------------
在Page_Load里面调用绑定的事件,每次页面刷新,数据就刷新了,