tags:

views:

25

answers:

1

hi ,

I have a grid which has check boxes and when selecting the top chek box select all will select all the check box in gridview and would update .for this im using for loop where it exceutes every time and this is taking lot of time cuase there are more thn 100 records in grid .

 try
{
    string StrOutputMessageDisplayDocReqCsu = string.Empty;
    string strid = string.Empty;
    string strflag = string.Empty;
    string strSelected = string.Empty;


    for (int j = 0; j < GdvDocReqMU.Rows.Count; j++)
    {
                    CheckBox Chkupdate = (CheckBox)GdvDocReqMU.Rows[j].Cells[1].FindControl("chkDR");
        if (Chkupdate != null)
        {
            if (Chkupdate.Checked)
            {
                strid = ((Label)GdvDocReqMU.Rows[j].FindControl("lblIDDocReqCsu")).Text;
                strflag = ((Label)GdvDocReqMU.Rows[j].FindControl("lblStatusDocReqCsu")).Text;                     
                cmd = new SqlCommand("sp_Update_v1", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", strSelected);
                cmd.Parameters.AddWithValue("@flag", DdlStatusDocReqMU.SelectedValue);
                cmd.Parameters.AddWithValue("@notes", txtnotesDocReqMU.Text);
                cmd.Parameters.AddWithValue("@user", strUseridDRAhk);
                cmd.Parameters.Add(new SqlParameter("@message", SqlDbType.VarChar, 100, ParameterDirection.Output, false, 0, 50, "message", DataRowVersion.Default, null));
                cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

            }

        }
    }
    //}

    StrOutputMessageDisplayDocReqCsu += (string)cmd.Parameters["@message"].Value + "<br/>";
    lbldbmessDocReqAhk.Text += StrOutputMessageDisplayDocReqCsu + "<br>";
    GetgridDocReq();
 }
catch (Exception ex)
{
    lbldbmessDocReqAhk.Text = ex.Message.ToString();
}

can some please help me on this where i can capture all the id in one string n pass it to procedure

Thanks

A: 

Here is how I've handled a similar situation (using jQuery to check the checkboxes):

  1. Add a single <asp:CheckBox Id="whatever" CssClass="chkAllSelector" ... to the column's header
  2. Add CssClass="chkRowSelector" to your current checkboxes
  3. Add a footer template to this column's footer with an update button or linkbutton.
  4. in the button's onclick, perform your update.

The ASP.NET checkbox control is weird because it wraps a span around the generated inputs.

Here is the jQuery code I've used before:

$('.chkAllSelector > input:checkbox').click(function() {
    $('.chkRowSelector > input:checkbox').each(
        function(){
            this.checked = $('.chkAllSelector > input:checkbox').attr('checked');
        }
    );
});
$('.chkRowSelector > input:checkbox').click(function() {
if ($('.chkAllSelector > input:checkbox').is(':checked') && !($(this).is(':checked')))
     { $('.chkAllSelector > input:checkbox').removeAttr('checked'); }
 });

As for your sql statement, since you're using a stored procedure that is updating only one row at a time, you're going to make a single call for every row. I'd suggest writing another stored procedure that takes an array of ids and sets the checked value. Note, you'll have to do this call once for all checked values and once for all unchecked values. If your logic for checking/unchecking is pretty atomic, e.g. checking the box doesn't change values in other tables within your stored procedure, it may be better to create a simple update statement and use it here. It's hard to say without knowing the logic in your stored procedure.

To avoid numerous headaches with the update scenario, you may also want to take a look at the SqlDataSource: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/sqldatasource.aspx
I use ObjectDataSource for nearly everything, because it handles insert/update/delete logic pretty nicely.

Edit: To get a comma separated list of values:

string idsCsv = GdvDocReqMU.Rows.Cast<GridViewRow>()
    .Where(x => ((CheckBox)x.FindControl("chkDR")).Checked)
    .Select(x => GdvDocReqMU.DataKeys[x.RowIndex].Value.ToString())
    .ToArray().Join(',');

I just wrote that from scratch, so I'd be surprised if it works without a little tweaking, but that's the general idea.

For this to work, you have to set the gridview's DataKeyNames="Id" where "Id" is the id of the row being bound. For instance if you're binding from sql and the column being bound to the gridview is "Customer_Id", you'd have DataKeyNames="Customer_Id".

Jim Schubert
Jim i have dynamice sql which takes multiple id's and javscript for select all . but the problem is in code where once i decalre string n capture in it because of the loop its executing twice and only first record is getting inserted.Thanks for you answer .
SmartDev
SmartDev: I must have misunderstood the issue. I updated the answer with a way to get a comma-separated string of ids in which the rows are checked.
Jim Schubert