This is a migrated thread and some comments may be shown as answers.

checkbox in grid

5 Answers 176 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Liu Yan
Top achievements
Rank 1
Liu Yan asked on 30 Jul 2008, 08:22 AM
Hi,

   In RadGrid, 5th col is checkbox(datatype:bit),I want to get its value when click the commandbotton which is not in grid . How to do....?

Thanks

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 30 Jul 2008, 08:53 AM
Hello Liu Yan,

You can check out the following code to access the value of a CheckBox on clicking a button outside the grid.
cs:
 protected void Button1_Click(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid2.MasterTableView.Items) 
        { 
            foreach (GridColumn col in RadGrid2.MasterTableView.RenderColumns) 
            { 
                if (col.UniqueName == "ToandFro") 
                { 
                    CheckBox chkbx = (CheckBox)item["ToandFro"].Controls[0]; 
                    string strtxt = chkbx.Checked.ToString(); 
                }   
            } 
        } 
    } 

Thanks
Princy.
0
Liu Yan
Top achievements
Rank 1
answered on 30 Jul 2008, 09:15 AM
Hi,Princy
Thanks for you help

I want the selected row of checkbox value.
In other words, when selecting a row, and then returned to where the line of checkbox value.
Thanks
0
Shinu
Top achievements
Rank 2
answered on 30 Jul 2008, 09:41 AM
Hello Liu Yan,

You can try out the code snippet given below and see if it meets your requirement.
aspx:
<telerik:GridClientSelectColumn UniqueName="SelectCol"></telerik:GridClientSelectColumn> 

cs:
 protected void Button1_Click(object sender, EventArgs e)  
    {  
        foreach (GridItem item in RadGrid2.SelectedItems)  
        {  
            GridDataItem dataitem = (GridDataItem)item;  
            CheckBox chkbx = (CheckBox)dataitem["SelectCol"].Controls[0];  
              
        }  
    } 

Thanks
Shinu.
0
Liu Yan
Top achievements
Rank 1
answered on 30 Jul 2008, 12:58 PM

Hi, this is my C# code
 
   protected void BtnUnlock_Click(object sender, EventArgs e)
    {  
        if (HiddenField1.Value != "")
        {
// if using Microsoft Grid,the code can get Checkbox value
//bool bIsLocked = ((CheckBox)GridView1.Rows[GridView1.SelectedIndex].Cells[9].Controls[0]).Checked;           
// Error code: get checkbox value(datatyp: bit)
            bool bIsLocked = ((CheckBox)RadGrid1.Rows[RadGrid1.SelectedIndex].Cells[5].Controls[0]).Checked;
            if (bIsLocked)  //Locked
            {
                MembershipUser u;
                string strUserName = this.RadGrid1.SelectedValue.ToString();
                u = Membership.GetUser(strUserName);
                string connstring = ConfigurationManager.ConnectionStrings["CBPCConnectionString"].ConnectionString;
                using (SqlConnection dataConnection = new SqlConnection(connstring))
                {
                    dataConnection.Open();
                    SqlCommand dataCommand = new SqlCommand("aspnet_Membership_UnlockUser");
                    dataCommand.Connection = dataConnection;
                    dataCommand.CommandType = CommandType.StoredProcedure;
                    dataCommand.Parameters.Add(new SqlParameter("@ApplicationName", SqlDbType.NVarChar));
                    dataCommand.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar));
                    dataCommand.Parameters[0].Value = "CBPC";
                    dataCommand.Parameters[1].Value = strUserName;
                    SqlDataReader reader = dataCommand.ExecuteReader();
                    reader.Close();
                    dataConnection.Close();
                }
                //this.RadGrid1.DataSource = SqlDataSource1;
                this.RadGrid1.Rebind();
                //发送邮件
                MailMessage m_message = new MailMessage();
                m_message.From = new MailAddress("cbpc_vcis@hq.cofcoko.com");
                m_message.To.Add(new MailAddress(u.Email));
                m_message.Subject = "From P-VCIS";
                string MailBody = "Hi," + strUserName + "\r\n:";
                MailBody = MailBody + "Your account has been unlocked .";
                m_message.Body = MailBody;
                SmtpClient m_smtpClient = new SmtpClient();
                m_smtpClient.Send(m_message);

            }
        }
        else
        {
            this.Label2.Text = "No Selecting!";
        }
    }

0
Princy
Top achievements
Rank 2
answered on 31 Jul 2008, 11:11 AM
Hi Liu Yan,

Here I have tried with a CheckBox in the ItemTemplate of a GridTemplateColumn. On clicking the Button I am trying to access the CheckBox from the selected rows of the Grid.

CS:
  protected void Button1_Click(object sender, EventArgs e)  
    {  
        foreach (GridItem item in RadGrid1.SelectedItems)  
        {   
            GridDataItem dataitem=(GridDataItem)item;  
            CheckBox chkbx = (CheckBox)dataitem["CheckCol1"].FindControl("CheckBox1");  
            bool BoolVal = chkbx.Checked;  
        }  
    } 


ASPX:
<telerik:GridTemplateColumn UniqueName="CheckCol1"  HeaderText="CheckCol1" > 
                       <ItemTemplate> 
                           <asp:CheckBox ID="CheckBox1" runat="server"  Checked='<%#Eval("CheckCol1") %>'/>  
                       </ItemTemplate> 
                    </telerik:GridTemplateColumn> 


Thanks
Princy.
Tags
Grid
Asked by
Liu Yan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Liu Yan
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or