0

I'm looking for a small piece of code that allows me the have a checkbox checked or not checked depending on the text value of the Checkbox.

For example: If the text value of the checkbox is "Yes" it needs to be checked. If it's not "Yes" it shouldn't be checked.

This is what I have:

ASPX-page:

<asp:TemplateField HeaderText="Contract Check" SortExpression="Contract_Check" ItemStyle-Wrap="false">
            <EditItemTemplate></EditItemTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="chk_contract_check" Checked='<%# contractCheck(Container.DataItem) %>' Text='<%# Bind("Contract_Check") %>' runat="server" Enabled="false" />
            </ItemTemplate>
            <FooterTemplate></FooterTemplate>
        </asp:TemplateField>

Code Behind File:

protected String contractCheck(CheckBox obj)
    {
        if (obj.Text == "Prior Transition")
            return "True";
        else
            return "False";}

However this is not working. Do you guys have any idea how I can get this too work? Thanks

Kevin

4

3 回答 3

2

There are two options in my mind:

1 - Inline logic: Updating the aspx (no function required)

You should be able to just put the logic inline, directly into the markup:

<asp:TemplateField HeaderText="Contract Check" SortExpression="Contract_Check" ItemStyle-Wrap="false">
        <EditItemTemplate></EditItemTemplate>
        <ItemTemplate>
            <asp:CheckBox ID="chk_contract_check" Checked='<%# Eval("Text").ToString() == "Prior Transition" %>' Text='<%# Bind("Contract_Check") %>' runat="server" Enabled="false" />
        </ItemTemplate>
        <FooterTemplate></FooterTemplate>
    </asp:TemplateField>

(the amended part is Checked='<%# Eval("Text").ToString() == "Prior Transition" %>')

OR

Option 2 - Separate Logic: Update the function

If you'd prefer to keep the logic separate, then there are a couple of changes to make. You are currently passing in the data item, not the Checkbox (which is what your function is expecting). The second issue is that you need to return a bool from your logic function, not a string. You need to make two changes in this instance:

1 - Pass in the text to be checked to the function in the aspx

<asp:CheckBox ID="chk_contract_check" Checked='<%# contractCheck(Eval("Text").ToString()) %>' Text='<%# Bind("Contract_Check") %>' runat="server" Enabled="false" />

2 - Amend the function to check the text, and return a bool

protected bool contractCheck(string text)
{
    return text == "Prior Transition";
}

Basically, the Checked attribute requires a bool to work, and you're giving it a String.

于 2012-10-01T16:41:18.437 回答
0

Why don't you create a custom CheckBox control (inherit from CheckBox), then put the code-behind logic in there to check the box if necessary when the text is set?

Here's a link that may help: http://www.codeproject.com/Articles/28783/Your-First-ASP-NET-Custom-Control

于 2012-10-01T15:13:38.767 回答
0

Your contractCheck method needs the object that can be evaluated

Like this

protected String contractCheck(Object obj)
{
        if (DataBinder.Eval(obj, "Name of the field").ToString() == "Prior Transition")
            return "True";
        else
            return "False";}
于 2012-10-01T15:18:17.527 回答