How to check if CheckBoxField in a Gridview is checked?
I have a webform with a GridView. In this GridView, I have a databound CheckBoxField. I also have a databound field, which is editable, with a statuscode (order-statuscode).
What I want to achieve is the following:
If the user edit's a row, and checks the checkbox, the statuscode needs to have a new value assigned, in case the user forgets to update the statuscode as well.
ASP.Net:
<asp:GridView ID="gvRestordrer" runat="server" DataSourceID="basketConn2" AutoGenerateColumns="False" DataKeyNames="k_ordre_id" CssClass="gvBasket" GridLines="None" Width="706px" AllowPaging="true" PageSize="10" OnRowEditing="getOrderDetails" OnRowCancelingEdit="clearOrderDetails" OnRowUpdating="clearOrderDetails">
<RowStyle CssClass="gvRow" />
<Columns>
...
// BoundField with statuscode. This value need to be updated on rowupdating, if Checkboxfield has been checked by the user
<asp:BoundField DataField="k_order_statuscode" HeaderText="Statuscode" SortExpression="k_order_statuscode" >
<HeaderStyle CssClass="gvHeader" />
</asp:BoundField>
// Checkboxfield bound to a database bool "k_order_completed"
<asp:CheckBoxField DataField="k_order_completed" HeaderText="Completed?" SortExpression="k_order_completed" >
<HeaderStyle CssClass="gvHeader" />
</asp:CheckBoxField>
// Commandfield
<asp:CommandField DeleteText="" EditText="Handle Order" ShowDeleteButton="True" ShowEditButton="True" UpdateText="Update Order" >
<HeaderStyle CssClass="gvHeader" />
</asp:CommandField>
...
</asp:GridView>
C# s开发者_JS百科tuff:
protected void clearOrderDetails(object sender, GridViewUpdateEventArgs e)
{
lblOrdreDetaljer.Text = "";
int index = Convert.ToInt32(e.RowIndex);
// Not working
if (gvRestordrer.Rows[index].Cells[4].Value == true)
{
basketConn2.UpdateParameters["k_order_statuscode"].DefaultValue = "4";
}
}
Is there other solutions, other than making a TemplateField and using a checkbox inside this? I figure there's gotta be a simpler way, but Google doesn't provide me with an answer.
Any help would be appreciated.
This may not work for a databound checkbox, but here's how I did it for checkboxes in a gridview.
Thus far I've been able to get the checked items via:
Checkboxes get added to a gridview:
internal static void AddCheckboxes(System.Web.UI.WebControls.GridView gv,IEnumerable<int> checkedRows)
{
foreach (var item in gv.Rows.Cast<System.Web.UI.WebControls.GridViewRow>().Select(c => c.Cells[0]).Where(cell => cell.Controls.Count == 0).WithIndex())
item.Item1.Controls.Add(new System.Web.UI.WebControls.CheckBox() { ClientIDMode = System.Web.UI.ClientIDMode.Static, ID = "ckUpdate" + item.Item2, Checked = checkedRows != null && checkedRows.Contains(item.Item2) });
}
then in a postback I use
internal static IEnumerable<int> GetCheckedRows(IEnumerable<string> paramKeys)
{
return paramKeys
.Where(f => f.Contains("$"))
.Where(f => System.Text.RegularExpressions.Regex.IsMatch(f, "ckUpdate[0-9]+$"))
.Select(p => int.Parse(System.Text.RegularExpressions.Regex.Match(p, "[0-9]+$").Value));
}
then calling
GetCheckedRows(Request.Params.AllKeys).ToList();
helper:
public static IEnumerable<Tuple<T, int>> WithIndex<T>(this IEnumerable<T> enumerable)
{
return enumerable.Select((item, index) => Tuple.Create(item, index));
}
精彩评论