Question OnItemCommand problem

maxcata

Member
Joined
Jan 11, 2018
Messages
6
Programming Experience
1-3
Hi there, I've got a problem with this command. I' have a button inside a repeater with this piece of html:

Repeater:
<asp:Repeater runat="server" ID="rptDocumenti" OnItemCommand="rptDocumenti_OnItemCommand">
  <ItemTemplate>
    <li>
      <asp:Literal runat="server" ID="litDocumento" Text='<%# httpLinkToADocument("http://" + ConfigurationManager.AppSettings["piattaforma"].ToString() + "/" + Eval("percorsoDocumento").ToString().Trim(), string.Format("{0}", Eval("nomeDocumento").ToString().Trim()), "_blank", "", "") %>' />
      <span class="float-right"><i class="far fa-clock"></i><%# Convert.ToDateTime(Eval("dataInserimento").ToString()).ToString(" dd/MM/yyyy alle HH:mm") %></span>
      <asp:LinkButton CssClass="btn btn-sm btn-danger ml-3" runat="server" ID="btnElimina" ToolTip="Elimina" CommandName="elimina" CommandArgument='<%# Eval("sysId").ToString() %>' Text="<i class='fas fa-trash'></i>" Visible='<%# !Convert.ToBoolean(Eval("obbligatorio").ToString()) %>' />
      <asp:ConfirmButtonExtender ID="cbebtnElimina" runat="server" TargetControlID="btnElimina" ConfirmText="Conferma cancellazione" />
                <hr class="m-1" />
    </li>
  </ItemTemplate>
</asp:Repeater>

C#:
        protected void rptDocumenti_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName.ToString())
            {
                case "elimina":
                {
                     if (deleteAFile(getNomeDocumentoByIdDocumento(e.CommandArgument.ToString()), "~/Repository/Docs/"))
                     {
                           SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
                           SqlCommand cmd = new SqlCommand("TDocumento_Delete", conn);

                           //add the parameter to the SqlCommand object
                           cmd.Parameters.Add("@sysId", SqlDbType.VarChar).Value = e.CommandArgument.ToString();

                           //open connection
                           conn.Open();

                           //set the SQLCommand type to StoredProcedure
                           cmd.CommandType = CommandType.StoredProcedure;

                           //execute the stored procedure                   
                           cmd.ExecuteNonQuery();

                           //close connection
                           conn.Close();

                           loadDocumenti(Request.QueryString["p"].ToString());
                     }
                     break;
                }

                default:
                     break;

            }
        }

When I run a dubug the program run only the first Statement, switch (e.CommandName.ToString()) and then exit from the routine.
I'can't explain why that appen.
Any help is appreciated.
Thx
 
Moved this thread to WebForms which is more appropriate than where the original ".NET Framework" was posted.
 
Not directly related to your problem but I wanted to point out that CommandName is already a string. There is no need to use ToString() on it.

Anyway, back to your problem: What was the value of CommandName while you were debugging?
 
Yes, You are right. but it's not the problem. It's only a try if it was it.
the value is "elimina" (delete in italian)
 
Last edited:
Try setting a breakpoint on line 7. If the value truly is "elimina" the breakpoint should be hit.
 
only the first Statement, switch (e.CommandName.ToString()) and then exit from the routine
Can you post a screenshot of the debugger showing the value of e.CommandName truly really is "elimina", no trailing spaces, no accented characters etc ?

Show a screenshot of the immediate window result of doing "elimina".Equals(e.CommandName) ?
 
This is a screenshot of the debug

If I go on the program leaves the routine.
 

Attachments

  • Debug.png
    Debug.png
    148.9 KB · Views: 10
I'll try to put only three statements in the method in the same way:


Repeater:
 protected void rptDocumenti_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string x = "1";
            string y = "1";
            string z = "1";
        }

The debug stops on the first statement and leave the routine.
It's incredible.......... I Thought that only in Javascript happened so!
 
Last edited:
Based on you screenshot in post #7, you are debugging a Release build. Release builds are optimized by the compiler and may not have a line by line correspondence with the code anymore. Please try debugging a Debug build.
 
Back
Top Bottom