[ASP.NET]GridView新增資料(考試網頁)

摘要:[ASP.NET]GridView新增資料(考試網頁)

若想做一個網頁給人做線上測驗,可以利用網頁的方式做出來

GridView自己寫後置程式新增資料功能,GridView本來就沒有Insert事件,要觸發此事件可以自己寫Button,Insert與Update很類似,但Insert要新增DataRow

1.      拉一個GridView

2.      GridView搭配一個DBinit()自己寫的資料來源

3.      GridView加入RadioBoxList,可以擇A,B,C,D

4.      加入一個Save Button

5.      Insert Code寫入 Button Click事件

SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["HRMSConnectionString"].ConnectionString);

SqlDataAdapter adpter = new SqlDataAdapter();

adpter.InsertCommand = new SqlCommand("INSERT INTO [zHrms_N_EXAM_ANS_HIST] ([FAB_ID], [TYPE], [CERT_KEY], [QUES_KEY], [EMP_ID], [ANS], [ANS_TARGET],[POINT], [UPDATED_DATE], [UPDATED_ID]) VALUES (@FAB_ID, @TYPE, @CERT_KEY, @QUES_KEY, @EMP_ID, @ANS ,@ANS_TARGET, @POINT ,getdate() , @UPDATED_ID)", Conn);

adpter.InsertCommand.Parameters.AddWithValue("FAB_ID", ((Label)GridView1.Rows[i].FindControl("Label1")).Text);

  RadioButtonList rrl = (RadioButtonList)GridView1.Rows[i].Cells[6].FindControl("RadioButtonList1");

   

//參數太多省略

DataSet ds = new DataSet();

adpter.SelectCommand = new SqlCommand(@"SELECT * FROM [HRMS].[dbo].[zHrms_N_EXAM_ANS_HIST]", Conn);

adpter.Fill(ds,"test");

DataRow new_row = ds.Tables["test"].NewRow();

new_row["FAB_ID"] = ((Label)GridView1.Rows[i].FindControl("Label1")).Text;

new_row["ANS"] = rrl.SelectedValue.ToString();

//參數太多省略

ds.Tables["test"].Rows.Add(new_row);

adpter.Update(ds, "test");