dropdownlist update数据这么写不对么?
datagrid中的dropdownlist 在update数据库的时候总是出错 谢谢哥哥 姐姐帮我看看
'更新的事件
sub mydatagrid_update(sender as object,e as datagridcommandeventargs)
strCon = ConfigurationSettings.AppSettings( "strCon ")
conn = new sqlconnection(strCon)
dim txtid,txtName,txtPassword,txtClsid,txtStyid,txtEmail,txtIdc,txtdate as textbox
txtName = e.item.cells(3).controls(0)
txtPassword = e.item.cells(4).controls(0)
dim txtRl as dropdownlist
txtRl = e.item.cells(5).controls(1)
txtClsid = e.item.cells(6).controls(0)
txtStyid = e.item.cells(7).controls(0)
txtEmail = e.item.cells(8).controls(0)
txtIdc = e.item.cells(9).controls(0)
dim strSql as String
strSql= "Update users Set User_name= ' " & txtName.text & " ',User_password= ' " & txtPassword.text & " ' ,User_roles= " & CInt(txtRl.selecteditem.text) & ",User_clsid= " & Clng(txtClsid.text) & ",User_styid= " & CInt(txtStyid.text) & ",User_email= ' " & txtEmail.text & " ',User_idc= ' " & txtIdc.text & " ' where user_id = " & mydatagrid.datakeys(CInt(e.item.itemindex))
dim cmd as sqlcommand
cmd = new sqlcommand(strSql,conn)
dim exp as exception
try
conn.open()
cmd.executenonquery()
conn.close()
message.text= "操作已成功! "
catch exp
message.text = "更新时出现错误 "
end try
mydatagrid.edititemindex = -1
call binddata()
end sub
在页面中
<form runat= "server ">
<asp:datagrid id = "mydatagrid " headerstyle-backcolor = "#aaaadd " width= "95% "
allowpaging= "true " pagesize= "40 " onpageindexchanged= "mydatagrid_page "
pagerstyle-horizontalalign= "right " pagerstyle-mode= "numericpages "
oneditcommand= "mydatagrid_edit " oncancelcommand= "mydatagrid_cancel " ondeletecommand= "mydatagrid_delete "
onupdatecommand= "mydatagrid_update " datakeyfield= "user_id "
autogeneratecolumns = "false " runat= "server " >
<Columns>
<asp:editcommandcolumn edittext= "编辑 " updatetext= "更新 " canceltext= "取消 " itemstyle-wrap= "false "/>
<asp:buttoncolumn text= "删除 " commandName= "delete " itemstyle-wrap= "false "/>
<asp:boundcolumn headertext= "用户编号 " datafield= "user_id " readonly= "true "/>
<asp:boundcolumn headertext= "用户名 " datafield= "user_name " />
<asp:boundcolumn headertext= "密码 " datafield= "user_password " />
<asp:templatecolumn headertext= "用户级别 " >
<itemtemplate>
<asp:label text= ' <%#container.dataitem( "user_roles ")%> ' runat= "server " />
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist id= "txtRl " selectedindex= ' <%#container.dataitem( "user_roles ")-1%> 'runat= "server ">
<asp:listiten> 1 </asp:listiten>
<asp:listiten> 2 </asp:listiten>
<asp:listiten> 3 </asp:listiten>
<asp:listiten> 4 </asp:listiten>
</asp:dropdownlist>
</edititemtemplate>
</asp:templatecolumn>
<asp:boundcolumn headertext= "班级编号 " datafield= "user_clsid " />
<asp:boundcolumn headertext= "学号 " datafield= "user_styid " />
<asp:boundcolumn headertext= "Email " datafield= "user_email " />
<asp:boundcolumn headertext= "身份证号 " datafield= "user_idc " />
<asp:boundcolumn headertext= "注册时间 " datafield= "user_date " DataFormatString= "{0:dd-MM-yyyy} " readonly= "true "/>
</Columns>
</asp:datagrid>
</form>
[解决办法]
跟踪一下strSql,然后在查询分析器中运行一下,
[解决办法]
e.item.cells(5).controls(1)==这个cell里面有没有两个控件?
[解决办法]
编辑的时候,指定editindex然后绑定,就会出现dropdownlist了
[解决办法]
this.mydatagrid.EditIndex = e.newEditIndex;this.mydatagrid.DataBind();