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

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
}
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

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