Professional Web Applications Themes

Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked. - ASP.NET Data Grid Control

I am trying to update the database field when the checkbox is clicked. I am trying to modified the following solution but.. got stuck on the ItemCommand Events where Dim prodchknew As CheckBox = e.Item.FindControl("chk") cannot find the control. I am trying to put the ItemCommand Code into ItemBound Event but seems to be stuck on the above line .. Anyone got ideas? Thanks a bunch, Joey Solution: webform1.aspx <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 9px; POSITION: absolute; TOP: 15px" runat="server" AutoGenerateColumns="False" DataKeyField="Productid"> <Columns> <asp:TemplateColumn HeaderText="ProductName"> <ItemTemplate> <asp:CheckBox id=chk runat="server" Text =<%# DataBinder.Eval(Container.DataItem, "ProductName") %> checked='<%# DataBinder.Eval(Container.DataItem, "Discontinued") %>'> </asp:CheckBox> </ItemTemplate> ...

  1. #1

    Default Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.

    I am trying to update the database field when the checkbox is clicked.

    I am trying to modified the following solution but.. got stuck on the
    ItemCommand Events where
    Dim prodchknew As CheckBox = e.Item.FindControl("chk")
    cannot find the control.

    I am trying to put the ItemCommand Code into ItemBound Event but seems to be
    stuck on the above line ..
    Anyone got ideas?

    Thanks a bunch,
    Joey


    Solution:
    webform1.aspx
    <asp:datagrid id="DataGrid1"
    style="Z-INDEX: 101; LEFT: 9px; POSITION: absolute; TOP: 15px"
    runat="server" AutoGenerateColumns="False"
    DataKeyField="Productid">
    <Columns>
    <asp:TemplateColumn HeaderText="ProductName">
    <ItemTemplate>
    <asp:CheckBox id=chk runat="server"
    Text =<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
    checked='<%# DataBinder.Eval(Container.DataItem, "Discontinued") %>'>
    </asp:CheckBox>

    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:ButtonColumn ButtonType="PushButton"
    CommandName="Update" Text="Update"></asp:ButtonColumn>
    </Columns>
    </asp:datagrid>



    webform1.asp.vb
    Step 1:Simple Binding with the DataGrid. The Products Table has the Field
    Discontinued (DataType:bit) Based on the value 0/1 of the Discontinued field
    uncheck/check the Checkbox.
    Dim myconnection As SqlConnection
    Dim myda As SqlDataAdapter
    Dim mycmd As SqlCommand
    Dim ds As DataSet
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    If Not Page.IsPostBack Then
    bindData()
    End If
    End Sub
    Sub bindData()
    myconnection = New
    SqlConnection("Server=localhost;uid=sa;password=;d atabase=northwind;")
    myda = New SqlDataAdapter("Select * from Products where
    productid<4", myconnection)
    ds = New DataSet()
    myda.Fill(ds, "AllTables")
    DataGrid1.DataSource = ds
    DataGrid1.DataBind()
    End Sub



    Step 2: If the user check's/uncheck's the original value of the Checkbox and
    clicks the button the new checkbox value should be stored in the Database
    table.
    Private Sub DataGrid1_ItemCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
    DataGrid1.ItemCommand
    Dim prodchknew As CheckBox = e.Item.FindControl("chk")
    Dim updval As String
    If prodchknew.Checked Then
    updval = "1"
    Else
    updval = "0"
    End If

    If e.CommandName = "Update" Then
    myconnection = New
    SqlConnection("Server=localhost;uid=sa;password=;d atabase=northwind;")
    Dim strsql As String = "Update Products set Discontinued='" &
    updval & "'" & _
    " where Productid=" & DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
    ' Response.Write(strsql)
    mycmd = New SqlCommand(strsql, myconnection)
    myconnection.Open()
    mycmd.ExecuteNonQuery()
    bindData()
    End If
    End Sub




    Sushila S. Patel, Microsoft .NET MVP


    Joey Pang Guest

  2. #2

    Default Re: Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.

    Can you post your ItemDataBound method?

    Whenever I have to put code in that method, I always forget to make sure
    that the current item isn't a header or footer:
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
    ListItemType.AlternatingItem Then

    End If


    "Joey Pang" <joeyjypangnetscape.net> wrote in message
    news:%23ERL$uWbFHA.3464tk2msftngp13.phx.gbl...
    >I am trying to update the database field when the checkbox is clicked.
    >
    > I am trying to modified the following solution but.. got stuck on the
    > ItemCommand Events where
    > Dim prodchknew As CheckBox = e.Item.FindControl("chk")
    > cannot find the control.
    >
    > I am trying to put the ItemCommand Code into ItemBound Event but seems to
    > be stuck on the above line ..
    > Anyone got ideas?
    >
    > Thanks a bunch,
    > Joey
    >
    >
    > Solution:
    > webform1.aspx
    > <asp:datagrid id="DataGrid1"
    > style="Z-INDEX: 101; LEFT: 9px; POSITION: absolute; TOP: 15px"
    > runat="server" AutoGenerateColumns="False"
    > DataKeyField="Productid">
    > <Columns>
    > <asp:TemplateColumn HeaderText="ProductName">
    > <ItemTemplate>
    > <asp:CheckBox id=chk runat="server"
    > Text =<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
    > checked='<%# DataBinder.Eval(Container.DataItem, "Discontinued") %>'>
    > </asp:CheckBox>
    >
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:ButtonColumn ButtonType="PushButton"
    > CommandName="Update" Text="Update"></asp:ButtonColumn>
    > </Columns>
    > </asp:datagrid>
    >
    >
    >
    > webform1.asp.vb
    > Step 1:Simple Binding with the DataGrid. The Products Table has the Field
    > Discontinued (DataType:bit) Based on the value 0/1 of the Discontinued
    > field uncheck/check the Checkbox.
    > Dim myconnection As SqlConnection
    > Dim myda As SqlDataAdapter
    > Dim mycmd As SqlCommand
    > Dim ds As DataSet
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > If Not Page.IsPostBack Then
    > bindData()
    > End If
    > End Sub
    > Sub bindData()
    > myconnection = New
    > SqlConnection("Server=localhost;uid=sa;password=;d atabase=northwind;")
    > myda = New SqlDataAdapter("Select * from Products where
    > productid<4", myconnection)
    > ds = New DataSet()
    > myda.Fill(ds, "AllTables")
    > DataGrid1.DataSource = ds
    > DataGrid1.DataBind()
    > End Sub
    >
    >
    >
    > Step 2: If the user check's/uncheck's the original value of the Checkbox
    > and clicks the button the new checkbox value should be stored in the
    > Database table.
    > Private Sub DataGrid1_ItemCommand(ByVal source As Object, _
    > ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
    > DataGrid1.ItemCommand
    > Dim prodchknew As CheckBox = e.Item.FindControl("chk")
    > Dim updval As String
    > If prodchknew.Checked Then
    > updval = "1"
    > Else
    > updval = "0"
    > End If
    >
    > If e.CommandName = "Update" Then
    > myconnection = New
    > SqlConnection("Server=localhost;uid=sa;password=;d atabase=northwind;")
    > Dim strsql As String = "Update Products set Discontinued='" &
    > updval & "'" & _
    > " where Productid=" & DataGrid1.DataKeys(CInt(e.Item.ItemIndex))
    > ' Response.Write(strsql)
    > mycmd = New SqlCommand(strsql, myconnection)
    > myconnection.Open()
    > mycmd.ExecuteNonQuery()
    > bindData()
    > End If
    > End Sub
    >
    >
    >
    >
    > Sushila S. Patel, Microsoft .NET MVP
    >

    Kim Quigley Guest

  3. #3

    Default Re: Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.

    This page has a good example for doing something when a checkbox in a
    datagrid is changed:

    [url]http://www.geekswithblogs.net/sgreenberger/archive/2004/11/12/14871.aspx[/url]

    This is basically what it says:

    In the aspx page, modify the checkbox tag by adding the OnCheckedChanged
    attribute, and setting it to the name of the sub that you just created
    above. You'll also need autopostback set to true.
    <ItemTemplate>
    <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"
    OnCheckedChanged="Check_Clicked">asp:CheckBox>
    >

    Now when the checkbox is clicked, the Check_Clicked event will be
    fired. Add the following code to the Check_Clicked procedure:

    Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    Dim ck1 As CheckBox = CType(sender, CheckBox)
    Dim dgItem As DataGridItem = CType(ck1.NamingContainer,
    DataGridItem)
    'now we've got what we need!
    Label1.Text = "You selected row " & dgItem.Cells(0).Text
    End Sub



    Kim Quigley Guest

  4. #4

    Default Re: Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.

    Thank you so much Kim... :D Let met try it out :D
    "Kim Quigley" <kimTquigleyhotmail.com> wrote in message
    news:OYjJQ2ebFHA.1404TK2MSFTNGP09.phx.gbl...
    > This page has a good example for doing something when a checkbox in a
    > datagrid is changed:
    >
    > [url]http://www.geekswithblogs.net/sgreenberger/archive/2004/11/12/14871.aspx[/url]
    >
    > This is basically what it says:
    >
    > In the aspx page, modify the checkbox tag by adding the OnCheckedChanged
    > attribute, and setting it to the name of the sub that you just created
    > above. You'll also need autopostback set to true.
    > <ItemTemplate>
    > <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"
    > OnCheckedChanged="Check_Clicked">asp:CheckBox>
    >>
    >
    >
    > Now when the checkbox is clicked, the Check_Clicked event will be
    > fired. Add the following code to the Check_Clicked procedure:
    >
    > Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    > Dim ck1 As CheckBox = CType(sender, CheckBox)
    > Dim dgItem As DataGridItem = CType(ck1.NamingContainer,
    > DataGridItem)
    > 'now we've got what we need!
    > Label1.Text = "You selected row " & dgItem.Cells(0).Text
    > End Sub
    >
    >
    >

    Joey Pang Guest

  5. #5

    Default Re: Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.

    It worked!! Thank you Thank you Kim :D :D :D You save my day...

    "Kim Quigley" <kimquigleyhotmail.com> wrote in message
    news:OYjJQ2ebFHA.1404TK2MSFTNGP09.phx.gbl...
    > This page has a good example for doing something when a checkbox in a
    > datagrid is changed:
    >
    > [url]http://www.geekswithblogs.net/sgreenberger/archive/2004/11/12/14871.aspx[/url]
    >
    > This is basically what it says:
    >
    > In the aspx page, modify the checkbox tag by adding the OnCheckedChanged
    > attribute, and setting it to the name of the sub that you just created
    > above. You'll also need autopostback set to true.
    > <ItemTemplate>
    > <asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"
    > OnCheckedChanged="Check_Clicked">asp:CheckBox>
    >>
    >
    >
    > Now when the checkbox is clicked, the Check_Clicked event will be
    > fired. Add the following code to the Check_Clicked procedure:
    >
    > Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    > Dim ck1 As CheckBox = CType(sender, CheckBox)
    > Dim dgItem As DataGridItem = CType(ck1.NamingContainer,
    > DataGridItem)
    > 'now we've got what we need!
    > Label1.Text = "You selected row " & dgItem.Cells(0).Text
    > End Sub
    >
    >
    >

    Joey Pang Guest

Similar Threads

  1. bind to a checkbox
    By Melarky_op in forum Coldfusion Flash Integration
    Replies: 3
    Last Post: June 20th, 11:11 PM
  2. Datagrid Edit, Insert & Update Checkbox (ASP.NET Using VB.NET)
    By Jacobus Visser via DotNetMonster.com in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: April 7th, 10:19 PM
  3. How to bind checkbox to one of the values of a datagrid
    By asp123 vbnet via .NET 247 in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: March 26th, 04:36 AM
  4. Datagrid checkbox value update issue
    By irene in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: October 20th, 03:23 PM
  5. how to bind data to checkbox in datagrid
    By LiFo in forum ASP.NET Data Grid Control
    Replies: 4
    Last Post: November 17th, 02:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139