选择显示字体大小

强化datagrid web 控件的编辑接口


接下来的议题比较复杂,不过详细研究了解后可以产生亲和力较佳的使用者接口。datagrid web控件在进入编修模式时,可以自订一些可视化的输入组件,代替使用者在textbox 中输入。这样一来可以让使用者快速轻松输入外,也可以避免一些输入的错误。首先我们来看看加强后的编修画面,我们利用dropdownlist web 控件以及checkbox web 控件,协助使用者输入性别、血型、星座以及是否订阅电子报,如下画面所示:

按下编辑选项后,提供可视化的工具供使用者使用:


使用者修改完毕后,数据已经更新:


上述范例画面的完整程序代码如下所示:

<%@import namespace=system.data.ado%>
<%@import namespace=system.data%>
<html>
<form runat=&quot;server&quot;>
<asp:datagrid id=&quot;dga&quot; allowpaging=&quot;true&quot; pagesize=&quot;5&quot;
onpageindexchanged=&quot;dga_pagechg&quot; runat=&quot;server&quot;
pagerstyle-mode=&quot;numericpages&quot; bordercolor=&quot;#808080&quot;
headerstyle-backcolor=&quot;#0066cc&quot; headerstyle-forecolor=&quot;white&quot;
headerstyle-horizontalalign=&quot;center&quot;
autogeneratecolumns=&quot;false&quot;
oneditcommand=&quot;dga_ecmd&quot; onupdatecommand=&quot;dga_ucmd&quot;
oncancelcommand=&quot;dga_ccmd&quot; >
<property name=&quot;edititemstyle&quot;>
<asp:tableitemstyle horizontalalign=&quot;center&quot; backcolor=&quot;#d1dceb&quot;/>
</property>
<property name=&quot;itemstyle&quot;>
<asp:tableitemstyle horizontalalign=&quot;center&quot;/>
</property>
<property name=&quot;columns&quot;>
<asp:editcommandcolumn
headertext=&quot;编辑&quot;
edittext=&quot;<img border=0 src=edit.gif>&quot;
updatetext=&quot;<img border=0 src=save.gif>&quot;
canceltext=&quot;<img border=0 src=cancel.gif>&quot;/>
<asp:templatecolumn>
<template name=&quot;headertemplate&quot;>
姓名
</template>
<template name=&quot;itemtemplate&quot;>
<asp:image imageurl=&quot;ico7.gif&quot; runat=&quot;server&quot;/>
<%#container.dataitem(&quot;username&quot;)%>
</template>
<template name=&quot;edititemtemplate&quot;>
<asp:image imageurl=&quot;ico8.gif&quot; runat=&quot;server&quot;/>
<asp:textbox id=&quot;txtname&quot;
text='<%#container.dataitem(&quot;username&quot;)%>'
runat=&quot;server&quot;/>
</template>
</asp:templatecolumn>
<asp:templatecolumn>
<template name=&quot;headertemplate&quot;>
性别
</template>
<template name=&quot;itemtemplate&quot;>
<%#container.dataitem(&quot;usersex&quot;)%>
</template>
<template name=&quot;edititemtemplate&quot;>
<asp:radiobuttonlist id=&quot;rblsex&quot; repeatdirection=&quot;horizontal&quot;
selectedindex='<%#getproperty(&quot;rblsex&quot;)%>'
runat=&quot;server&quot;>
<asp:listitem>男</asp:listitem>
<asp:listitem>女</asp:listitem>
</asp:radiobuttonlist>
</template>
</asp:templatecolumn>
<asp:templatecolumn>
<template name=&quot;headertemplate&quot;>
血型
</template>
<template name=&quot;itemtemplate&quot;>
<%#container.dataitem(&quot;userblood&quot;)%>
</template>
<template name=&quot;edititemtemplate&quot;>
<asp:dropdownlist id=&quot;ddlblood&quot; datasource='<%#arblood%>'
selectedindex='<%#getproperty(&quot;ddlblood&quot;)%>' runat=&quot;server&quot;>
</asp:dropdownlist>
</template>
</asp:templatecolumn>
<asp:templatecolumn>
<template name=&quot;headertemplate&quot;>
星座
</template>
<template name=&quot;itemtemplate&quot;>
<%#container.dataitem(&quot;usercons&quot;)%>
</template>
<template name=&quot;edititemtemplate&quot;>
<asp:dropdownlist id=&quot;ddlcons&quot; datasource='<%#arcons%>'
selectedindex='<%#getproperty(&quot;ddlcons&quot;)%>' runat=&quot;server&quot;/>
</template>
</asp:templatecolumn>
<asp:templatecolumn>
<template name=&quot;headertemplate&quot;>
是否订阅电子报
</template>
<template name=&quot;itemtemplate&quot;>
<%#container.dataitem(&quot;ordernews&quot;)%>
</template>
<template name=&quot;edititemtemplate&quot;>
<asp:checkbox id=&quot;cbordernews&quot;
checked=<%#getproperty(&quot;cbordernews&quot;)%>
runat=&quot;server&quot;/>
</template>
</asp:templatecolumn>
</property>
</asp:datagrid>
</form>
<script language=&quot;vb&quot; runat=&quot;server&quot;>
dim dsca as adodatasetcommand=new adodatasetcommand(&quot;select * from
users&quot;, _
&quot;provider=microsoft.jet.oledb.4.0;data
source=c:\.netpub\wwwroot\cr\ch08\myweb.mdb&quot;)
dim dsdataset as dataset=new dataset
dim dtdatatable as datatable
dim arcons() as string={&quot;天秤座&quot;,&quot;天蝎座&quot;,&quot;水瓶座&quot;,&quot;巨蟹座&quot;,&quot;金牛座&quot;,&quot;
射手座&quot;, _
&quot;处女座&quot;,&quot;狮子座&quot;,&quot;双子座&quot;,&quot;双鱼座&quot;,&quot;魔羯座&quot;,&quot;牡羊座&quot;}
dim arblood() as string={&quot;a&quot;,&quot;b&quot;,&quot;o&quot;,&quot;ab&quot;}
sub page_load(sender as object, e as eventargs)
if page.ispostback=false then
bindgrid()
end if
end sub
sub bindgrid() '本程序用来执行控件和数据源间的系结
dsca.filldataset(dsdataset,&quot;users&quot;)
dtdatatable=dsdataset.tables(&quot;users&quot;)
dga.datasource=dtdatatable.defaultview
page.databind()
end sub
sub dga_pagechg(sender as object, e as datagridpagechangedeventargs)
bindgrid()
end sub
sub dga_ecmd(sender as object, e as datagridcommandeventargs)
dga.edititemindex=e.item.itemindex
bindgrid()
end sub
sub dga_ucmd(sender as object, e as datagridcommandeventargs) '更新数
据源的程序
dim shtr as short=(dga.currentpageindex * dga.pagesize) +
e.item.itemindex
dsca.filldataset(dsdataset,&quot;users&quot;)
dtdatatable=dsdataset.tables(&quot;users&quot;)
dim objcontrol as object
objcontrol=e.item.findcontrol(&quot;txtname&quot;)
dtdatatable.rows(shtr)(&quot;username&quot;)=objcontrol.text
objcontrol=e.item.findcontrol(&quot;rblsex&quot;) '传回rblsex 的参考
dtdatatable.rows(shtr)(&quot;usersex&quot;)=objcontrol.selecteditem.text '将
使用者的选择更新
'至datatable 中所
对应的字段
objcontrol=e.item.findcontrol(&quot;ddlblood&quot;)
dtdatatable.rows(shtr)(&quot;userblood&quot;)=objcontrol.selecteditem.text
objcontrol=e.item.findcontrol(&quot;ddlcons&quot;)
dtdatatable.rows(shtr)(&quot;usercons&quot;)=objcontrol.selecteditem.text
objcontrol=e.item.findcontrol(&quot;cbordernews&quot;)
dtdatatable.rows(shtr)(&quot;ordernews&quot;)=iif(objcontrol.checked,&quot;是&quot;,&quot;否
&quot;)
dsca.update(dsdataset,&quot;users&quot;)
dga.edititemindex=-1
bindgrid()
end sub
sub dga_ccmd(sender as object, e as datagridcommandeventargs)
dga.edititemindex=-1
bindgrid()
end sub
'进入编辑模式时, 让编辑数据的控件显示正确值的程序
function getproperty(byval strctlname as string)
dim shtr as short=(dga.currentpageindex * dga.pagesize) +
dga.edititemindex
if shtr>-1 then
dim shti as short
select case strctlname '判断控件名称
case &quot;rblsex&quot; '传回性别
return iif(dtdatatable.rows(shtr)(&quot;usersex&quot;)=&quot;男&quot;,0,1) '如果等于男
则传回0,否则传回1
case &quot;ddlblood&quot; '传回血型
for shti=0 to 3
if dtdatatable.rows(shtr)(&quot;userblood&quot;)=arblood(shti) then
'判断资料表中的资
return shti '料在数组的索引
exit function
end if
next
case &quot;ddlcons&quot; '传回星座
dim strcons=dtdatatable.rows(shtr)(&quot;usercons&quot;)
for shti=0 to 11
if arcons(shti)=strcons then
return shti
exit function
end if
next
case &quot;cbordernews&quot; '传回是否订阅电子报
return iif(dtdatatable.rows(shtr)(&quot;ordernews&quot;)=&quot;是
&quot;,&quot;true&quot;,&quot;false&quot;)
end select
end if
end function
</script>
</html>

