delete button problems in datagrid

Discussion in 'ASP .Net Datagrid Control' started by John, Jun 7, 2005.

  1. John

    John Guest

    i have a datagrid with a delete button which for some reason quit working,
    i'm thinking its a problem with the database but here is the code:
    SqlCommand cmdDeletePIN = null;


    try
    {
    myConnection = new SqlConnection(connectionString);
    myConnection.Open();

    cmdDeletePIN = new SqlCommand("usp_AdminDeactivatePIN", myConnection);
    cmdDeletePIN.CommandType = CommandType.StoredProcedure;

    SqlParameter prmClientID = cmdDeletePIN.Parameters.Add("@clientID",
    SqlDbType.Int);
    prmClientID.Value = Request.Params["clientID"];

    SqlParameter prmPIN= cmdDeletePIN.Parameters.Add("@PIN", SqlDbType.Int);
    prmPIN.Value = dgPINList.DataKeys[e.Item.ItemIndex].ToString();

    cmdDeletePIN.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
    Trace.Write("Admin - EditClient.aspx", "dgPINList_DeleteCommand() - ",
    ex);
    }
    finally
    {
    cmdDeletePIN.Dispose();
    myConnection.Close();
    }

    dgPINList.EditItemIndex = -1;
    bindGrid();
    }
    sorry if its hard to read. i know the method gets called by my image button
    because i put some trace.write's in there to follow execution. no exceptions
    are thrown because that trace.write never gets executed and i get no other
    errors. it just doesnt do anything except post back. also I have a link
    around the image button which causes a custom confirm box to popup, which
    works fine in mozilla, but doesnt show in IE 6.0 i know the code works
    because i tested it in its own page before adding it to the project.

    <asp:templatecolumn>
    <itemstyle horizontalalign="Center"></itemstyle>
    <itemtemplate>
    <a href="javascript:confirmDeactivatePIN()">
    <asp:ImageButton runat="server" id="ibtnDelete"
    CommandName="Delete" ImageUrl="images/icon_delete.gif"
    AlternateText="Deactivate this PIN"></asp:ImageButton>
    </a>
    </itemtemplate>
    </asp:templatecolumn>

    there's how a call the box. i also need to return a value from the box to
    determine if the user wans to deactivate the service or the entire pin based
    on which button they push in the custom box and get it to the codebehind for
    an if statement i have yet to write.

    Thanks in advance

    John
     
    John, Jun 7, 2005
    #1
    1. Advertisements

  2. John

    Elton Wang Guest

    Hi John,


    Generally speaking, your ImageButton in datagrid can trigger
    datagrid_ItemCommand event. In the event you can have

    if (e.CommandName.Equals("Delete"))
    {
    // process delete function
    }

    BTW, in the event, you can use e.Item.Cells[col_index].Text to get
    BoundColumn's data, or use ((TextBox)e.Item.FindControl("controlID")).Text
    to get TemplateColumn + TextBox's data.

    HTH

    Elton W
     
    Elton Wang, Jun 7, 2005
    #2
    1. Advertisements

  3. John

    John Guest

    it does trigger the datagrid_DeleteCommand event and that code does fire
    because i can read my trace.writes which i omitted in my post to make it
    easier to read. i'm just wondering if there is a problem in my code which is
    why things arent deleteing. also i need to know why my popup shows in
    mozilla but not in IE, and how to get the values it returns into the code
    behind. thanks for your help. if you need to see any code just shoot me an
    email at and i'll try to send whatever you need.

     
    John, Jun 7, 2005
    #3
  4. John

    Elton Wang Guest

    Could you post html code of the datagrid?

     
    Elton Wang, Jun 7, 2005
    #4
  5. John

    John Guest

    <td><asp:datagrid id="dgPINList" runat="server" backcolor="White"
    width="550px" datakeyfield="PIN"
    allowsorting="True" autogeneratecolumns="False"
    bordercolor="#3D653D">
    <alternatingitemstyle
    backcolor="#DDDDDD"></alternatingitemstyle>
    <headerstyle cssclass="tableHeader"></headerstyle>
    <columns>
    <asp:editcommandcolumn buttontype="LinkButton"
    updatetext="<img src='images/icon_save.gif' width='16' height='16' alt='Save
    Changes' border='0'>"
    canceltext="<img src='images/icon_cancel.gif' width='16'
    height='16' alt='Cancel Changes' border='0'>"
    edittext="<img src='images/icon_edit.gif' width='16'
    height='16' alt='Edit This Record' border='0'>"></asp:editcommandcolumn>
    <asp:templatecolumn>
    <itemstyle horizontalalign="Center"></itemstyle>
    <itemtemplate>
    <a href="javascript:confirmDeactivatePIN()">
    <asp:ImageButton runat="server" id="ibtnDelete"
    CommandName="Delete" ImageUrl="images/icon_delete.gif"
    AlternateText="Deactivate this PIN"></asp:ImageButton>
    </a>
    </itemtemplate>
    </asp:templatecolumn>

    there is more but i'm fairly certain that the rest of the columns dont
    matter as they are just data. and my boss recently told me that the delete
    command has never worked so if you dont mind could you or someone please take
    a look at the c# code i posted dealing with the sql commands and let me know
    why the clientID and PIN number arent in sinc

    thanks guys
     
    John, Jun 7, 2005
    #5
  6. John

    John Guest

    also here is my custom box...sorry but the code is rather long and possilbly
    hard to read once i put in on here

    function confirmDeactivatePIN(){
    var confirmwin;
    if (navigator.appName=="Microsoft Internet Explorer" ||
    navigator.appName=="MSIE 6.0"){
    confirmwin = window.showModalDialog("popup_ConfirmContent.aspx",
    "confirmwin",
    "dialogHeight:150px;dialogWidth:200px;dialogLeft:300;dialogTop:300;resizable:no; scroll:no");
    }
    if (navigator.appName=="Netscape"){
    var winHeight = 150;
    var winWidth = 200;
    var xPos = (screen.availWidth/2)-(winWidth/2);
    var yPos = (screen.availHeight/2)-(winHeight/2);
    confirmwin = window.open("", "confirmwin",
    "height="+winHeight+",width="+winWidth+",screenX="+xPos+",screenY="+yPos+",left="+xPos+",top="+yPos);
    var content = "<html>"+
    "<head>"+
    "<title>Confirm</title>"+
    "</head>"+
    "<body bgcolor='#CCCCCC' onblur='window.focus();'>"+
    "<center>"+
    "<form id=form1 name=form1>"+
    "<table bgcolor='green' border='0' cellpadding='0' cellspacing='0'>"+
    "<tr>"+
    "<td>"+
    "<table width='+(winWidth+20)+' height='+(winHeight-100)+'
    border='0'>"+
    "<tr>"+
    "<td colspan='3' height='+(winHeight-100)+' valign='top'
    align='left' bgcolor='white'>"+
    "Press Service to deactivate the PIN's service, PIN to
    deactivate their login and service"+
    "</td>"+
    "</tr>"+
    "<tr>"+
    "<td height='50' width='33%' valign='middle' align='center'
    bgcolor='white'>"+
    "<input type='button' width='50' style='width:50'
    value='Service' onclick='window.returnValue=7;window.close();'>"+
    "</td>"+
    "<td height='50' width='34%' valign='middle' align='center'
    bgcolor='white'>"+
    "<input type='button' width='50' style='width:50' value='PIN'
    onclick='window.returnValue=6;window.close();'>"+
    "</td>"+
    "<td height='50' width='33%' valign='middle' align='center'
    bgcolor='white'>"+
    "<input type='button' width='50' style='width:50'
    value='Cancel' onclick='window.close();'>"+
    "</td>"+
    "</tr>"+
    "</table>"+
    "</td>"+
    "</tr>"+
    "</table>"+
    "</center>"+
    "</form>"+
    "</body>"+
    "</html>";
    confirmwin.document.write(content);
    confirmwin.document.close();
    }//end if navigator.appname=="mozilla"
    }//end function
     
    John, Jun 7, 2005
    #6
  7. John

    Elton Wang Guest

    But I cannot see any data column in datagrid.
     
    Elton Wang, Jun 7, 2005
    #7
  8. John

    Elton Wang Guest

    Since you mentioned DeleteCommand event works, I suppose something wrong in
    your deleting logic, it's data related. Hence it's better to have data
    coulmns.
     
    Elton Wang, Jun 7, 2005
    #8
  9. John

    John Guest

    here is the entire datagrid control, i belive i know what is wrong but i'm
    not exactly sure how to fix it. the client ID and PIN numer need to match or
    the db wont find the correct listing and will therefore not delete anything,
    so i need to work on the delete logic and the stored procedure's. if you
    have any ideas on how to make the delete logic look for the correct client
    id. i belive i can fix that part now that i think about it somemore. but i
    cant seem to get my javascript to fire in IE 6.0, but it does fire in mozilla
    but the page wont post back. i NEED this script to fire send a value
    (determinied by what button the user pushes) back to the server to determine
    which stored procedure to run. Note: the if statement about which procedure
    to run has not been written yet beacuse i cant get the first few steps to
    work right. so for now i'd be happy getting a value back for now and worry
    about using it later.

    <td><asp:datagrid id="dgPINList" runat="server" backcolor="White"
    width="550px" datakeyfield="PIN"
    allowsorting="True" autogeneratecolumns="False"
    bordercolor="#3D653D">
    <alternatingitemstyle
    backcolor="#DDDDDD"></alternatingitemstyle>
    <headerstyle cssclass="tableHeader"></headerstyle>
    <columns>
    <asp:editcommandcolumn buttontype="LinkButton"
    updatetext="<img src='images/icon_save.gif' width='16' height='16' alt='Save
    Changes' border='0'>"
    canceltext="<img src='images/icon_cancel.gif' width='16'
    height='16' alt='Cancel Changes' border='0'>"
    edittext="<img src='images/icon_edit.gif' width='16'
    height='16' alt='Edit This Record' border='0'>"></asp:editcommandcolumn>
    <asp:templatecolumn>
    <itemstyle horizontalalign="Center"></itemstyle>
    <itemtemplate>
    <a href="javascript:confirmDeactivatePIN()">
    <asp:ImageButton runat="server" id="ibtnDelete"
    CommandName="Delete" ImageUrl="images/icon_delete.gif"
    AlternateText="Deactivate this PIN"></asp:ImageButton>
    </a>
    </itemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn>
    <itemstyle horizontalalign="Center"></itemstyle>
    <itemtemplate>
    <asp:label runat="server" id="Label1">
    <a href='javascript:popupClientWindow(<%#
    DataBinder.Eval(Container, "DataItem.PIN") %>);'>
    <img src="images/icon_login.gif" border="0" alt="Login
    Under this PIN"></a></asp:label>
    </itemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn>
    <itemstyle horizontalalign="Center"></itemstyle>
    <itemtemplate>
    <a href='SwitchBoard.aspx?mode=archive&clientID=<%#
    Request.Params["clientID"] %>&PIN=<%# DataBinder.Eval(Container,
    "DataItem.PIN") %>'>
    <img src="images/icon_archive.gif" width="16"
    height="16" border="0" alt="Archive this PIN"></a>
    </itemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn sortexpression="Pin" headertext="PIN">
    <itemtemplate>
    <a href='EditClient2.aspx?PIN=<%#
    DataBinder.Eval(Container, "DataItem.PIN") %>&clientID=<%#
    DataBinder.Eval(Container, "DataItem.ClientId") %>'>
    <%# DataBinder.Eval(Container, "DataItem.PIN") %>
    </a>
    </itemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn sortexpression="Description"
    headertext="Description">
    <itemtemplate>
    <asp:label runat="server" text='<%#
    DataBinder.Eval(Container, "DataItem.Description") %>' id="Label3">
    </asp:label>
    </itemtemplate>
    <edititemtemplate>
    <asp:textbox runat="server" text='<%#
    DataBinder.Eval(Container, "DataItem.Description") %>' width="180px"
    id="txtPINDescription">
    </asp:textbox>
    </edititemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn sortexpression="ANI" headertext="ANI">
    <itemtemplate>
    <asp:label runat="server" text='<%#
    DataBinder.Eval(Container, "DataItem.ANI") %>' id="Label4">
    </asp:label>
    </itemtemplate>
    <edititemtemplate>
    <asp:textbox runat="server" text='<%#
    DataBinder.Eval(Container, "DataItem.ANI") %>' width="80px" id="txtPINANI">
    </asp:textbox>
    </edititemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn sortexpression="Priority"
    headertext="Priority">
    <itemtemplate>
    <asp:label runat="server" text='<%#
    DataBinder.Eval(Container, "DataItem.Priority") %>' id="Label5">
    </asp:label>
    </itemtemplate>
    <edititemtemplate>
    <asp:textbox runat="server" text='<%#
    DataBinder.Eval(Container, "DataItem.Priority") %>' width="50px"
    id="txtPINPriority">
    </asp:textbox>
    </edititemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn sortexpression="CallsLeft"
    headertext="Calls Left">
    <itemtemplate>
    <asp:label runat="server" text='<%#
    DataBinder.Eval(Container, "DataItem.CallsLeft") %>' id="Label6">
    </asp:label>
    </itemtemplate>
    <edititemtemplate>
    <asp:textbox runat="server" text='<%#
    DataBinder.Eval(Container, "DataItem.CallsLeft") %>' width="50px"
    id="txtPINCallCount">
    </asp:textbox>
    </edititemtemplate>
    </asp:templatecolumn>
    </columns>
    </asp:datagrid><asp:label id="lblSortOrder" runat="server"
    visible="False"></asp:label></td>
     
    John, Jun 7, 2005
    #9
  10. John

    Elton Wang Guest

    Do you need to change client_ID or the client_ID(s) are from data source and
    are bound in datgrid, to say PIN(s)?

    If it's the second, you don't need do any thing in client-side (javascript).
    It's very easy to catch it on server-side. The datagrid has powerful
    function to obtain these data.


     
    Elton Wang, Jun 7, 2005
    #10
  11. John

    John Guest

    i fixed the problem with the delete button not deleting people, now i am left
    with what to you guys probably seems like a simple problem. i have to get
    that confirmDeactivatePIN function to fire in IE and take the value it
    returns and send it to an if statement in the code behind. if you can please
    help me with that issue i think i'll have my problems sloved until the next
    project.

    Thanks,
    John
     
    John, Jun 7, 2005
    #11
  12. John

    Elton Wang Guest

    deleting people??

    Anyway, Suppose you use DataItem.ClientId as @clientID in cmdDeletePIN.
    Following code snippet shows how to get the value from server-side:

    Datagrid html code:

    <asp:datagrid id="dgPINList" runat="server" backcolor="White"
    width="550px" datakeyfield="PIN"
    allowsorting="True" autogeneratecolumns="False"
    bordercolor="#3D653D">
    <alternatingitemstyle
    backcolor="#DDDDDD"></alternatingitemstyle>
    <headerstyle cssclass="tableHeader"></headerstyle>
    <columns>
    <!-- other columns -->
    <asp:BoundColumn DataField="ClientId" ReadOnly="True"
    HeaderText="Client"></asp:BoundColumn>
    </columns>
    </asp:datagrid>

    Code in DeleteCommand event
    string clientID = e.Item.Cells[8].Text;
    // ...
    SqlParameter prmClientID = cmdDeletePIN.Parameters.Add("@clientID",
    SqlDbType.Int);
    prmClientID.Value = clientID;
    //...


    HTH

    Elton Wang
     
    Elton Wang, Jun 8, 2005
    #12
  13. John

    John Guest

    no client id i dont think is shown in the grid or its showing the wrong one,
    anyway i managed to fix it. but i still cant get my custom confirm dialong
    to appear any ideas on how to make it work in IE, and get its return value
    back to the codebehind.
     
    John, Jun 8, 2005
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.