C# 方法调用,刷新界面

我在打开界面的时候写了一个方法,为RoomType_Load,往表格里写入数据
public void RoomType_Load(object sender, EventArgs e)
{
this.selectStr = "select * from RoomType "; ;
this.sqlConnection = new SqlConnection(HotelSystem.Connection.ConnString);
try
{

if (this.sqlConnection.State == ConnectionState.Closed)
this.sqlConnection.Open();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(selectStr, sqlConnection);
sda.Fill(ds);

int i;
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["RoomType"].ToString();
dataGridView1.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["Area"].ToString();
dataGridView1.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["Bed"].ToString();
dataGridView1.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["Price"].ToString();
dataGridView1.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i]["AC"].ToString();
dataGridView1.Rows[i].Cells[5].Value = ds.Tables[0].Rows[i]["TV"].ToString();
dataGridView1.Rows[i].Cells[6].Value = ds.Tables[0].Rows[i]["Phone"].ToString();
dataGridView1.Rows[i].Cells[7].Value = ds.Tables[0].Rows[i]["BR"].ToString();

}

}
catch (System.Exception E)
{
Console.WriteLine(E.ToString());
this.sqlConnection.Close();

}
finally
{
this.sqlConnection.Close();
}
}
然后,现在我有一个按钮事件,是新建一条记录,在同一个界面上,我想在按钮事件触发的同时,刷新原有的表格。我的想法是调用那个RoomType_Load,来实现刷新,但是不知道要怎么加到代码里:
try
{
if (this.sqlConnection2.State == ConnectionState.Closed)
this.sqlConnection2.Open();
this.sqlCommand1.ExecuteNonQuery();
MessageBox.Show("新建成功!");
}
我想加到MessageBox后面,这样,在新建成功的时候,调用那个方法,就能刷新出新建的记录。
不知道怎么写,求高手指点下
大师们能说的具体点吗,本人写代码能力不是很强,只会用最简单最笨最繁琐的方法,调用方法和一楼所说的绑定数据库,不是很会弄。能说的具体点吗,或者给个类似的例子参考下

C#刷新界面有五种方法:
第一:
private void Button1_Click( object sender, System.EventArgs e )
{
Response.Redirect( Request.Url.ToString( ) );
}

第二:
private void Button2_Click( object sender, System.EventArgs e )
{
Response.Write( "
<script language=javascript>window.location.href=document.URL;
</script>" );
}

第三:
private void Button3_Click( object sender, System.EventArgs e )
{
Response.AddHeader( "Refresh","0" );
}

第四:
private void Button6_Click( object sender, System.EventArgs e )
{
//Response.Write( "
<script language=javascript>window.location.reload( );
</script>" );
}

第五:(需替换<>)
<script><!--
var limit="3:00"
if ( document.images )
{
var parselimit=limit.split( ":" )parselimit=parselimit[0]*60+parselimit[1]*1
}
function beginrefresh( )
{
if ( !document.images )returnif ( parselimit==1 )window.location.reload( )else
{
parselimit-=1curmin=Math.floor( parselimit/60 )cursec=parselimit%60if ( curmin!=0 )curtime=curmin+"分"+cursec+"秒后重刷本页!"elsecurtime=cursec+"秒后重刷本页!"window.status=curtimesetTimeout( "beginrefresh( )",1000 )
}
}
window.onload=beginrefresh//--> </script><DIV style="Z-INDEX: 102;
LEFT: 408px;
POSITION: absolute;
TOP: 232px" ms_positioning="text2D">
<P><FONT size="3">自动刷新页面</FONT></P>
</DIV>
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-12-02
把load里边的代码写成一个函数调用
第2个回答  推荐于2017-09-03
public void Bind()
{
this.selectStr = "select * from RoomType "; ;
this.sqlConnection = new SqlConnection(HotelSystem.Connection.ConnString);
try
{

if (this.sqlConnection.State == ConnectionState.Closed)
this.sqlConnection.Open();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(selectStr, sqlConnection);
sda.Fill(ds);

int i;
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["RoomType"].ToString();
dataGridView1.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["Area"].ToString();
dataGridView1.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["Bed"].ToString();
dataGridView1.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i]["Price"].ToString();
dataGridView1.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i]["AC"].ToString();
dataGridView1.Rows[i].Cells[5].Value = ds.Tables[0].Rows[i]["TV"].ToString();
dataGridView1.Rows[i].Cells[6].Value = ds.Tables[0].Rows[i]["Phone"].ToString();
dataGridView1.Rows[i].Cells[7].Value = ds.Tables[0].Rows[i]["BR"].ToString();

}

}
catch (System.Exception E)
{
Console.WriteLine(E.ToString());
this.sqlConnection.Close();

}
finally
{
this.sqlConnection.Close();
}
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if (this.sqlConnection2.State == ConnectionState.Closed)
this.sqlConnection2.Open();
this.sqlCommand1.ExecuteNonQuery();
MessageBox.Show("新建成功!");
Bind();
}catch()
{
return;
}

}本回答被提问者采纳
第3个回答  2010-12-02
主页面:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using JH.Game.ServiceLibrary.Bll;
using JH.Game.ServiceLibrary.Model;
using System.Data.SqlClient;
using JH.Game.ServiceLibrary;

