mardi 22 janvier 2019

Asp:Repeater Datasouce - Datatable Checkbox inside Repeater. Delete the current row

Couldn't find anything like it. The page has a Repeater, the data source is a datatable. Rows from the page are added to the datatable, then Repeater is updated and displayed on the page. For example, a user has added incorrect information and needs to remove something from Repeater by placing a check mark in the Checkbox. If the Checkbox is checked, the selected data (a row in the datatable ) should be removed from the data source and Repeater should be updated. How can this be implemented and whether it is possible at all? My code:

<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">

            <ContentTemplate>

                    <asp:Repeater ID="Repeater1" runat="server">

                        <ItemTemplate>

                                <asp:TableCell Width="80px" BackColor="#e3a99a"  HorizontalAlign="Right">
                                      <asp:CheckBox ID="cb_GetOut" runat="server" AutoPostBack="true" Checked="false" Text="DeleteRow" style="padding-right:5px" />
                                </asp:TableCell>

                        </ItemTemplate>

                    </asp:Repeater>

            </ContentTemplate>

        </asp:UpdatePanel>


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

        If Page.IsPostBack = False Then

            dtTest_add.Columns.AddRange(New DataColumn(3) {New DataColumn("text1"), New DataColumn("text2"), New DataColumn("text3"), New DataColumn("text4")})

            ViewState("dtTest_add") = dtTest_add

        Else

            dtTest_add = ViewState("dtTest_add")
            Repeater1.DataSource = TryCast(ViewState("dtTest_add"), DataTable)
            Repeater1.DataBind()

        End If

    End Sub


    Protected Sub btn_addTest_Click(sender As Object, e As System.EventArgs) Handles btn_addTest.Click

        dtTest_add.Rows.Add(txt1.Text, txt2.Text, txt3.Text, txt4.Text)

        dtTest_add = ViewState("dtTest_add")

        ViewState("dtTest_add") = dtTest_add
        Repeater1.DataSource = TryCast(ViewState("dtTest_add"), DataTable)
        Repeater1.DataBind()

        UpdatePanel1.Update()

    End Sub


    Protected Sub Repeater1_ItemCreated(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemCreated

        Dim cb_GO As CheckBox = CType(e.Item.FindControl("cb_GetOut"), CheckBox)
        ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(cb_GO)

    End Sub


    Protected Sub Repeater1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound

        Dim cb_GO As CheckBox = DirectCast(e.Item.FindControl("cb_GetOut"), CheckBox)

        cb_GO.Attributes("onclick") = "this.checked = (" + dtTest_add + ").deleteRow(" + CStr(e.Item.ItemIndex) + ");"

    End Sub

The "btn_addTest" button is on a form not in Repeater. With it I add data to the data source. Then update the Repeater.

I handle the "onclick" Checkbox event in this line of code

cb_GO.Attributes("onclick") = "this.checked = (" + dtTest_add + ").deleteRow(" + CStr(e.Item.ItemIndex) + ");"

It is necessary to remove data from datatable, and Repeater will simply be updated then and all. But how do you do that? Can't get through to a datatable with the Repeater in ItemDataBound.




Aucun commentaire:

Enregistrer un commentaire