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

    Grid view check box should check one by one

    I have a grid view of items which is sorted by purchased date descending where i can select items by checking check box one by one from top to bottom.

    My objective is to force the user to check the first line and then second one (not random)

    Help me in this regard.

    Hope i explained if any details will be provided.
  • #767961
    Hi Syed,

    Please check the below link for the Grid view Checkbox selection.
    This below code will be used for choosing the data in the list of grid.


    protected void GetSelectedRecords(object sender, EventArgs e)

    {

    DataTable dt = new DataTable();

    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Country") });

    foreach (GridViewRow row in GridView1.Rows)

    {

    if (row.RowType == DataControlRowType.DataRow)

    {

    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);

    if (chkRow.Checked)

    {

    string name = row.Cells[1].Text;

    string country = (row.Cells[2].FindControl("lblCountry") as Label).Text;

    dt.Rows.Add(name, country);

    }

    }

    }

    gvSelected.DataSource = dt;

    gvSelected.DataBind();

    }



    http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-in-ASPNet.aspx

    Thanks,
    Mani

  • #767962
    Thanks mani but this is not what i expect. I need to click one by one second should be checked only if first is checked.

  • #767964
    Hi Syed,

    Now I understand your requirement.
    Initially make the first Checkbox enable and implement the below code.
    Kindly implement this and let us know if you are able to satisfy your requirement.


    public void CheckACheckBox(Checkbox ck)
    {
    foreach (Control ckb in this.Controls)
    {
    if ((ckb is CheckBox) && (ckb == ck))
    ck.Checked = true;
    else
    ck.Checked = false;
    }
    }

    Thanks,
    Mani

  • #767968
    below is the gridview the records are in descending order of purchase date. i need to force the user to select first record then second and third ... and vicevsrsa for unselecting.

    Then i have a button down reserve which it will reserve all the selected records.

    my objective is to select record in above mentioned order and not like random records and reserve.



    Hope i explained.

    Delete Attachment

  • #767998
    Hey syed,

    Here i put some solution aboout this you prefered follow:

    <asp:GridView ID="grv_week_day" runat="server" AutoGenerateColumns="False"
    CssClass="datatable" OnRowDataBound="grv_week_day_RowDataBound" >
    <Columns>
    <asp:TemplateField HeaderStyle-Height="40px" HeaderStyle-Width="200px">
    <HeaderTemplate>
    <h2>week days
    </h2>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label ID="lbl_weekday" runat="server" CssClass="title" Text='<%# Bind("WeekDay") %>'></asp:Label>
    </ItemTemplate>
    <HeaderStyle Height="40px" Width="100px"></HeaderStyle>
    </asp:TemplateField>
    <asp:TemplateField HeaderStyle-Height="40px">
    <HeaderTemplate>
    <h2>Attendance Type</h2>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:DropDownList ID="drp_att" runat="server" AutoPostBack="True" Width="200px" Enabled="false"
    CausesValidation="false" OnSelectedIndexChanged="OnSelectedIndexChanged_drp">
    </asp:DropDownList>
    </ItemTemplate>
    <HeaderStyle Height="40px"></HeaderStyle>
    </asp:TemplateField>
    <asp:TemplateField HeaderStyle-Height="40px">
    <HeaderTemplate>
    <h2>From
    </h2>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label ID="lbl_From" runat="server"></asp:Label>
    </ItemTemplate>
    <HeaderStyle Height="40px"></HeaderStyle>
    </asp:TemplateField>
    <asp:TemplateField HeaderStyle-Height="40px">
    <HeaderTemplate>
    <h2>To
    </h2>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Label ID="lbl_To" runat="server"></asp:Label>
    </ItemTemplate>
    <HeaderStyle Height="40px"></HeaderStyle>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Short Day">
    <ItemTemplate>
    <asp:CheckBox ID="chk_short_day" runat="server" AutoPostBack ="true" OnCheckedChanged="chk_short_day_CheckedChanged" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>

    Hope It Helps,

    Thanks,
    Vaibhav Shah

  • #767999
    below is the gridview the records are in descending order of purchase date. i need to force the user to select first record then second and third ... and vicevsrsa for unselecting.

    Then i have a button down reserve which it will reserve all the selected records.

    my objective is to select record in above mentioned order and not like random records and reserve.



    Hope i explained.

    Delete Attachment

  • #768033
    Hey Syed

    Well what you can do is disabling the Update button if more than one checkbox was selected. Basically to your <ItemTemplate> you add a standart asp.net checkbox and you register the click event on all checkboxes (in js) and disable the update button if more than one was selected. I would include jquery in your project and add some code like the following:
    <script type="text/javascript">
    $(":checkbox").click(function()
    {
    if($(":checkox:checked").length > 1)
    {
    $("#idofupdatebutton").attr("disabled", "disabled");
    }
    else
    {
    $("#idofupdatebutton").removeAttr("disabled");
    }
    });
    </script>


    What it does is registering the click event of all checkboxes. If one is clicked it checks if more than one checkboxes are selected and disables the update button if that's the case. You have to replace "idofupdatebutton" with the ClientId of the Update Button.

    Hope that helps


  • Sign In to post your comments