public partial class Manage_Examin_FillBlankManage : System.Web.UI.Page
{
private int moduleid = 12;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

if (this.Session["UserName"] == null)
{
Response.Write(" <script type=text/javascript> alert('您没有登录或登录超时,请重新登录!');window.top.opener=null; window.top.close(1);window.open('../../Login.aspx'); </script> ");
}
else
{
this.form1.Visible = false;
Authority(moduleid);
}
Bind();
Bind1();
Session.Remove("newdb");
}
}

public void Authority(int moduleid)
{
Userinfo users = new Userinfo();
UserinfoBll usersbll = new UserinfoBll();
string UserName = Session["UserName"].ToString();
users = usersbll.GetModelByName(UserName);
int uid = users.id;
UserRole usersrole = new UserRole();
UserRoleBll usersrolebll = new UserRoleBll();
usersrole = usersrolebll.GetModelByUser(uid);
if (usersrole == null)
{
Response.Write("<script> alert('您暂无该权限!');location='firstWeb.aspx';</script>");
}
else
{
string rid = usersrole.urroleid.ToString();
RoleuserAuth roleuAuth = new RoleuserAuth();
RoleuserAuthBll roleuAuthBll = new RoleuserAuthBll();
roleuAuth = roleuAuthBll.GetModelById(Convert.ToInt32(rid), moduleid);
if (roleuAuth == null)
{
Response.Write("<script> alert('您暂无该权限!');location='firstWeb.aspx';</script>");
}
else
{
string flag = roleuAuth.flag.ToString();
if (flag.Equals("0"))
{
this.form1.Visible = true;
Bind();
}
else
{
Response.Write("<script> alert('您暂无该权限!');location='firstWeb.aspx';</script>");
}
}
}
}

FillBlankProblem fillbleanproblem = new FillBlankProblem();
FillBlankProblemBll fillbleanproblemBll = new FillBlankProblemBll();
public void Bind()
{
DataSet da = fillbleanproblemBll.Gets();
da.AcceptChanges();
this.GvExamin.DataSource = da;
this.GvExamin.DataBind();
this.ddlCurrentPage.Items.Clear();

for (int i = 1; i <= this.GvExamin.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
this.ddlCurrentPage.SelectedIndex = this.GvExamin.PageIndex;

if (da.Tables[0].Rows.Count == 0)
{
this.notxt.Text = "没有相关数据!";
}

}
public void Bind1()
{
CourseBll courseBll = new CourseBll();
DataSet da = courseBll.Gets();
this.ddlCourse.DataSource = da;
ddlCourse.DataValueField = "ID";
ddlCourse.DataTextField = "Name";
ddlCourse.DataBind();
ddlCourse.Items.Insert(0, new ListItem("--请选择--", ""));
}

protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
{

//ddlCourse.Items.Remove(ddlCourse.Items.FindByText("--请选择--"));
DataSet res;
string id = this.ddlCourse.SelectedValue;
if (this.ddlCourse.SelectedIndex == 0)
{
res = fillbleanproblemBll.Gets();
}
else
{
res = fillbleanproblemBll.GetByID(int.Parse(id));
}
this.GvExamin.DataSource = res;
this.GvExamin.DataBind();

string newdb = "123";
Session.Add("newdb", newdb);
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.GvExamin.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}

if (this.GvExamin.Rows.Count != 0)
{
this.ddlCurrentPage.SelectedIndex = this.GvExamin.PageIndex;
}
else
{

this.notxt.Text = "没有相关数据!";
}
}

protected void GvExamin_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GvExamin.PageIndex = e.NewPageIndex;
GvExamin.DataBind();
Bind();
}

protected void GvExamin_RowDataBound(object sender, GridViewRowEventArgs e)
{
this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GvExamin.PageIndex + 1, this.GvExamin.PageCount);
//遍历所有行设置边框样式
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color:Black";
}
//用索引来取得编号
if (e.Row.RowIndex != -1)
{
int id = GvExamin.PageIndex * GvExamin.PageSize + e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}

if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到每项时颜色交替效果
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#bfe2ff';this.style.color='#8C4510'");
e.Row.Attributes["style"] = "Cursor:hand";
}

//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
}
}
}
protected void ddlCurrentPage_SelectedIndexChanged(object sender, EventArgs e)
{
this.GvExamin.PageIndex = this.ddlCurrentPage.SelectedIndex;
Bind();
if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}

protected void lnkbtnFrist_Click(object sender, EventArgs e)
{
this.GvExamin.PageIndex = 0;
Bind();

if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this.GvExamin.PageIndex > 0)
{
this.GvExamin.PageIndex = this.GvExamin.PageIndex - 1;
Bind();

if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this.GvExamin.PageIndex < this.GvExamin.PageCount)
{
this.GvExamin.PageIndex = this.GvExamin.PageIndex + 1;
Bind();

if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}
}
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.GvExamin.PageIndex = this.GvExamin.PageCount;
Bind();

if (Session["newdb"] == "123")
{
ddlCourse_SelectedIndexChanged(sender, e);
}
}
protected void GvExamin_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(GvExamin.DataKeys[e.RowIndex].Value.ToString());
int res = fillbleanproblemBll.Delete(ID);
if (res > 0)
{
GvExamin.EditIndex = -1;
Bind();
}
}
}

添加页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JH.Game.ServiceLibrary.Bll;
using JH.Game.ServiceLibrary.Model;
using System.Data;
public partial class Manage_Examin_FillBlankAdd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind1();
}
}

public void Bind1()
{
CourseBll courseBll = new CourseBll();
DataSet da = courseBll.Gets();
this.ddlCourse.DataSource = da;
this.ddlCourse.DataValueField = "ID";
this.ddlCourse.DataTextField = "Name";
this.ddlCourse.DataBind();
ddlCourse.Items.Insert(0, new ListItem("--请选择--", ""));
}

protected void btnSave_Click(object sender, EventArgs e)
{
FillBlankProblem param = new FillBlankProblem();
FillBlankProblemBll fillblankProblemBll = new FillBlankProblemBll();
int id = Convert.ToInt32(this.ddlCourse.SelectedValue);
param.FrontTitle = txtFrontTitle.Text.ToString();
param.Answer = txtAnswer.Text.ToString();
param.CourseID = id;
int res = fillblankProblemBll.Add(param);
if (res > 0)
{
MessageBox.Show(this.btnSave, "添加成功");
this.txtFrontTitle.Text = "";
this.txtAnswer.Text = "";
}
else
{
MessageBox.Show(this.btnSave, "添加失败");
Bind1();

}
}
protected void btnClen_Click(object sender, EventArgs e)
{
this.txtFrontTitle.Text = "";
this.txtAnswer.Text = "";
}
protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
{
ddlCourse.Items.Remove(ddlCourse.Items.FindByText("--请选择--"));
}
}
第4个回答  2010-12-02
你要刷新可以把绑定数据库写到一个方法里啊!

然后重新绑定就可以的
相似回答