开发者

Checkbox Within EditItemTemplate Of GridView Column

I have a template field within a GridView to display a checkbox in edit view and some custom text in normal view.

The results of 开发者_运维知识库the query to populate the grid are coming back in a DataSet.

In the edit view I can't get the checkbox to accept the column that is retuning from the DataSet (which is in T/F format) as it says its not the correct type. Even if I return "true" or 1 in the column it still dosn't work.

I take it I need to convert but can't get the syntax correct in this context

<EditItemTemplate>
              <asp:CheckBox ID="cbPostToFarm" runat="server" Checked='<% Bind("BOOL_COL")' />
            </EditItemTemplate>


Use the GridView DataBound event to set the value. Inside of the DataBound event:

Ensure the row you are working with is not a Header/Footer row.
Create a reference to the CheckBox.
Create a reference to the data that is being bound.
Mark the CheckBox checked when appropriate.


This seems working for me (.net Visual Studio 2010)

<asp:TemplateField HeaderText="Ordered" SortExpression="Ordered">
                        <ItemTemplate>
                            <asp:CheckBox ID="ID_CKBOX" runat="server" 
                            Checked='<%#Eval("ordered") %>' />
                        </ItemTemplate>
                        <ItemStyle Width="8px" />
                    </asp:TemplateField>

Where 'ordered' is the one of the db field of select statement.


You were so close! Just missing the # and the ending %>

<EditItemTemplate>
<asp:CheckBox ID="cbPostToFarm" runat="server" Checked='<%# Bind("BOOL_COL") %>'/>
</EditItemTemplate>

Note that the use of single quotes after Checked and not Double Quotes is important. You will get errors with double quotes.


I got round the issue by not binding directly to the dataset but creating a strongly typed list.

I'm sure there must be a syntactical trick to get the checkbox to bind directly to a DataSet but for the sake of a simple type I gave up looking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