VB-WEB: Difficulties with ItemTemplate CheckBox ID

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JimSnotes
    New Member
    • Jan 2008
    • 3

    VB-WEB: Difficulties with ItemTemplate CheckBox ID

    I am looking to do something like this to assign a unique ID to each checkbox in a gridview:

    Code:
    <asp:TemplateField Visible="False" HeaderStyle-CssClass="SpreadSheetHeader" ItemStyle-CssClass="SpreadSheetCell" ShowHeader="True" HeaderText="Add to Cart / Qty" >
                            <ItemTemplate>
                                Add?<asp:CheckBox ID='<%# Eval("DFNumber") %>' CssClass="chkOrder" runat="Server" />
                                Qty:<asp:TextBox CssClass="txtBoxQuantity" ID="txtQuantity" runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
    But I get the error message 'server tag is not well-former'. Is it possible to do it like this? If not, what would be a good way to assign a unique ID to a checkbox control in an item template?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I remember attempting to do the same thing that you are trying to do. I discovered that it isn't possible..and at one point had a good reason as to why this is but can't remember it off the top of my head.

    Because the controls are dynamically given an ID anyways, why don't you just give the CheckBox an ID like "ChkboxAdd" , and include a hidden column called something like "DFNumber" that will keep track of what DFNumber you are "adding".

    EG
    [code=asp]
    <asp:TemplateFi eld Visible="False" HeaderStyle-CssClass="Sprea dSheetHeader" ItemStyle-CssClass="Sprea dSheetCell" ShowHeader="Tru e" HeaderText="Add to Cart / Qty" >
    <ItemTemplate >
    Add?<asp:CheckB ox ID='ChkboxAdd' CssClass="chkOr der" runat="Server" />
    Qty:<asp:TextBo x CssClass="txtBo xQuantity" ID="txtQuantity " runat="server"> </asp:TextBox>
    </ItemTemplate>
    </asp:TemplateFie ld>
    <asp:BoundFie ld Header="DFNumbe r" DataField="DFNu mber ">
    <ItemStyle CssClass="noSho w" width="0px">
    <HeaderStyle CssClass="noSho w" width="0px">
    </asp:BoundField>
    [/code]

    Then in your code that does "stuff" loop through the rows in the gridview, grab the checkbox check value and the DFNumber for that row....and do your processing on that:

    Code:
    For Each dr In MyGridView.Rows
           Dim chkBox as CheckBox=Ctype(dr.FindControl("ChkboxAdd"),Checkbox)
           If chkbox.checked = true then 
    'Here retrieve the DFNumber for the row that has been "checked".
           End If

    Comment

    Working...