943,739 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 755
  • ASP.NET RSS
Mar 1st, 2009
0

prevent a file from being deleted unless its selected first

Expand Post »
hi all.
ok i have been able to create an error message - if the user tries to delete a row which has not been selected first; but it still deletes from the gridview...

ASP.NET Syntax (Toggle Plain Text)
  1. void DeleteRowButton_Click(Object sender, EventArgs e)
  2. {
  3.  
  4. LinkButton btn = sender as LinkButton;
  5. GridViewRow row = btn.NamingContainer as GridViewRow;
  6.  
  7.  
  8. if (row == GridView1.SelectedRow) {
  9. GridView1.DeleteRow(GridView1.SelectedIndex);
  10.  
  11. // delete file from server
  12.  
  13. }
  14.  
  15. else
  16.  
  17. lblDeleteResult.Text = "Please select this file first, if you wish to delete it";
  18.  
  19.  
  20.  
  21.  
  22. }


but the asp.net delete command- Delete="Delete From tbl....Where.......etc" deletes the file from the gridview regardless. How can one prevent the delete command from running unless the row is selected first?

p.s. the delete command is specified in the .aspx file

thanks
Last edited by julseypart; Mar 1st, 2009 at 9:47 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
julseypart is offline Offline
42 posts
since Oct 2008
Mar 3rd, 2009
0

Re: prevent a file from being deleted unless its selected first

enable /disable the delete button in gridview based on the row selection.then this same code will works for u.let me know it works for u?
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
srikanthkadem is offline Offline
68 posts
since Mar 2008
Mar 3rd, 2009
0

Re: prevent a file from being deleted unless its selected first

good idea!, does anyone have some sample code to allow only the delete button on the 'selected row' visible?
Reputation Points: 10
Solved Threads: 0
Light Poster
julseypart is offline Offline
42 posts
since Oct 2008
Mar 10th, 2009
0

Re: prevent a file from being deleted unless its selected first

hi,

u can create delete button with in gridview itslef.check this example

ASP.NET Syntax (Toggle Plain Text)
  1. <asp:GridView ID="gdview" runat="server" ShowFooter="true" AutoGenerateColumns="False" DataKeyNames="CategoryID" OnRowCancelingEdit="gdview_RowCancelingEdit" OnRowDeleting="gdview_RowDeleting" OnRowEditing="gdview_RowEditing" OnRowUpdating="gdview_RowUpdating" Width="100%" AllowPaging="True" PageSize="5" OnPageIndexChanging="gdview_PageIndexChanging" OnRowDataBound="gdview_RowDataBound" >
  2. <Columns>
  3. <asp:BoundField HeaderText="Category Name" DataField="CategoryName" SortExpression="CategoryName" >
  4. <ItemStyle Height="20px" Width="150px" />
  5. </asp:BoundField>
  6.  
  7. <asp:CommandField ShowEditButton="True">
  8. <ItemStyle Width="100px" />
  9. </asp:CommandField>
  10. <asp:TemplateField>
  11.  
  12.  
  13.  
  14. <ItemTemplate>
  15. <asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
  16.  
  17. </ItemTemplate>
  18. <ItemStyle Width="100px" />
  19.  
  20. </asp:TemplateField>
  21. </Columns>
  22.  
  23.  
  24. </asp:GridView>
  25. protected void Page_Load(object sender, EventArgs e)
  26. {
  27.  
  28. if (!Page.IsPostBack)
  29. {
  30. bindgrid();
  31. total = 0;
  32.  
  33. }
  34.  
  35. }
  36.  
  37. public void bindgrid()
  38. {
  39. SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
  40. SqlCommand cmd = new SqlCommand("select CategoryName,CategoryID from Categories ", conn);
  41.  
  42. SqlDataAdapter da = new SqlDataAdapter("", conn);
  43. da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);
  44. DataSet ds = new DataSet();
  45. da.Fill(ds, "data");
  46. gdview.DataSource = ds.Tables[0].DefaultView;
  47. gdview.DataBind();
  48.  
  49.  
  50. }
  51. protected void gdview_RowDeleting(object sender, GridViewDeleteEventArgs e)
  52. {
  53.  
  54. int catid = int.Parse(gdview.DataKeys[0].Value.ToString());
  55. SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
  56. SqlDataAdapter da = new SqlDataAdapter("", conn);
  57. conn.Open();
  58. da.DeleteCommand = new SqlCommand("delete from Categories where CategoryID="+catid, conn);
  59. da.DeleteCommand.ExecuteNonQuery();
  60. conn.Close();
  61. bindgrid();
  62. }
Reputation Points: 25
Solved Threads: 29
Posting Whiz
greeny_1984 is offline Offline
372 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Read value from DB
Next Thread in ASP.NET Forum Timeline: Asp .Net with xml





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC