C# 方法调用,刷新界面

我在打开界面的时候写了一个方法,为RoomType_Load,往表格里写入数据publicvoidRoomType_Load(objectsender,EventArgs... 我在打开界面的时候写了一个方法,为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后面,这样,在新建成功的时候,调用那个方法,就能刷新出新建的记录。
不知道怎么写,求高手指点下
大师们能说的具体点吗,本人写代码能力不是很强,只会用最简单最笨最繁琐的方法,调用方法和一楼所说的绑定数据库,不是很会弄。能说的具体点吗,或者给个类似的例子参考下
展开
 我来答
可以叫我表哥
推荐于2017-10-03 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
采纳数:25897 获赞数:1464972
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。

向TA提问 私信TA
展开全部
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>
mach369
2010-12-02 · TA获得超过274个赞
知道小有建树答主
回答量:873
采纳率:0%
帮助的人:431万
展开全部
把load里边的代码写成一个函数调用
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zlkzlpzhao
推荐于2017-09-03 · TA获得超过191个赞
知道答主
回答量:205
采纳率:0%
帮助的人:130万
展开全部
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;
}

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
宋高通
2010-12-02 · 超过89用户采纳过TA的回答
知道答主
回答量:242
采纳率:0%
帮助的人:175万
展开全部
主页面: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("--请选择--"));
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
踏浪帅
2010-12-02 · TA获得超过319个赞
知道小有建树答主
回答量:1900
采纳率:0%
帮助的人:1313万
展开全部
你要刷新可以把绑定数据库写到一个方法里啊!

然后重新绑定就可以的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式