You must Sign In to post a response.
  • Category: ASP.Net MVC

    Check uncheck checkbox according to database

    Hello friends,

    I have used checkbox in table and I am binding whole table dynamically through database. in that i am able to save checked value of checkbox. when i am going to bind that checked values to table from database, then unable to bind check or uncheck values to checkbox of each row.

    Please give me solution for this issue. I m stuck in this issue.

    Thanks in Advance,
    Sonal patel
  • #763598
    Hi Sonal,

    As per my understand the post you want to check or uncheck checkbox based on database value, if that is your requirement then refer below sample code.

    Design your TemplateField like below

    <TemplateField>
    <ItemTemplate>
    <asp:Label ID="lblChkStatus" runat="server" Text='<%=DataBinder.Eval(Container.DataItem,"CheckStatus")%>' />
    <asp:CheckBox ID="chkSelection" runat="server"/>


    DataSet ds=//get data from database;
    gv.DataSource=ds;
    gv.DataBind();


    and in gridview rowdatabound event check the value of each record whether that row is checked or not.


    protected void gv_OnRowDataBound(object sender,GridviewRowEventArgs e)
    {
    if(e.Row.RowType==DataControlRowType.DataRow)
    {
    if((e.Row.FindControl("lblChkStatus") as Label))
    {
    (e.Row.FindControl("chkSelection") as CheckBox).Checked=true;
    }
    else
    {
    (e.Row.FindControl("chkSelection") as CheckBox).Checked=false;
    }
    }
    }


    Try something like above to achieve your goal.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763604
    you can use jQuery to find checkboxes and check or uncheck as per db value

    http://dotnetmentors.com/aspnet/jquery-check-uncheck-all-checkboxes-in-gridview.aspx

    Laxmikant

  • #763608
    do you want it in MVC ? if yes then you can check below link
    http://stackoverflow.com/questions/23925035/mvc-4-checkbox-list-dynamically-bind-with-database-table

    else in ASP.NET you can put checkbox in gridview and on 'dataRowDataBound' event you can use 'FindControl' for checkbox and check it

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763677
    Hai Sonal,
    It seems that when the check-box is getting checked from the database, due to its post-back it gets unchecked again.
    So you need to use some client side script to make the check-box checked so that post-back should not occur and your check-boxes should be checked as per the database values.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments