在Datalist中获取所选项目值

时间:2014-03-14 17:57:22

标签: c# asp.net

我在Datalist中使用ImageButton,并希望将所选项目值存储在varible中以进一步处理它。如何在按钮点击时获取数据。我需要的数据是Productid,名称,价格等。有帮助吗? Datalist的代码是

<asp:DataList ID="DataList1" runat="server" Width="100%" 
    onselectedindexchanged="DataList1_SelectedIndexChanged" 
    oncancelcommand="DataList1_CancelCommand" oneditcommand="DataList1_EditCommand">
    <ItemTemplate>
        <table class="style3" style="border: medium solid #000000; width: 100%;">
            <tr>
                <td rowspan="3" width="50%" valign="top">
                   <center style="border-width: thin; border-color: #000000; height: 149px; border-right-style: solid;"> 
                       <asp:HyperLink ID="HyperLink9" runat="server" Height="100px" 
                           NavigateUrl='<%# "~/ProductDetails.aspx?ProductId=" + Eval("Product_id") %>' 
                           Width="100px" style="margin-left: 0px; margin-bottom: 0px"> 
                           <asp:Image ID="img" runat="server" ImageUrl='<%# "images/" + Eval("Category")+"/"+Eval("Sub_category")+"/"+Eval("image") %>'   ToolTip='<%# Eval("Product_name") %>' /></asp:HyperLink>
                       <br />
                       <br />
                       <asp:Label ID="Label1" runat="server" 
                           Text='<%# "Price : " + Eval("Price") + " Rs." %>'></asp:Label>
                   </center>
                 </td>
                <td class="style6" style="width: 50%">
                   <center style="border-bottom-style: solid; border-width: thin; border-color: #000000"> 
                       <asp:HyperLink ID="HyperLink1" runat="server" 
                           Text='<%# Eval("Product_name") %>' ToolTip='<%# Eval("Product_name") %>'></asp:HyperLink>
                   </center>
                </td>
            </tr>
            <tr>
                <td class="style7" valign="top">
                    <center style="border-width: thin; border-color: #000000; height: auto; border-bottom-style: solid;">
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Product_info") %>'></asp:Label></center>
                </td>
            </tr>
            <tr>
                <td class="style5">
                    <center>
                        <asp:ImageButton ID="ImageButton3" runat="server" Height="49px" 
                            ImageUrl="~/images/add to cart.jpg" onclick="ImageButton3_Click" 
                            Width="135px" />
                    </center>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>

我希望在图像按钮上单击所选的行值应存储在购物车表中,为此我需要将此值存储到我的后端代码中,并且需要至少的Productid值。

1 个答案:

答案 0 :(得分:1)

您可以根据需要创建string,并将它们放在CommandArgument的{​​{1}}属性中,并在ImageButton处理程序中,从{{读取该字符串1}}传递给处理程序,为了方便起见,您可以在Server Side字符串和服务器sender中将JSON数据创建为您想要的字符串deserialize, 您可以像这样设置stringified

Type

并且在CommandArgument的服务器端处理程序中需要这个:

<asp:ImageButton 
    ID="ImageButton3" 
    runat="server" 
    Height="49px" 
    CommandArgument='<%# "{\"Product_id\": \""+ Eval("Product_id") +"\", \"Price\": \"" + Eval("Price") + "\"}" %>'
    Width="135px" 
    OnClick="ImageButton3_Click" />

**或**

您可以使用DataList的ImageButton事件,如下所示:

1-设置处理程序:

protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
    System.Web.Script.Serialization.JavaScriptSerializer o = new System.Web.Script.Serialization.JavaScriptSerializer();
    Argument arg = o.Deserialize<Argument>(((ImageButton)sender).CommandArgument);
}

class Argument
{
    // these property names, must be the same with what you set in 
    // CommandArgument where you write, for example : ... ='<%# "{\"Product_id\": \""+  ...

    // i assume the type of Product_id is guid
    public Guid Product_id { get; set; }

    // i assume the type of Price is decimal
    public decimal Price { get; set; }
}

2-设置用作关键字的OnItemCommand <asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_OnItemCommand"> 属性,如:

CommandName

3-创建一些ImageButton<asp:ImageButton ID="ImageButton3" runat="server" CommandName="ImageButtonClick" /> 的{​​{1}}标记中包含您在服务器上的内容。赞:

HiddenFields

4-实现ItemTemplate的服务器端DataList处理程序,并创建<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_OnItemCommand"> <ItemTemplate> <asp:ImageButton ID="ImageButton3" runat="server" CommandName="ImageButtonClick" <asp:HiddenField ID="HiddenFieldProductID" runat="server" Value='<%# Eval("Product_id") %>' /> <asp:HiddenField ID="HiddenFieldPrice" runat="server" Value='<%# Eval("Price") %>' /> </ItemTemplate> </asp:DataList> 并读取其值,例如:

OnItemCommand