欢迎光临
我们一直在努力

数据绑定的使用-.NET教程,数据库应用

建站超值云服务器,限时71元/月

如果要在运行时将数据值分配给一个控件属性,则可以响应由该控件引发的数据绑定事件。在事件代码中,可以执行任何逻辑,无论是否要求该逻辑来分配属性值。

注意 在设计时,可以创建在运行时计算的数据绑定表达式,以将值分配给控件属性。有关详细信息,请参阅在设计时数据绑定单值 web 服务器控件。

在运行时数据绑定单值控件

1. 确保您的 web 窗体页中的代码或者调用您正使用的控件的 databind 方法,或者调用此页的该方法。将引发数据绑定事件以响应此方法调用。

注意 通常不需要在每个往返行程中都调用 databind 方法(即在页初始化阶段不需要检查回发),因为这样做会替换控件中的值。

2. 为控件的 databinding 事件创建事件处理程序。

3. 在事件处理程序中,根据需要添加代码以将值分配给控件的属性。

下面的示例说明如何在运行时执行简单的数据绑定。web 窗体页包含两个 textbox web 服务器控件,显示来自 sql server pubs 数据库中标题表的信息。“上一个”和“下一个”按钮设置在会话状态中存储的“记录位置”变量。只要用户单击这些按钮以更改记录位置,就调用该页的 databind 方法,这将引发用于填充文本框的事件。

visual basic

private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load

sqldataadapter1.fill(dstitles1)

session("recordpos") = 0

me.databind()

end sub

private sub btnnext_click(byval sender as system.object, byval e as system.eventargs) handles btnnext.click

dim recordpos as integer = ctype(session("recordpos"), integer)

recordpos += 1

if recordpos > dstitles1.titles.count then

recordpos -= 1

end if

session("recordpos") = recordpos

me.databind()

end sub

private sub btnprevious_click(byval sender as system.object, byval e as system.eventargs) handles btnprevious.click

dim recordpos as integer = ctype(session("recordpos"), integer)

if recordpos > 0 then

recordpos -= 1

end if

session("recordpos") = recordpos

me.databind()

end sub

private sub textbox1_databinding(byval sender as object, byval e as system.eventargs) handles textbox1.databinding

dim recordpos as integer = ctype(session("recordpos"), integer)

textbox1.text = dstitles1.titles(recordpos).title_id

textbox2.text = dstitles1.titles(recordpos).title

end sub

// c#

private void page_load(object sender, system.eventargs e)

{

sqldataadapter1.fill(dstitles1);

session["recordpos"] = 0;

this.databind();

}

private void btnnext_click(object sender, system.eventargs e)

{

int recordpos = (int) session["recordpos"];

recordpos++;

if (recordpos > dstitles1.titles.count)

{

recordpos–;

}

session["recordpos"] = recordpos;

this.databind();

}

private void btnprevious_click(object sender, system.eventargs e)

{

int recordpos = (int) session["recordpos"];

if (recordpos > 0)

{

recordpos–;

}

session["recordpos"] = recordpos;

this.databind();

}

private void textbox1_databinding(object sender, system.eventargs e)

{

int recordpos = (int) session["recordpos"];

textbox1.text = dstitles1.titles[recordpos].title_id;

textbox2.text = dstitles1.titles[recordpos].title;

}

下面的示例将 label 控件绑定到通过执行 sql 命令返回的值。该命令对象(对于 sql server 是显式的,由已命名的参数指示)以前已通过名为 @empid 的单个参数定义。

用户输入雇员 id 并单击按钮。该按钮调用 label 控件的 databind 方法,这将引发 databinding 事件。在事件处理程序中,代码获取雇员 id,将其设置为 sqlcommand 对象的参数,并运行该命令的 sql 语句。该命令返回一个数据阅读器对象,其中具有一个记录,代码读取它并显示在标签中。

visual basic

private sub btnlogin_click(byval sender as system.object, byval e as system.eventargs) handles btnlogin.click

label1.databind()

end sub

private sub label1_databinding(byval sender as object, byval e as system.eventargs) handles label1.databinding

sqlcommand1.parameters("@empid").value = loginid.text

sqlcommand1.commandtext = _

"select id, fname, lname from employee where (id = @empid)"

sqlcommand1.commandtype = commandtype.text

dim dr as system.data.sqlclient.sqldatareader

sqlconnection1.open()

dr = sqlcommand1.executereader()

if dr.read() then

label1.text = "welcome, " & ctype(dr(1), string) & " " & ctype(dr(2), string)

else

label1.text = "no employee found with id of " & loginid.text

end if

sqlconnection1.close()

end sub

// c#

private void btnlogin_click(object sender, system.eventargs e)

{

label1.databind();

}

private void label1_databinding(object sender, system.eventargs e)

{

sqlcommand1.parameters["@empid"].value = loginid.text;

sqlcommand1.commandtext =

"select id, fname, lname from employee where (id = @empid)";

sqlcommand1.commandtype = commandtype.text;

system.data.sqlclient.sqldatareader dr;

sqlconnection1.open();

dr = sqlcommand1.executereader();

if (dr.read())

{

label1.text = "welcome, " + dr[1] + " " + dr[2];

}

else

{

label1.text = "no employee found with id of " + loginid.text;

}

sqlconnection1.close();

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 数据绑定的使用-.NET教程,数据库应用
分享到: 更多 (0)

相关推荐

  • 暂无文章