How to send check boxes ID in gridview in one string for mass update
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
Here is how I've handled a similar situation (using jQuery to check the checkboxes):
- Add a single
<asp:CheckBox Id="whatever" CssClass="chkAllSelector" ...
to the column's header - Add
CssClass="chkRowSelector"
to your current checkboxes - Add a footer template to this column's footer with an update button or linkbutton.
- 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".
精彩评论