上述程序代码我们利用定义每个字段的edititemtemplate,并且利用数据系结叙述取得该字段数据的正确值。例如下列宣告了性别字段的编辑样版,使用者在进入编辑模式时以radiobuttonlist web 控件来让使用者选择:

<template name=&quot;edititemtemplate&quot;>
<asp:radiobuttonlist id=&quot;rblsex&quot; repeatdirection=&quot;horizontal&quot;
selectedindex='<%#getproperty(&quot;rblsex&quot;)%>'
runat=&quot;server&quot;>
<asp:listitem>男</asp:listitem>
<asp:listitem>女</asp:listitem>
</asp:radiobuttonlist>
</template>

而getproperty 程序可以检查每个控件所应该显示的状态值,只要在呼叫本程序时传入该控件的名称,这个程序就会到datatable 中将控件的适当状态设定传回,如下程序代码片段所示:

function getproperty(byval strctlname as string)
dim shtr as short=(dga.currentpageindex * dga.pagesize) +
dga.edititemindex
if shtr>-1 then
dim shti as short
select case strctlname '判断控件名称
case &quot;rblsex&quot; '传回性别
return iif(dtdatatable.rows(shtr)(&quot;usersex&quot;)=&quot;男&quot;,0,1) '如果等于
男则传回0,
'否则传回1

上述程序代码先取得记录在datatable 中的绝对地址,再判断使用者所传入的控件名称是否为rblsex。若为rblsex 所执行的数据系结动作,则传回iif 函式的判断结果。iif 的使用语法如下所示:

变量=iif(条件判断式, 成立所传回的资料, 不成立所传回的资料)

iif 中的条件判断式的最结果若为true,则传回成立所传回的资料;若不成立则传回不成立所传回的资料。所以若目前usersex 这个字段的值若为「男」则传回数值0,若不成立则为传回1;此时radiobuttonlist web 控件收到后即可显示数据正确的选项,其它栏控件取得正确状态的方式也是如此。   


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons