I want to prompt the user for confirmation when he tries to delete a record in a detail view? I have command filed in which showDeletebutton set to true.
I found how to do the confirmation for gridview, but how can I modify to match detail view?
Code:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// loop all data rows
foreach (DataControlFieldCell cell in e.Row.Cells)
{
// check all cells in one row
foreach (Control control in cell.Controls)
{
// Must use LinkButton here instead of ImageButton
// if you are having Links (not images) as the command button.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete")
// Add delete confirmation
button.OnClientClick = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
}
}
Anybody?
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
.....
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity"
SortExpression="Quantity" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="New" Text="New"></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView
This can be done easily on the markup code. I simply added the js code to the onClientClick property of the delete button:
OnClientClick="return confirm('Are you sure you want to delete this record');"
Or if you want do this in the code behind:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
LinkButton bttn = (LinkButton)DetailsView1.FindControl("lnkDelete");
bttn.OnClientClick = "return confirm('Are you sure you want to delete this record!');";
}
I found the answer to my question.
My answer:
protected void DViewComputer_DataBound1(object sender, EventArgs e)
{
int noRow = DViewComputer.Rows.Count - 1;//get the no of record
if (noRow >0)
{
Button button = (Button)(DViewComputer.Rows[noRow].Cells[0].Controls[2]);
// Add delete confirmation
((System.Web.UI.WebControls.Button)(button)).OnClientClick = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
Anyways thanks for your help guys.
foreach (Control control in cell.Controls)
{
// Must use LinkButton here instead of ImageButton
// if you are having Links (not images) as the command button.
ImageButton button = control as ImageButton;
if (button != null && button.CommandName == "Delete")
// Add delete confirmation
button.Attributes.Add("onclick","your javascript here");
}
Please see the below URL......
http://www.codeproject.com/Articles/32756/ASP-NET-GridView-delete-confirmation-using-asp-Com
This corrects the OP's solution. The code was translated from the code found here: http://forums.aspfree.com/net-development-11/confirm-button-when-deleting-detailsview-120113-2.html
protected void dvEvent_DataBound(object sender, EventArgs e)
{
int commandRowIndex = dvEvent.Rows.Count - 1;
if (commandRowIndex > 0)
{
DetailsViewRow commandRow = dvEvent.Rows[commandRowIndex];
DataControlFieldCell cell = (DataControlFieldCell)commandRow.Controls[0];
foreach (Control ctrl in cell.Controls)
{
if (ctrl is ImageButton)
{
ImageButton ibt = (ImageButton)ctrl;
if (ibt.CommandName == "Delete")
{
ibt.ToolTip = "Click here to Delete";
ibt.CommandName = "Delete";
ibt.Attributes["onClick"] = "if (!confirm('Are you sure " +
"you want to delete this record?')) return;";
}
}
}
}
}
Related
How do i disable/hide a button inside that is inside a itemtemplate? i want to hide the save button on load, and then show it and hide the edit button when the edit button is clicked.
The template:
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonEdit" runat="server" CommandName="Edit" ImageUrl="loginstyling/images/Edit.png" Visible="true" />
<asp:ImageButton ID="ImageButtonUpdate" runat="server" CommandName="Update" ImageUrl="loginstyling/images/Save.png" Visible="true" OnClick="ImageButtonUpdate_Click" />
<asp:ImageButton ID="ImageButtonDelete" runat="server" CommandName="Delete" ImageUrl="loginstyling/images/Remove.png"/>
</ItemTemplate>
</asp:TemplateField>
Behind:
private void ImageButtonUpdate_Click(object sender, EventArgs e)
{
ImageButtonUpdate.Enabled = false; // not working, dosnt find the button
}
use the sender of the event
protected void ImageButtonUpdate_Click(object sender, EventArgs e){
ImageButton btnupdate= sender as ImageButton;
btnupdate.Enabled = false;
//if you need to get other controls in the same row
GridViewRow row = (GridViewRow)btnupdate.NamingContainer;
var btnedit= (ImageButton)row.FindControl("ImageButtonEdit");
btnedit.Enabled = false; // do enable or disable as you need
}
You Need to retrieve the ImageButton as follows as it is residing in the GridView
private void ImageButtonUpdate_Click(object sender, EventArgs e)
{
//Get the ImageButton that raised the event
ImageButton btn = (ImageButton )sender;
btn .Enabled = false;
}
Edit
Step 1
Provide same Onclick event for Update and Edit i.e
OnClick="ImageButtonUpdate_Click"
Step 2
Use CommandName Attribute
ImageButton btn = (ImageButton )sender;
if(btn.CommandName =="Update")
{
btn.Enabled = false;
}
if(btn.CommandName =="Edit")
{
btn.Enabled = false;
}
Hi I would like to know how to get the text field and index from a hyperlink in a gridview that has been clicked. Basically, the user would click on the hyperlink in the gridview and when the user has been navigated to the link, the text field and index of the link would be stored into an arraylist. Does anyone have any idea how I can go about doing this?
I have came up with this "pseudo code" for the onrowdatabound event handler in gridview:
ArrayList linksClicked = new ArrayList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("links");
if (hl != null)
{
linksClicked.Add(h1.ToString());
}
}
You should use ItemTemplate with LinkButton. In this button you can keep index or id like CommandArgument, also you easily catch event onClick and add index to your array. Use this sample.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="hyperLinkButton" Text="link" PostBackUrl="youruri.com" runat="server"
CommandArgument="<%# Eval("SomeFieldYouNeedArguementFrom") %>" OnClick="hyperLinkButton_Click" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void hyperLinkButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument;
// do what you need here
}
Previously in order to delete my record i will use double click, but when it comes to tablet or phone the double click will identify as zoom in / zoom out, therefore i change to onclick with confirmation message and here comes the problem, how should i delete it after user press ok?
P/S: I didn't use button but by selecting the row to delete the data
current onclick code
e.Row.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this order? ');");
Previous doubleclick delete code
//e.Row.Attributes.Add("ondblclick", "__doPostBack('CView','Select$" + e.Row.RowIndex + "');");
the full code of it
protected void CView_RowDataBound(Object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#cccccc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
e.Row.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this order?');");
//e.Row.Attributes.Add("ondblclick", "__doPostBack('CView','Select$" + e.Row.RowIndex + "');");
}
}
I'm still searching the answer through google. If someone else asked the same question before do share me the link. Thank you
If below line deletes the row from gridview
e.Row.Attributes.Add("ondblclick", "__doPostBack('CView','Select$" + e.Row.RowIndex + "');");
and you want to call above functionality on confirmation then you can do following changes.
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#cccccc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
e.Row.Attributes.Add("onclick", "var vconfirm = confirm('Are you sure you want to delete this order?'); if (vconfirm){ __doPostBack('CView','Select$" + e.Row.RowIndex + "');}");
}
Just try to assign the OnclientClick in aspx code for the button as follows
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="deletebtn" runat="server" CommandName="Delete"
Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this order? ');" />
</ItemTemplate>
</asp:TemplateField>
Try the following code
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="OnRowDeleting" AutoGenerateColumns = "false" OnRowDataBound = "OnRowDataBound">
<Columns>
<asp:BoundField DataField="..columnname.." HeaderText="...." />
<asp:BoundField DataField="..columname.." HeaderText="...." />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
Then on Row DataBound
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string item = e.Row.Cells[0].Text;
foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>())
{
if (button.CommandName == "Delete")
{
button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };";
}
}
}
}
Then on row deleting
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
// delete code
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[index].Delete();
ViewState["dt"] = dt;
BindGrid();
}
i want to disable button in row data bound . when its text or value is 'Waiting for Approval'. im getting this error . Object reference not set to an instance of an object.// button.Enabled = true;
protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Button button = (Button)e.Row.FindControl("btnReportedlink");
string Id =((DataRowView)e.Row.DataItem)["ReportLinks"].ToString();
if (Id == "Waiting for Approval")
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}
my aspx
<asp:TemplateField HeaderText="Reportd Link" ItemStyle-HorizontalAlign="center" >
<ItemTemplate>
<button onclick="window.open('<%#Eval("ReportLinks")%>', '_blank');" title='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"> Link</button>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
Why are you using the HTML <button /> element ? use <asp:button /> web server control from asp.net for a better control over reading and disabling the server controls.
Use the OnClientClick property to specify additional client-side script that executes when a Button control's Click event is raised.
<ItemTemplate>
<asp:button onclientclick="javascript:window.open('<%#Eval("ReportLinks")%>', '_blank');"
text='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"/>
</ItemTemplate>
With the above setup, now you will be able to access the button in row data bound event with NO more object references error.
use DataBinder work fine
protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Button button = (Button)e.Row.FindControl("btnReportedlink");
string Id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ReportLinks"));
if (Id == "Waiting for Approval")
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}
I want to delete record from GridView.Before to this ask for confirmation like "Are you sure to delete?"
I used command field in GridView,
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
I wrote a function in javascript
function confirm_Delete()
{
var r = confirm("Are you sure you want to Remove this Record!");
if (r == true)
{
alert("Record Deleted");
return true;
}
else
{
return false;
}
}
How I will call this on delete click.
Kindly suggest !
You can't achieve this using the command field, you have to make a template field:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete"
OnClientClick="javascript:return confirm('Are you sure you want to Remove this Record!');">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
It will behave the same way you are doing currently with the Command Field.
I would do the same as #Muhammad told you, and at the server side code for deleting I would also register an script for showing the "Record Deleted" message, as follows;
public void MethodForDeletingARecord()
{
ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "RecordDeletedMessage", "javascript:alert('Record Deleted');", true);
}
I think you can achieve this for commandfield
Assuming it would be the first column, Found Here
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// reference the Delete LinkButton
LinkButton db = (LinkButton)e.Row.Cells[0].Controls[0];
db.OnClientClick = "javascript:return confirm('Are you certain you want to delete?');"
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "return Conformation();");
}
}