This is a migrated thread and some comments may be shown as answers.

Hide button when EditCommand in RadGrid is clicked

5 Answers 315 Views
Button
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 16 Sep 2015, 07:50 PM

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#?

5 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 18 Sep 2015, 12:27 PM
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
0
Jason
Top achievements
Rank 1
answered on 18 Sep 2015, 01:15 PM

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
}

0
Konstantin Dikov
Telerik team
answered on 23 Sep 2015, 07:29 AM
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
0
Jason
Top achievements
Rank 1
answered on 24 Sep 2015, 08:11 PM
The button is located outside the grid.
0
Konstantin Dikov
Telerik team
answered on 28 Sep 2015, 01:05 PM
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
Tags
Button
Asked by
Jason
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Jason
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or