我试图通过FindControl函数在“ lblprodNameCart”中获取值,但仍无法获取该值。谁能帮我找到错误?

时间:2019-11-21 07:10:34

标签: asp.net vb.net repeater

aspx.file

具有“ lblprodNameCart”,“ lblprodOriginCart”,“ lblprodPriceCart”和“ btnDelete”的转发器

<asp:Repeater ID="RepeaterPersonalCart" runat="server" DataSourceID="SqlDataSourcePersonalProduct" 
 OnItemDataBound="RepeaterPersonalCart_ItemDataBound">
            <ItemTemplate>
                <div>
                    <asp:Table runat="server" BorderColor="White" BorderStyle="Ridge" BorderWidth="1px" 
                         BackColor="LightGray" CssClass="tableCart">
                        <asp:TableRow>
                            <asp:TableCell Width="350px">
                               <asp:Label runat="server" ID="lblprodNameCart"> 
                                  <%#Eval("product_name")%>
                               </asp:Label>
                            </asp:TableCell>
                            <asp:TableCell></asp:TableCell>
                            <asp:TableCell Width="350px">
                               <asp:Label runat="server" ID="lblprodOriginCart"> 
                                  <%#Eval("product_origin")%>
                               </asp:Label>
                            </asp:TableCell>
                            <asp:TableCell></asp:TableCell>
                            <asp:TableCell Width="350px">
                               <asp:Label runat="server" ID="lblprodPriceCart"> 
                                  <%#Eval("product_price")%>
                               </asp:Label>
                            </asp:TableCell>
                            <asp:TableCell></asp:TableCell>
                            <asp:TableCell>
                               <asp:Button ID="btnDelete" runat="server" Text="Delete" 
                                CssClass="btnDelete" BorderStyle="None" OnClick="removeRecords_click" /> 
                            </asp:TableCell>
                        </asp:TableRow>
                    </asp:Table>
                </div>
            </ItemTemplate>
        </asp:Repeater>

aspx.vb

中继器

Protected Sub RepeaterPersonalCart_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)

    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then

        'Reference the Repeater Item
        Dim item As RepeaterItem = e.Item

        'Reference the Controls
        Dim productName As String = TryCast(item.FindControl("lblprodNameCart"), Label).Text
        Session("pdName") = productName.ToString.Trim

    End If

End Sub

删除按钮

Protected Sub removeRecords_click(sender As Object, e As EventArgs)
    'Declaration
    Dim query As String

    query = "UPDATE product_list SET username = @username WHERE product_name = '" & Session("pdName") & "'"

    cmd.CommandText = query
    cmd.CommandType = CommandType.Text
    cmd.Connection = con

    cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = "NULL"

    Try
        cmd.ExecuteNonQuery()
        MsgBox("Successfully removed!", MsgBoxStyle.Exclamation)
    Catch ex As Exception
        MsgBox("You failed to do so! HAHHAHAHAHA", MsgBoxStyle.Information)
    End Try

    con.Close()
    con.Dispose()

End Sub

结果

1。 Session(“ pdName”)=“”-表明Session(“ pdName”)为空

2。结果显示错误“您未能这样做!HAHAHAHAHA”。它无法从中继器获取数据

0 个答案:

没有答案