-
GridView RowDeleting
Hi I have the following gridview which contains a CheckBoxField. Now when delete is clicked I want to do an if statement to see if the checkbox is checked and if so cancel the delete and display an error message.
Not quite sure how to do this.
Gridview code:
Code:
<asp:GridView ID="SharedGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlManageSharedArea" CellPadding="4" ForeColor="#333333" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" datakeynames="doc_area_id" onrowdeleted="SharedGridView_RowDeleted" onrowdeleting="SharedGridView_RowDeleting" AllowPaging="True" AllowSorting="True" PageSize="5" style="position: static">
<Columns>
<asp:BoundField DataField="doc_area_name" HeaderText="Name" SortExpression="doc_area_name" />
<asp:templatefield headertext="Type">
<itemtemplate>
<%# GetAreaTypeSharedString(Eval("doc_area_type")) %>
</itemtemplate>
</asp:templatefield>
<asp:CheckBoxField DataField="doc_area_default" HeaderText="Default" SortExpression="doc_area_default" />
<asp:HyperLinkField Text="<img src='../images/edit.gif' border='0' />" DataNavigateUrlFormatString="EditSharedDocumentArea.aspx?doc_area_id={0}" DataNavigateUrlFields="doc_area_id" />
<asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/Delete.gif" DeleteText="Delete Area" ShowDeleteButton="True">
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:CommandField>
</Columns>
</asp:GridView>
If statement in code behind:
Code:
protected void SharedGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
if (SharedGridView ??????????
{
e.Cancel = true;
MsgShared.Text = "The area has not been deleted. You may not delete the Shared Area Default.";
}
}
Thanks for any help you can give!
-
Re: GridView RowDeleting
I am struggling to understand why you need a delete button on every row as well as a checkbox. Surely you either need checkboxes with single button, or buttons on every row and no checkboxes? :confused:
Anyway,
Heres how I would do it with 1 delete button. This will show you how to check if a certain row's checkbox is checked. Although, for this to work - you need to give you CheckBoxField a name (called chkBox in this code).
Code:
protected void buttonDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow r in SharedGridView.Rows)
{
CheckBox cBox = r.Controls.ToString("chkBox");
if (cBox.Checked)
{
// Delete Row? Heres your doc_area_id.
String docAreaID = r.DataItemIndex;
}
else
{
// Probably best to just not delete if not selected.
// Your user could be tied into pressing no a load of times otherwise.
};
};
}
Hope that is useful. ;)
-
Re: GridView RowDeleting
No, the checkbox is not for the user to check if he wants to delete it. The checkbox is not editable, only one checkbox is ever checked in the gridview (from a value from the database).
I want it so if the delete button is clicked and the checkbox is checked then just to display an error message but if its not checked then to delete.
Cheers, Mark
-
Re: GridView RowDeleting
Oh right, sorry..
makes sense now ;)
Try something like this:
Code:
protected void SharedGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
GridViewRow r = SharedGridView.Rows(e.RowIndex);
CheckBox cBox = r.Controls("chkBox");
if (cBox.Checked)
{
e.Cancel;
};
}
protected void SharedGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
CheckBox cBox = e.Row.Controls("chkBox");
if (cBox.Checked)
{
// Get your delete button
ImageButton iBtn = e.Row.Cells(4).Controls(0);
// You could make it invisible i.e
// iBtn.Visible = false;
// Or you can add a javascript alert
iBtn.Attributes.Add("onclick", "alert('You cant delete this');");
}
}
HTH :thumb:
-
Re: GridView RowDeleting
sort of a hibrid of my own stuff mixed with the dot net:
Code:
protected void deleteBookmarks(object sender, ImageClickEventArgs e)
{
DataKeyArray aryKeys = GridView1.DataKeys;//these need to be defined in the aspx file gridview attributes
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox RowCheckBox = (CheckBox)gvr.FindControl("bookmarkID");//bookmarkID is the primary key value I have defined as a non-visible column representing a unique identifier in the database
if (RowCheckBox.Checked == true)
{
deleteBookmark(long.Parse(aryKeys[gvr.DataItemIndex].Value.ToString()));//this is a call to my own function to delete the row from the database
GridView1.DeleteRow(gvr.RowIndex);
}
}
}
-
Re: GridView RowDeleting
Thanks for the replies, I have now got the following message from doing the below code:
'System.Web.UI.WebControls.GridView.Rows' is a 'property' but is used like a 'method'
Code:
<asp:GridView ID="SharedGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlManageSharedArea" CellPadding="4" ForeColor="#333333" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" datakeynames="doc_area_id" onrowdatabound="SharedGridView_RowDataBound" onrowdeleted="SharedGridView_RowDeleted" AllowPaging="True" AllowSorting="True" PageSize="5" style="position: static">
<Columns>
<asp:BoundField DataField="doc_area_name" HeaderText="Name" SortExpression="doc_area_name" />
<asp:templatefield headertext="Type">
<itemtemplate>
<%# GetAreaTypeSharedString(Eval("doc_area_type")) %>
</itemtemplate>
</asp:templatefield>
<asp:CheckBoxField DataField="doc_area_default" HeaderText="Default" SortExpression="doc_area_default" />
<asp:HyperLinkField Text="<img src='../images/edit.gif' border='0' />" DataNavigateUrlFormatString="EditSharedDocumentArea.aspx?doc_area_id={0}" DataNavigateUrlFields="doc_area_id" />
<asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/Delete.gif" DeleteText="Delete Area" ShowDeleteButton="True">
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:CommandField>
</Columns>
</asp:GridView>
Code:
protected void SharedGridView_RowDeleted(Object sender, GridViewDeletedEventArgs e)
{
// Display whether the delete operation succeeded.
if (e.Exception == null)
{
MsgShared.Text = "The Shared Area has been deleted successfully.";
}
else
{
MsgShared.Text = "An error occurred while attempting to delete the area.";
e.ExceptionHandled = true;
}
}
protected void SharedGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
GridViewRow r = SharedGridView.Rows(e.RowIndex);
CheckBox cBox = r.Controls("chkBox");
if (cBox.Checked)
{
e.Cancel;
};
}
protected void SharedGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
CheckBox cBox = e.Row.Controls("chkBox");
if (cBox.Checked)
{
// Get your delete button
ImageButton iBtn = e.Row.Cells(4).Controls(0);
// You could make it invisible i.e
// iBtn.Visible = false;
// Or you can add a javascript alert
iBtn.Attributes.Add("onclick", "alert('You cant delete this');");
}
}
Cheers, Mark
-
Re: GridView RowDeleting
Which line throws the error?
ps: maybe you need to change your checkbox field into a regualar checkbox inside a template field. Remember to call the checkbox "chkBox".
-
Re: GridView RowDeleting
Hi, that message is referring to the line:
GridViewRow r = SharedGridView.Rows(e.RowIndex);
I have now changed the checkbox field to:
Code:
<asp:TemplateField HeaderText="Default" SortExpression="doc_area_default">
<ItemTemplate>
<asp:CheckBox id="chkBox" runat="server" value="<%# Eval("doc_area_default") %>"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
And am getting the following message:
The server tag is not well formed.
Have I done the value bit correct? should there be a DataField somewhere in here like there was in the checkbox field?
Mark
-
Re: GridView RowDeleting
Try your template field like this:
Code:
<asp:TemplateField HeaderText="Default" SortExpression="doc_area_default">
<ItemTemplate>
<asp:CheckBox id="chkBox" runat="server" checked="<%# Eval('doc_area_default') %>" />
</ItemTemplate>
</asp:TemplateField>
I have only recently started getting into c#. Before, I was big into vb, so I am still making a few syntax mistakes. Try the error raising line like this maybe:
Code:
GridViewRow r = SharedGridView.Rows[e.RowIndex];
-
Re: GridView RowDeleting
Hi, I now have:
Too many characters in character literal
pointing to the line:
<asp:CheckBox id="chkBox" runat="server" checked="<%# Eval('doc_area_default') %>" />
Cheers, Mark
-
Re: GridView RowDeleting
Exactly what data is in doc_area_default? should be 'true' or 'false' or '1' or '0'
-
Re: GridView RowDeleting
yep it is either a 1 or a 0
-
Re: GridView RowDeleting
Instead of
Code:
<%# Eval('doc_area_default') %>
try
Code:
<%# DataBinder.Eval (Container.DataItem, 'doc_area_default') %>
-
Re: GridView RowDeleting
No, its still saying Too many characters in character literal on the same line.
-
Re: GridView RowDeleting
Not sure what the problem could be. Would it be possible to take a look at the project?