简体   繁体   中英

GridView convert 'Y' or 'N' into checkbox.checked on ItemTemplate field

I have a GridView below that will show some text and provide a checkbox.

    <asp:GridView ID="surveyTableTest" runat="server" AutoGenerateColumns="false" >
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:HiddenField ID="hdnSurveyID" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "survey_id") %>' />                
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="URL">
                <ItemTemplate>
                    <asp:Label ID="surveyURL" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "URL") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Used">
                <ItemTemplate>
                    <asp:CheckBox ID="surveyUsed" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>            
    </asp:GridView>

I have a simple class which is bound to the GridView. This is populated from a SQL stored procedure.

    public class surveyURL 
    {
        public int survey_id { get; set; }
        public string URL { get; set; }
        public string used { get; set; }
    }

How can I take the information from 'used' which is NCHAR(1) 'Y' or 'N' and turn that into a checkbox.checked result where 'used' = 'Y'

This is what I am currently doing in C#, this is enough to populate the hiddenfield and first template field. I don't know how to proceed for the checkbox.

    var test = cn.Query<surveyURL>("sp_create_url", p, commandType: CommandType.StoredProcedure);

    surveyTableTest.DataSource = test;
    surveyTableTest.DataBind();

You can use the Checked property of CheckBox control and use the C# Conditional operator like this:-

<asp:CheckBox ID="surveyUsed" runat="server" 
              Checked='<%# Eval("used").ToString() == "Y" ? true: false %>' />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM