Visual_Basic 板


LINE

※ 引述《Lamfree (酷熱夏天)》之銘言: 感謝大大的回應,小弟這幾天也查有關findcontrol的用法 但卻不知該怎用ASP.NET寫,冏 因為查到資料大部分都是用JAVA或C#或VB.NET(跟ASP.NET還是有出入) 語法寫 所以很慚愧的又來問大大 可以簡述一下findcontrol跟control的轉換用法 及注意事項 感恩~~ 還有回F大大 我想control(0)應該是textbox 看了回覆後參閱書本,並沒有講的很詳細,不過我前後看的很久 應該沒錯吧 希望您對此有所了解...可以幫小弟解惑 : 小弟不才最近剛接觸練習用ASP.NET的VB語法寫了一個新增、刪除、修改的退貨表單 : 並用SQLserver2000 當資料庫 : 不知為何在寫修改部分(照書上模式打)ꔩ[王有禮老師的透視ASP.NET] : 在畫面修改完畢點選儲存時 : 卻出現"System.InvalidCastException指定的轉換無效" : 上網找資料 仍然毫無頭緒 : 行 25: for i = 0 to e.Item.Cells.Count - 2 : 行 26:=>有問題的地方 : strText = CType(e.Item.Cells(i).Controls(0), TextBox).text : 行 27: if strText <> "" then : 行 28: params(j) = strText : 堆疊追蹤: : [InvalidCastException: 指定的轉換無效。] : ASP.C_back_aspx.UpdateDataStore(DataGridCommandEventArgs e) in F:\Project_A\C_back.aspx:26 : ASP.C_back_aspx.b_form_Update(Object obj, DataGridCommandEventArgs e) in F:\Project_A\C_back.aspx:91 : System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109 : System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507 : System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26 : System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106 : System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26 : System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121 : System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115 : System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18 : System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138 : System.Web.UI.Page.ProcessRequestMain() +1292 : 這是全部程式碼 : <%@ Page Language = "VB" Debug="true"%> : <%@ Import Namespace="System.Data" %> : <%@ Import Namespace="System.Data.SQLClient" %> : <html> : <head> : <title>客戶退貨</title> : <script language="VB" runat="Server"> : Dim check_day As Date = Today : Dim Conn As new SQLConnection("server=localhost;uid=sa;pwd=;database=ICS") : '修改資料 : function UpdateDataStore( e as DataGridCommandEventArgs ) as Boolean : dim i as integer : dim j as integer = 0 : dim params(5) as string : dim strText as string : dim blnGo as boolean = true : for i = 0 to e.Item.Cells.Count - 2 : strText = CType(e.Item.Cells(i).Controls(0), TextBox).text : if strText <> "" then : params(j) = strText : j = j + 1 : else : blnGo = false : lblMessage.Text = lblMessage.Text & "欄位不可空白<p>" : end if : next : if not blnGo then : return false : exit function : end if : dim strSQL as string = "Update 退貨單 set" & _ : "退貨單編號 = '" & params(0) & "'," & _ : "退貨商品編號 = '" & params(1) & "'," & _ : "退貨商品數量 = " & params(2) & "," & _ : "退貨商品售價 = " & params(3) & "," & _ : " where 退貨商品編號 ='" & CType(e.Item.Cells(2).Controls(0), TextBox).Text & "'" : ExecuteStatement(strSQL) : return blnGo : end function : sub Page_Load(obj as Object, e as EventArgs) : if Not Page.IsPostBack then : FillDataGrid2() : end if : end sub : '顯示修改 : Sub FillDataGrid2(Optional EditIndex As integer = -1) : dim objCmd2 as new SQLCommand("select * from 退貨單", Conn) : dim objReader as SQLDataReader : try : objCmd2.Connection.Open() : objReader = objCmd2.ExecuteReader() : catch ex as Exception : lblMessage.Text = "錯誤的資料庫運算" : end try : b_form.DataSource = objReader : if not EditIndex.Equals(Nothing) then : b_form.EditItemIndex = EditIndex : end if : b_form.DataBind() : objReader.Close : objCmd2.Connection.Close() : End sub : '修改資料 : sub b_form_Edit(obj as object, e as DataGridCommandEventArgs) : FillDataGrid2(e.Item.ItemIndex) : end sub : '修改更新 : sub b_form_Update(obj as object, e as DataGridCommandEventArgs) : if UpdateDataStore(E) then : FillDataGrid2(-1) : end if : end sub : '修改取消 : sub b_form_Cancel(obj as object, e as DataGridCommandEventArgs) : FillDataGrid2(-1) : end sub : '新增 : Sub add_Click(Sender As object, e As EventArgs) : if Page.IsValid then : Dim strSQL as string = "Insert into 退貨單 (退貨單編號, 退貨日期, 退貨商品編號, 退貨商品數量, 退貨商品售價 )values " & _ : "('" & textbox0.text & "','" & check_day & "', '"& textbox1.text & "', '" & textbox2.text & "'," & _ : "'" & textbox3.text & "')" : ExecuteStatement(strSQL) : End If : '顯示表格 : FillDataGrid() : End Sub : '清除 : Sub clear_Click(Sender As object, e As EventArgs) : textbox0.text=" " : textbox1.text=" " : textbox2.text=" " : textbox3.text=" " : End Sub : '刪除資料 : Sub b_form_delete(obj As Object, e As DataGridCommandEventArgs) : Dim strSQL As string = "Delete From 退貨單 where 退貨單編號 = '" & e.Item.Cells(0).Text & "' " : ExecuteStatement(strSQL) : FillDataGrid() : End sub : '顯示退貨表 : Sub FillDataGrid(Optional EditIndex As integer = -1) : Dim myCommand As new SQLDataAdapter("select * from 退貨單 where 退貨日期 = ('" & check_day & "') ", Conn) : Dim dsC As DataSet = new DataSet() : myCommand.Fill(dsC, "退貨單") : b_form.DataSource = dsC.Tables("退貨單").DefaultView : b_form.DataBind() : End sub : Function ExecuteStatement(strSQL) : Dim objCmd as new SQLCommand(strSQL, Conn) : try : objCmd.Connection.Open() : objCmd.ExecuteNonQuery() : lblMessage.Text = "資料建檔完成!" : catch ex as Exception : lblMessage.Text = "建檔失敗!" : End try : objCmd.Connection.Close() : End function : </script> : </head> : <body> : <h2>客戶退貨</h2><hr> : <form id="Form1" runat="Server"> : <% : Response.Write("退貨日期 : " & check_day) : %><p></p> : <p> 退貨單編號 : : <asp:textbox id = "textbox0" Width="150px" runat="Server"/> : <asp:RequiredFieldValidator ControlToValidate="textbox0" : ErrorMessage="輸入退貨單編號" : Type="string" runat="server"/> : <p> 商品編號 : : <asp:textbox id = "textbox1" Width="150px" runat="Server"/> : <asp:RequiredFieldValidator ControlToValidate="textbox1" : ErrorMessage="輸入商品編號" : Type="string" runat="server"/> : <p> 商品數量: : <asp:textbox id = "textbox2" width="100px" runat="server"/> : <asp:RequiredFieldValidator ControlToValidate="textbox2" : ErrorMessage="輸入商品數量" : Type="Integer" runat="server"/> : <p> 商品售價 : : <asp:textbox id = "textbox3" width="100px" runat="server"/> : <asp:RequiredFieldValidator ControlToValidate="textbox3" : ErrorMessage="輸入商品售價" : Type="Integer" runat="server"/> : <p> <asp:button id = "add" Text="新增" OnClick="add_Click" runat="server"/> : <asp:button id = "clear" Text="清除" OnClick="clear_Click" runat="server"/> : <asp:Label ID="lblMessage" runat="server" /> : <asp:DataGrid id="b_form" runat="Server" BorderColor="black" CellPadding="4" Font-Size="8pt" : HeaderStyle-BackColor="#cccc99" ItemStyle-BackColor="#ffffff" : AlternatingItemStyle-BackColor="#cccccc" AutoGenerateColumns="False" : OnDeleteCommand="b_form_delete" : OnEditCommand="b_form_Edit" OnCancelCommand="b_form_Cancel" OnUpdateCommand="b_form_Update"> : <Columns> : <asp:BoundColumn HeaderText="退貨單編號" DataField="退貨單編號"/> : <asp:BoundColumn HeaderText="退貨日期" DataField="退貨日期"/> : <asp:BoundColumn HeaderText="商品編號" DataField="退貨商品編號"/> : <asp:BoundColumn HeaderText="商品數量" DataField="退貨商品數量"/> : <asp:BoundColumn HeaderText="商品售價" DataField="退貨商品售價"/> : <asp:ButtonColumn ButtonType="PushButton" HeaderText="刪除" Text="刪除" CommandName="delete"/> : <asp:EditCommandColumn EditText="修改" CancelText="還原" : UpdateText="儲存" HeaderText="編輯" ItemStyle-Wrap="False"/> : </Columns> : </asp:DataGrid> : </form></body></html> : 希望板上大大能幫小弟解惑 --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.248.81.160







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Soft_Job站內搜尋

TOP