locked
Checkboxlist random value wanted to be checked. RRS feed

  • Question

  • User-730391234 posted

    Hi friends,

    i have one problem with the checkboxlist control. i am using checkboxlist of months[i.e, jan-dec] in a gridview row. now i wanted to check the jan and feb field wich i am getting from the database since this month payment has done.

    this is my code..

    <asp:GridView ID="gv_data" runat="server" AutoGenerateColumns="False" 
    CellPadding="4" onrowdatabound="gv_data_RowDataBound" >
    <Columns>
                        <asp:BoundField DataField="fdesc" HeaderText="Description" />
                        <asp:TemplateField HeaderText="Month">
    <ItemTemplate>
    <asp:CheckBoxList ID="chkMonth" runat="server" RepeatDirection="Horizontal" >
    <asp:ListItem>Jan</asp:ListItem>
    <asp:ListItem>Feb</asp:ListItem>
    <asp:ListItem>Mar</asp:ListItem>
                            :
    <asp:ListItem>Dec</asp:ListItem>
    </asp:CheckBoxList>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    
    
    protected void gv_data_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    CheckBoxList chk = (CheckBoxList)e.Row.FindControl("chkMonth");
    if (ds2.Tables[0].Rows.Count > 0)
    {
    for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
    {
    if (DataBinder.Eval(e.Row.DataItem, "fdesc").ToString() == ds2.Tables[0].Rows[i]["feeType"].ToString())
    {
    for (int j = 0; j < 12; j++)
    {
    if (chk.Items[j] == ds.Tables[0].Rows[0]["pMonth"])
    {
    chk.Items[j].Selected = true;
    //gv_data2.FindControl("chkMonth");
    //chk.Enabled = false;
    // chk.Checked = true;
    // chk.Enabled = false;
    }
    }
    }
    }
    }
    }
    }
    here i am checking whether description is ther in the database, if true i am checking which month, 
    then i have to make that cheked in the checkbox list..
    
    
    so please help me on this.. Thank you
    Tuesday, October 18, 2011 12:49 AM

Answers

  • User-68639941 posted

    hi,

    simply use FindByText or FindByValue

    	ListItem li = chk.Items.FindByText(ds.Tables[0].Rows[0]["pMonth"]);
    		if (li != null)
    			li.Selected = true;
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 18, 2011 1:18 AM

All replies

  • User-68639941 posted

    hi,

    simply use FindByText or FindByValue

    	ListItem li = chk.Items.FindByText(ds.Tables[0].Rows[0]["pMonth"]);
    		if (li != null)
    			li.Selected = true;
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 18, 2011 1:18 AM
  • User-730391234 posted

    thank you so much karthik, it helped me a lot. i have tried in for loop, and it was working, but it was a very long process and time consuming.

    this was my try..

    for (int j = 0; j < 11; j++)
    {
    if (chk.Items[j].ToString() == ds2.Tables[0].Rows[i]["pMonth"].ToString())
    {
    chk.Items[j].Selected = true;
    chk.Items[j].Enabled = false;
    }
    }

     

    god bless you,

    Tuesday, October 18, 2011 1:59 AM