如何从按钮单击调用UpdateCommand?

时间:2015-09-21 13:53:53

标签: c# asp.net telerik

我正在开发Q&amp ;;的某些页面的功能。一个拥有完整编辑/删除功能的网站。

以下是程序:

1.用户在一个页面上单击数据源超链接的链接,然后将他们单击的QuestionID传输到另一个填充两个RadGrids的页面。这些RadGrids是问题和答案。

2.问题网格填充用户单击的问题,并且答案网格填充与问题表共享相同QuestionID的所有答案。

3.然后,用户可以单击Answer RadGrid中的“Edit”,然后使用存储提交该答案的人的名称填充RadTextBox,并使用他们的答案填充RadEditor。需要注意的另一个功能是,当用户单击“编辑”按钮时,它会隐藏“提交”按钮(此按钮具有SQL插入)并生成“更新”按钮(此按钮具有SQL更新)。

我遇到的问题是我必须更改Update按钮,因为我的查询正在更新与该QuestionID相关的所有Answers,即错误的SQL编程。为了抵消这种情况,我创建了一个UpdateCommand事件,它获取AnswerID而不是QuestionID。

我的问题是如何从“更新”按钮调用该UpdateCommand事件?

这是我的ItemCommand事件:

protected void RadGrid2_ItemCommand(object sender, GridCommandEventArgs e)
{  // This populates RadTextBox1 and RadEditor
     switch (e.CommandName)
     {
          case RadGrid.EditCommandName:
               e.Canceled = true;
               e.Item.Selected = true;
               e.Item.Edit = true;
               RadTextBox1.Text = (e.Item as GridDataItem)["AnsweredBy"].Text;
               RadEditor.Content = (e.Item as GridDataItem)["Answer(s)"].Text;
               break;
          case RadGrid.UpdateCommandName:
               SqlDataSource2.UpdateParameters.Add("Answered By", RadTextBox1.Text);
               SqlDataSource2.UpdateParameters.Add("Answer(s)", RadEditor.Content);
               break;
          default:
               break;                  
      }
      RadButton1.Visible = false;
      RadButton2.Visible = true;
 }

这是我的UpdateCommandEvent:

protected void RadGrid2_UpdateCommand(object sender, GridCommandEventArgs e)
{  // This performs the SQL Update
     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();
      }
}

最后,这是我的ButtonClick事件:

protected void RadButton2_Click(object sender, EventArgs e)
{
     // How do I call the UpdateCommand event?
}

我还应该注意调用RadGrid2_UpdateCommand(sender,e);不起作用,因为它无法从EventArgs转换为GridCommandEventArgs。

这是RadGrid2:

    <telerik:RadGrid ID="RadGrid2"
                runat="server" 
                DataSourceID="SqlDataSource2" 
                GroupPanelPosition="Top" 
                OnNeedDataSource="RadGrid2_NeedDataSource" 
                OnDeleteCommand="RadGrid2_DeleteCommand" 
                OnUpdateCommand="RadGrid2_UpdateCommand" 
                OnItemCommand="RadGrid2_ItemCommand">        
                <MasterTableView DataSourceID="SqlDataSource2" AutoGenerateColumns="False" DataKeyNames="AnswerID, Answer(s), Answered By">
                    <Columns>
                        <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Edit" HeaderText="Edit" Text="Edit" UniqueName="Edit" />
                        <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" Text="Delete" HeaderText="Delete" ConfirmText="Are you sure you want to delete the selected row?" />
                        <telerik:GridBoundColumn DataField="AnswerID" FilterControlAltText="Filter AnswerID column" HeaderText="AnswerID" SortExpression="AnswerID" ReadOnly="true" Visible="false" UniqueName="AnswerID" />
                        <telerik:GridBoundColumn DataField="Answer(s)" FilterControlAltText="Filter Answer(s) column" HeaderText="Answer(s)" SortExpression="Answer(s)" UniqueName="Answer(s)" />
                        <telerik:GridBoundColumn DataField="Answered By" FilterControlAltText="Filter Answered By column" HeaderText="Answered By" SortExpression="AnsweredBy" UniqueName="AnsweredBy" />
           </Columns>
     </MasterTableView>
     <HeaderStyle BorderStyle="Outset" Font-Bold="True" HorizontalAlign="Left" />
</telerik:RadGrid>

0 个答案:

没有答案