Hide button when EditCommand in RadGrid is clicked

6 posts, 0 answers
  1. Jason
    Jason avatar
    4 posts
    Member since:
    Jul 2015

    Posted 16 Sep 2015 Link to this post

    I'm fairly new to Telerik so please bear with me.

     

    I have two buttons: One does a SQL Insert and the other does a SQL Update, both work correctly as they currently are.  The update button is currently hidden from the user.  What I want to happen is if the user clicks the EditCommand link in the RadGrid, I want the Insert button to hide and the Update button to become visible. My question is - how do you interact with the EditCommand in C#?

  2. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 18 Sep 2015 Link to this post

    Hello Jason,

    Generally, you can use the ItemCommand event handler provided by RadGrid to catch the command actions:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.EditCommandName)
        {
            // execute custom logic to toggle the visibility of the buttons
        }
    }

    An alternative approach would be:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (RadGrid1.MasterTableView.IsItemInserted)
        {
            // insert mode is open
        }
        if (RadGrid1.EditIndexes.Count > 0)
        {
            // edit mode is open
        }
    }

    Hope this helps. Please give it a try and let me know if it works for you.

    Regards,
    Eyup
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jason
    Jason avatar
    4 posts
    Member since:
    Jul 2015

    Posted 18 Sep 2015 in reply to Eyup Link to this post

    I managed to get this to work by hiding the Insert button and showing the Update button from within my ItemCommand event handler.  I had to make some changes my SQL query and it led me to creating a UpdateCommand event.  Currently I'm not sure how to call that event in my button click.  How do I make that call?

    Here is my code for the UpdateCommand:

    protected void RadGrid2_UpdateCommand(object sender, GridCommandEventArgs e)
    {
         String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["KBConnectionString"].ConnectionString;
     
         string answerid = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["AnswerID"].ToString();
     
         using (SqlConnection conn = new SqlConnection(connectionString))
         {
              conn.Open();
              SqlCommand cmd = new SqlCommand("UPDATE Answers SET Answer_Name = @prmAnswer_Name, Answer = @prmAnswer, TimeAdded = CURRENT_TIMESTAMP WHERE AnswerID = " + answerid, conn);
              cmd.Parameters.Add(new SqlParameter("@prmAnswer_Name", RadTextBox1.Text));
              cmd.Parameters.Add(new SqlParameter("@prmAnswer", RadEditor.Text));
              cmd.ExecuteNonQuery();
              conn.Close();
         }
    }

    Here is the event for my button click:

    protected void RadButton2_Click(object sender, EventArgs e)
    {
         // I am not sure how to call the UpdateCommand event
    }

  5. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 23 Sep 2015 Link to this post

    Hi Jason,

    You could initiate the UpdateCommand by calling the MasterTableView's PerformUpdate method, which accepts a GridEditableItem:
    RadGrid1.MasterTableView.PerformUpdate(editedItem, true);

    However, although that it is not clear enough if your buttons are placed within the rows or outside the grid, if they are placed within the items you can set the CommandName property of the buttons, which will automatically initiate the update/insert:
    <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>

    The above is a simple example from our EditForm Template demo.

    If any other questions arise, please elaborate on your exact scenario.


    Regards,
    Konstantin Dikov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Jason
    Jason avatar
    4 posts
    Member since:
    Jul 2015

    Posted 24 Sep 2015 Link to this post

    The button is located outside the grid.
  7. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 28 Sep 2015 Link to this post

    Hi Jason,

    For a scenario where your button is outside the grid, you should use the first suggestion from my previous post, where could call the PerformUpdate method for the GridEditableItems that you want to update.


    Best Regards,
    Konstantin Dikov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017