0

In my grid view I've a checkbox column, and am binding the gridview with a dictionary. I need to get the corresponding Id from the checked boxes.

In my dictionary I've the key values like

Id         Name
--         ----
 1         Arts
 2         Science
 3         Engineering

Here, I tried to bind the value for this checkbox as

<asp:TemplateField>
    <ItemTemplate>
      <asp:CheckBox ID="chkSelItem" value="<%# Eval("Key.Id") %>" 
                                                       runat="server" />
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Department">
    <ItemTemplate>
         <%# Eval("Key.Name") %>
    </ItemTemplate>
    <HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>

and from the codebehind, I tried like

foreach (GridViewRow row in gridDepartments.Rows)
        {
            CheckBox chkSelItem = (CheckBox)row.FindControl("chkSelItem");

            if (chkSelItem.Checked)
            {
                int departmentId = int.Parse(chkSelItem.Text);
                ////
                ////                 
            }
        }

its throwing error, or not showing any value for the checkbox.

I also trid with FindControl, but no use of it, coz in the key & Value pair am using a class(which inherits another class) and a bool. thats what am trying like this, can anyone help me here, thanks in advance.....

1
  • I have a delete button above the gridview, when I select more than one checkboxes and clicking on the delete button, I've to get the corresponding Ids and delete the values from the database Commented Jun 22, 2012 at 6:23

4 Answers 4

1
You can try this one...

Bind id to lable instead of to checkbox as below.

<asp:TemplateField>     
<ItemTemplate>       
<asp:CheckBox ID="chkSelItem" runat="server" />     

<asp:Label ID="lblSelectedItem" value=<%# Eval("Key.Id")) %> visible="False"/>
</ItemTemplate> </asp:TemplateField>



In codebehind try this

foreach (GridViewRow row in gridDepartments.Rows)         
{             
      CheckBox chkSelItem = (CheckBox)row.FindControl("chkSelItem");
       Label lblSelectedItem= (Label)row.FindControl("lblSelectedItem");

      if (chkSelItem.Checked) 
      {
                 int departmentId = int.Parse(lblSelectedItem.Text); 
      }
} 



Hope this is what u want...
Sign up to request clarification or add additional context in comments.

1 Comment

ya its a nice idea, binding the Text value works like a charm, I can get the Id also, I was thinking how to hide the Text value, u've given such a nice idea, I'll try it and let u know
1

Use

CheckBox chkSelItem = (CheckBox)row.cell[0].FindControl("chkSelItem");// Replace row.cell[0] accrding to you celll index

instead of

 CheckBox chkSelItem = (CheckBox)row.FindControl("chkSelItem");

Comments

1

In Aspx page:

<asp:CheckBox ID="chkSelItem" Text="<%# Eval("Key.Id") %>" runat="server" />

In code-behind add a check to ensure the item is not null:

    CheckBox chkSelItem = (CheckBox)row.FindControl("chkSelItem");

    if (chkSelItem != null && chkSelItem.Checked && !string.IsNullOrEmpty(chkSelItem.Text))
    {

        int departmentId = int.Parse(chkSelItem.Text);                
    }

Comments

1

I think you have to use Text instead of value as you are assigning to value and getting text property. Text will be empty string and parsing it with int throws exception.

int.Parse(chkSelItem.Text);

<asp:CheckBox ID="chkSelItem" value="<%# Eval("Key.Id") %>" runat="server" />

Would be

<asp:CheckBox ID="chkSelItem" Text="<%# Eval("Key.Id") %>" runat="server" />

Or

If you have to use Value of check box, then access value not Text

<asp:CheckBox ID="chkSelItem" value="<%# Eval("Key.Id") %>" runat="server" />

int departmentId = int.Parse(chkSelItem.Attributes["value"].ToString());

4 Comments

its working Adil, but I need to hide the Text value, wait am working on that as per SimplyVaibh's solution, I'll let u know once its done
You are welcome you can get value by chkSelItem .Attributes["value"].ToString();
Update my answer, this can get the value of checkbox.
thanks Adil, for ur kind response, am in great confusion to which answer I've to mark as accepted

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.