如何在按钮点击时获得datalist项目值? [英] How do i get datalist item values on button click?

查看:107
本文介绍了如何在按钮点击时获得datalist项目值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的标签中获取值以打印到另一个标签,我收到错误对象引用未设置为对象的实例



这是我的datalist:

I am trying to get the value from my labels in a datalist to print out to another label, and i am getting error "Object reference not set to the instance of an object"

Here's my datalist:

<table>
<tr>
<td>
    <asp:DataList ID="ProdList" runat="server" DataKeyField="PRDProdID"

        DataSourceID="SqlDataSource1" RepeatColumns="3" BackColor="LightYellow"

        BorderColor="Black" BorderStyle="Dotted" BorderWidth="2px" CellPadding="10"

        CellSpacing="10" Font-Bold="True" Font-Names="Calibri" Font-Size="Small"

        RepeatDirection="Horizontal" Width="1000px">
        <ItemTemplate>
        <table>
        <tr>
        <td valign="top">
            <asp:Image ID="Image1" runat="server" Height="100" Width="100" />
        </td>
        <td valign="top">
        <table>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDProdIDLabel" runat="server" Text='<%# Eval("PRDProdID") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDCATIDLabel" runat="server" Text='<%# Eval("PRDCATID") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        SKU#:&nbsp;<asp:Label ID="PRDProdCodeLabel" runat="server" Text='<%# Eval("PRDProdCode") %>' />
        </td>
        </tr>
        <tr>
        <td>
        Product:&nbsp;<asp:Label ID="PRDNameLabel" runat="server" Text='<%# Eval("PRDName") %>' />
        </td>
        </tr>
        <tr>
        <td>
        Description:&nbsp;<asp:Label ID="PRDDescriptionLabel" runat="server" Text='<%# Eval("PRDDescription") %>' />
        </td>
        </tr>
        <tr>
        <td>
        Customer Rating:&nbsp;<asp:Label ID="PRDCustomerRatingLabel" runat="server" Text='<%# Eval("PRDCustomerRating") %>' />
        </td>
        </tr>
        <tr>
        <td>
        List Price:&nbsp;<asp:Label ID="PRDListPriceLabel" runat="server" Text='<%# Eval("PRDListPrice", "{0:C}") %>' />
        </td>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDPictureLabel" runat="server" Text='<%# Eval("PRDPicture") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        In Stock:&nbsp;<asp:Label ID="PRDInStockLabel" runat="server" Text='<%# Eval("PRDInStock") %>' />
        </td>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDThumbLabel" runat="server" Text='<%# Eval("PRDThumb") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        <asp:Label ID="PRDPictureEnlargeLabel" runat="server" Text='<%# Eval("PRDPictureEnlarge") %>' Visible="False" />
        </td>
        </tr>
        <tr>
        <td>
        Price:&nbsp;<asp:Label ID="PRDPriceLabel" runat="server" Text='<%# Eval("PRDPrice", "{0:C}") %>' />
        </td>
        </tr>
        </table>
        </tr>
        </td>
        <tr>
        <td>
            <asp:Button ID="addtocartbutton" runat="server" Text="Add to Cart"

                Font-Bold="True" Font-Names="calibri" Font-Size="Smaller"

                CommandName="addtocart" />
        </td>
        </tr>
        </table>
        </ItemTemplate>
    </asp:DataList>
</td>
</tr>
</table>
<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="White"></asp:Label>







和我的代码隐藏:






and my codebehind:

Protected Sub ProdList_ItemCommand(source As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles ProdList.ItemCommand
        If e.CommandName = "addtocart" Then

            Dim prodid = DirectCast(e.Item.FindControl("PRDProdID"), Label)
            Dim prodname = DirectCast(e.Item.FindControl("PRDName"), Label)

            Label1.Text = prodname.Text
        Else

        End If
    End Sub





can I not use button command name in itemcommand?



can I not use button command name in itemcommand?

推荐答案

Check this solution:

http://forums.asp.net/t/1204372.aspx/1[^]



Essentially you want to be calling FindControl on your datalist and not on your event argument.
Check this solution:
http://forums.asp.net/t/1204372.aspx/1[^]

Essentially you want to be calling FindControl on your datalist and not on your event argument.


这篇关于如何在按钮点击时获得datalist项目值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