80 likes | 234 Views
การ ใช้งาน GridView FormView การแสดงรูปภาพ. การแสดงรูปภาพผ่าน FormView. สร้าง datasource ติดต่อกับตาราง UserPic เพื่อเลือก username และ รูปภาพขึ้นมาแสดงผล โดยให้ค่าพารามิเตอร์ username มาจาก Session " SELECT * FROM [ UserPic ] WHERE ([username] = @username )”
E N D
การใช้งานGridViewFormViewการแสดงรูปภาพการใช้งานGridViewFormViewการแสดงรูปภาพ
การแสดงรูปภาพผ่าน FormView • สร้างdatasource ติดต่อกับตารางUserPicเพื่อเลือก username และรูปภาพขึ้นมาแสดงผล โดยให้ค่าพารามิเตอร์ username มาจาก Session "SELECT * FROM [UserPic] WHERE ([username] = @username)” • สร้างFormViewแล้วจับคู่กับ datasourceข้างต้น • สร้าง <asp:Image> ภายในFormViewโดย Edit DataBindingsดังรูป
สร้างตารางเพิ่มเติม • สร้างตารางโดยไปที่ Database explorer> table > add new table • กำหนดให้สร้างตารางStudentเก็บข้อมูลนักศึกษา
สร้างหน้า Student.aspx • สร้างDataSourceติดต่อกับตาราง Student โดยใช้คำสั่ง select * from student • กำหนดให้DataSourceสามารถเพิ่มคำสั่ง insert/update/delete ได้
หน้า Student.aspx • สร้างFormView เพื่อใช้แสดงผลข้อมูล Student • สามารถเข้าไปแก้ไข template ได้ที่ Edit Template
protectedvoidexeSQL(stringins_cmd) { try{ SqlCommandinsert_cmd=newSqlCommand(ins_cmd, dbcon.conn); insert_cmd.ExecuteNonQuery(); lbStatus.Text="เพิ่มข้อมูลสำเร็จ"; } catch (SqlExceptionse) { lbStatus.Text="เพิ่มข้อมูลไม่สำเร็จ "+se.ToString(); } }
protectedvoidButton1_Click(objectsender, EventArgse) { boolisPay; doublemid,final; chargrade; stringins_cmd; connect_db(); foreach (GridViewRowgvrinGridView1.Rows) { isPay= ((CheckBox)(gvr.FindControl("cbPay"))).Checked; if (isPay==true) { mid=Convert.ToDouble(((TextBox)(gvr.FindControl("tbMid"))).Text); final=Convert.ToDouble(((TextBox)(gvr.FindControl("tbFinal"))).Text); grade='A'; ins_cmd=“ เขียนคำสั่ง update………"; ins_cmd+=" where studentID='"+gvr.Cells[0].Text+"'"; exeSQL(ins_cmd); } } dbcon.closeDB(); GridView1.DataBind(); } protectedvoidconnect_db() {
protectedvoidGridView1_RowDataBound(objectsender, GridViewRowEventArgse) { charx; if (e.Row.RowType==DataControlRowType.DataRow) { x=Convert.ToChar( DataBinder.Eval(e.Row.DataItem, "grade")); if (x=='F') { e.Row.BackColor=System.Drawing.Color.Red; } } }