Results 1 to 2 of 2

Thread: how to access ButtonField in DetailsView?

  1. #1

    Thread Starter
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    how to access ButtonField in DetailsView?

    how can i access the button field?

    In the DetailsView DataBound event I've tried:

    Dim AddtoCartButton As Button = CType(DetailsView1.Controls(0). Button)

    but this doesn't work.

    Since there is no ID field I can't access it directly with FindControl method.

    Help?


    Code:
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" AllowPaging="false"
                    BorderWidth="0" BorderColor="white" DataSourceID="ObjectDataSourceItem">
                    <Fields>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <h1>
                                    <asp:Label ID="ProductName" runat="server" Text=' <%#Eval("Title")%>' />
                                </h1>
                                <br />
                                <asp:Image ID="Image1" CssClass="photo-border" runat="server" ImageUrl='<%# "images/" + Eval("ImageUrl") %>'
                                    AlternateText='<%#Eval("ImageAltText")%>' Height="250px" Width="275px" />
                                <br />
                                <br />
                                <br />
                                <h2>
                                    Price:
                                    <asp:Label ID="PriceLabel" runat="server" Text='<%#Eval("Price","{0:C}") %>' />
                                </h2>
                                <asp:Label ID="ProductID" runat="server" Text='<%#Eval("productid") %>' />
                                <br />
                                <asp:Label ID="DescriptionLabel" runat="server" Text='<%#Eval("Description") %>' />
                                <br />
                                <br />
                                <asp:Label ID="Color1Label" runat="server" Text="Color 1:" />&nbsp;<asp:DropDownList ID="colors1" runat="server" >
                                </asp:DropDownList><br />
                                <asp:Label ID="Color2Label" runat="server" Text="Color 2:" />&nbsp;<asp:DropDownList ID="colors2" runat="server" >
                                </asp:DropDownList>
                                <br /><br />                            
                                In Stock:
                                <asp:Label ID="InStockLabel" runat="server" Text='<%# IIf(Eval("InStock")= true, "Yes", "No") %>' />
                                <hr />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:ButtonField ButtonType="button" Text="Add to cart" CommandName="AddToCart" />
                    </Fields>
                </asp:DetailsView>

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: how to access ButtonField in DetailsView?

    The ButtonField most likely contains a button itself, so you may need to CType to ButtonField, then get the control(0) out of it instead.

    Try this, step through your code and when you get to

    Dim AddtoCartButton As Button = CType(DetailsView1.Controls(0). Button)

    Do a quickwatch on DetailsView1.Controls(0).

    Most likely it's something from your ItemTemplate. Increase the number till you get something related to your buttonfield.

Posting Permissions

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



Click Here to Expand Forum to Full Width