在asp.net中,如何一次性更新datagrid中的所有记录呢?可以用如下的方法,首先,
要对datagrid中要更新的列建立模版列,比如:
asp:datagrid id="dgpopularfaqs" runat="server"
autogeneratecolumns="false"
...>
<columns>
<asp:boundcolumn datafield="faqid" itemstyle-width="10%"
itemstyle-horizontalalign="center" headertext="faq id" />
<asp:boundcolumn datafield="categoryname" headertext="category" />
<asp:templatecolumn headertext="question">
<itemtemplate>
<asp:textbox runat="server" id="txtdescription" columns="75"
text='<%# container.dataitem("description") %>' />
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="submitted by">
<itemtemplate>
<asp:textbox runat="server" id="txtsubmittedby"
text='<%# container.dataitem("submittedbyname") %>' />
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
之后,对datagrid进行一次遍历,
dim myconnection as new sqlconnection(connection string)
dim mycommand as new sqlcommand(strsql, myconnection)
dim dgi as datagriditem
for each dgi in dgpopularfaqs.items
'read in the primary key field
dim id as integer = convert.toint32(dgpopularfaqs.datakeys(dgi.itemindex))
dim question as string = ctype(dgi.findcontrol("txtdescription"), textbox).text
dim submittedby as string = ctype(dgi.findcontrol("txtsubmittedby"), textbox).text
'issue an update statement...
dim updatesql as string = "update tablename set question = @question, " & _
"submittedbyname = @submittedbyname where faqid = @id"
mycommand.parameters.clear()
mycommand.parameters.add("@question", question)
mycommand.parameters.add("@submittedbyname", submittedby)
mycommand.executenonquery()
next
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 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器