2012-07-30 21 views
2

我必須插入喜歡這幅畫上insert.aspx如何在vb.net asp.net上自動檢查複選框?

enter image description here

一些數據值和值以及如何顯示這樣的更新數據update.aspx

enter image description here

所以如何使update.aspx上的複選框自動檢查insert.aspx

on Insert.aspx.vb

保護小組insertdata() 昏暗CLS作爲新的連接

cls.openconnections() 
    Dim cmd As New SqlClient.SqlCommand 
    cmd.Connection = cls.cn 

    cmd.CommandType = CommandType.StoredProcedure 
    cmd.CommandText = "sp_inserttrainer" 


    cmd.Parameters.AddWithValue("@id", txtKode.Text) 
    cmd.Parameters.AddWithValue("@nama", txtnama.Text) 
    cmd.Parameters.AddWithValue("@status", txtstatus.SelectedValue) 
    cmd.Parameters.AddWithValue("@alamat", txtalamat.Text) 
    cmd.Parameters.AddWithValue("@telp", txttel.Text) 
    cmd.Parameters.AddWithValue("@hp", txthp.Text) 
    cmd.Parameters.AddWithValue("@email", txtemail.Text) 

    cmd.ExecuteNonQuery() 
    cls.closeconnection() 

End Sub 
Protected Sub savedatamateri() 


    Dim cbterpilih As Boolean = False 
    Dim cb As CheckBox = Nothing 
    Dim n As Integer = 0 

    Do Until n = gridsecond.Rows.Count 
     cb = gridsecond.Rows.Item(n).FindControl("chkStatus") 
     If cb IsNot Nothing AndAlso cb.Checked Then 
      cbterpilih = True 


      Dim cls As New connections 
      cls.openconnections() 
      Dim cmd As New SqlClient.SqlCommand 
      cmd.Connection = cls.cn 

      cmd.CommandText = "Insert Into m_trainer_detil (trainer_id, materi_id)" 
      cmd.CommandText &= "values(@triner, @materi)" 

      Dim com As HiddenField = CType(gridsecond.Rows(n).FindControl("HiddenField2"), HiddenField) 

      cmd.Parameters.AddWithValue("@triner", txtKode.Text) 
      cmd.Parameters.AddWithValue("@materi", com.Value) 


      cmd.ExecuteNonQuery() 
      cls.closeconnection() 

     End If 

     n += 1 
    Loop 
End Sub 

Protected Sub insert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles insert.Click 
     If txtalamat.Text = "" Then 
      labelran.Text = "Tolong isi Alamat" 
     ElseIf txtemail.Text = "" Then 
      labelran.Text = "Tolong isi Email" 
     ElseIf txtnama.Text = "" Then 
      labelran.Text = "Tolong isi Nama" 
     ElseIf txttel.Text = "" Then 
      labelran.Text = "Tolong isi Telepon" 
     ElseIf txthp.Text = "" Then 
      labelran.Text = "Tolong isi Handphone" 
     Else 
      insertdata() 
      savedatamateri() 
      listhendle() 
     End If 


    End Sub 

和update.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

     If Not Page.IsPostBack Then 
      listhendle() 
      listmateri() 

    End If 
End Sub 

Protected Sub listhendle() 
    Dim ds As New DataSet 
    Dim cls As New connections 

    ds = cls.returndataset("select * from [m_trainer] ") 

    If ds.Tables(0).Rows.Count = 0 Then 
     ds.Tables(0).Rows.Add(ds.Tables(0).NewRow) 
    End If 
    griddata.DataSource = ds 
    griddata.DataBind() 

End Sub 
Protected Sub listmateri() 
    Dim ds As New DataSet 
    Dim cls As New connections 

    ds = cls.returndataset("select * from [m_materi] ") 

    If ds.Tables(0).Rows.Count = 0 Then 
     ds.Tables(0).Rows.Add(ds.Tables(0).NewRow) 
    End If 
    gridsecond.DataSource = ds 
    gridsecond.DataBind() 

End Sub 


Protected Sub griddata_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles griddata.SelectedIndexChanging 

     Dim kode As HiddenField = CType(griddata.Rows(e.NewSelectedIndex).FindControl("HiddenField1"), HiddenField) 

     txtKode.Text = kode.Value 
     txtnama.Text = Replace(griddata.Rows(e.NewSelectedIndex).Cells(1).Text, " ", "") 
     txtalamat.Text = Replace(griddata.Rows(e.NewSelectedIndex).Cells(2).Text, " ", "") 
     txttel.Text = Replace(griddata.Rows(e.NewSelectedIndex).Cells(3).Text, " ", "") 
     txthp.Text = Replace(griddata.Rows(e.NewSelectedIndex).Cells(4).Text, " ", "") 
     txtstatus.SelectedValue = Replace(griddata.Rows(e.NewSelectedIndex).Cells(5).Text, " ", "") 
     txtemail.Text = Replace(griddata.Rows(e.NewSelectedIndex).Cells(6).Text, " ", "") 



    End Sub 

但我不知道如何與數據庫連接 馬泰謝謝

+0

我們非常感謝截圖,但是您的update.aspx代碼隱藏示例,尤其是關於如何填充update.aspx中最後一個表格的代碼會更加高度讚賞。 – rikitikitik 2012-07-30 07:24:53

+0

你是如何在insert.aspx中做到的?它是一個數據綁定列嗎?請添加一些代碼。 – 2012-07-30 07:25:10

回答

0

它看起來像你存儲在表m_trainer materi_id。所以看起來你需要在你的「gridSecond」GridView上設置一個OnRowDataBound事件。在OnRowDatabound事件中獲取所選trainer_id的數據集,並且如果該數據集中存在materi_id,則將其檢查值設置爲true。