how to check/Uncheck a checkbox dynamically in a datagrid in asp.net

Discussion in 'ASP .Net' started by Neal, Apr 27, 2005.

  1. Neal

    Neal Guest

    HI all,

    I have an issue with check box in datagrid.
    I would like it to check and uncheck based on a database value of NULL
    or a Date Value.

    If there is a date value for that column then check the check box in
    datagrid

    If the value is null then keep it unchanged as (Unchecked)

    this is in asp.net

    Your help is appreciated

    Thank you

    Neal
     
    Neal, Apr 27, 2005
    #1
    1. Advertisements

  2. hi,

    Datagrid code with check box and date field

    <asp:DataGrid id="grdAuthors" runat="server" Width="626px">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:CheckBox ID="chkbox" Runat="server"></asp:CheckBox>
    </ItemTemplate>
    </Columns>
    </asp:DataGrid>

    and with dataItemBound event of grid u can write code as below


    string _lblDate = e.Item.Cells[1].Text.ToString(); //date column
    CheckBox _chkBox = (CheckBox)e.Item.FindControl("chkbox");
    if(_chkBox != null)
    {
    if(_lblDate == "True")//Here u can check if some date text is
    //bound with the label
    {
    _chkBox.Checked = true;
    }
    }
     
    surindersaini, Apr 27, 2005
    #2
    1. Advertisements

  3. Neal

    subashini

    Joined:
    Mar 1, 2012
    Messages:
    1
    Likes Received:
    0

    Hi,

    Try this... it vl help u.
    i used gridview.
    got the id of the student.
    as the userid is in a datalist. so i too from ther.
    check the active status from a table.
    i used checkboxes. if the user is in an active status then boxes should be checked.
    protected void dgGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow ||
    e.Row.RowType == DataControlRowType.Separator)
    {
    Label lblstudentname = (Label)e.Row.FindControl("lblstudentname");
    string studentname = lblstudentname.Text.Trim();
    Label lbluserid = (Label)e.Row.FindControl("lbluserid");
    string userid = lbluserid.Text.Trim();
    DataSet ds1 = comProfile.getAssessmentChar();
    DataList dldata = (DataList)e.Row.FindControl("dldata");
    for (int i = 0; i < dldata.Items.Count; i++)
    {
    Label lblid1 = (Label)dldata.Items.FindControl("lblid1");
    string lsid = lblid1.Text.Trim();
    int lsid1 = Convert.ToInt32(lsid);
    CheckBox chkactive = (CheckBox)dldata.Items.FindControl("chk1");
    DataSet ds = comProfile.getDetails(userid, iyear, lsid);

    if(ds.Tables[0].Rows.Count > 0)
    chkactive.Checked = true;
    else
    chkactive.Checked = false;
    }
    }
    }

    Regards
    suba


     
    subashini, Mar 5, 2012
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.