1

I have a gridview populated by checkbox using the code below

Data Source Result:

Branch,101,102,103,104,105,106,107,108,109,110  
00001,null,null,null,null,null,null,null,null,null,null  
00016,1,1,1,1,1,0,0,0,0,0
00244,1,1,1,1,1,1,1,1,1,1



 <asp:TemplateField HeaderText="101">
                <ItemTemplate>
                    <asp:CheckBox runat="server" id="cb101" Checked='<%# Eval("101").ToString().Equals("1") %>' />
                </ItemTemplate>
           </asp:TemplateField>... and so on

enter image description here It is properly working for checkbox if if the column is 0 and 1. Now what I need to do is if the column is null the checkbox should be disabled/readonly

5
  • Ypu can put that condition in Itemdatabound... Commented Oct 28, 2013 at 3:20
  • @Rony can you pls give me an example for this Commented Oct 28, 2013 at 3:22
  • Do u know how to get values of checkbox from Repeater? Commented Oct 28, 2013 at 3:26
  • what I need is onPageLoad event.. and select query .. now base on the selected query the checkbox will be either disable(null),checked(1),unchecked(0) Commented Oct 28, 2013 at 3:34
  • I now buddy... U can do it by ItemDataBound... Just Google it about that... Commented Oct 28, 2013 at 3:40

3 Answers 3

2

It should be something like this:

 <asp:CheckBox runat="server" id="CheckBox1" Checked='<%# Eval("101").ToString()=="1" ? true : false %>' Enabled='<%#(String.IsNullOrEmpty(Eval("101").ToString()) ? false: true) %>'/>

And it worked for me.

Sign up to request clarification or add additional context in comments.

1 Comment

OH MEN THIS IS GREAT!
1

Another option is to use the GridView's RowDataBound event, which fires for each row bound to the grid view, like this:

Markup:

<asp:GridView runat="server" id="GridView1" OnRowDataBound="GridView1_RowDataBound" />

Code-behind:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    // Only work with data rows, ignore header and footer rows
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        if(DataBinder.Eval(e.Row.DataItem, "Difference") == null)
        {
            CheckBox the101Checkbox = e.Row.FindControl("cb101") as CheckBox;

            // Verify the check box was found before we try to use it
            if(the101Checkbox != null) 
            {
                the101Checkbox.Enabled = false;
            }
        }
        else
        {
            if(DataBinder.Eval(e.Row.DataItem, "Difference") == "1")
            {
                CheckBox the101Checkbox = e.Row.FindControl("cb101") as CheckBox;

                // Verify the check box was found before we try to use it
                if(the101Checkbox != null) 
                {
                    the101Checkbox.Checked = true;
                }
            }
        }
    }
}

Note: Using the RowDataBound event offers the advantage of leveraging Visual Studio's Intellisense and generally catching problems as compile-time syntax errors, whereas embedded code blocks result in catching problems as run-time errors.

1 Comment

@zxc - the difference is that you are using the compiler as a tool versus writing text that is evaluated as code in the markup. The main advantage of this is that it is easier to debug as you can put breakpoints on your code in and step-through the code in code-behind whereas you cannot step through the logic in embedded code blocks. If I were coming along to maintain your code, I would much rather see code-behind where I can leverage the power of Visual Studio (compiler and debugger), rather than trying to figure out embedded code blocks. Just my two cents. :-)
0

Try this

<asp:CheckBox runat="server" id="cb101" Checked='<%# Convert.ToString(Eval("101")) == string.Empty ? 'True' : 'False' %>' Enabled='<%# Eval("101")==null ? 'false': 'true' %>' />

Here 101 mean your column name.

Regards Amit Vyas

1 Comment

yes 101 is my column name.. It says too many characters in string literal.. then i tried removing quotes still an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.