如何在vb.net asp.net上自动选中复选框?

时间:2012-07-30 07:19:07

标签: asp.net vb.net

我必须在insert.aspx

上插入一些数据值和值

enter image description here

我如何显示这样更新数据update.aspx

enter image description here

那么如何在update.aspx上设置复选框,就像在insert.aspx

上一样自动检查

在Insert.aspx.vb上

受保护的子插入数据()         Dim cls As New connections

    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

但我不知道如何将materi与数据库联系起来 谢谢

1 个答案:

答案 0 :(得分:0)

看起来您将materi_id存储在表m_trainer中。因此,您需要在“gridSecond”GridView上设置OnRowDataBound事件。在OnRowDatabound事件中,获取所选trainer_id的数据集,如果该数据集中存在materi_id,则将其选中的值设置为true。