2017-05-27 17 views
-4

我已经想出了如何创建下拉框,将它们链接到它们各自的数据源,以及创建链接到存储过程的数据源。我不知道如何将按钮链接到数据源,以便在单击时运行存储过程。我与2017年VS如何将按钮单击链接到数据源以运行存储过程

工作的HTML标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChangeShiftStat.aspx.cs" Inherits="Default2" %> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

    &nbsp; Shift&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="ShiftDataSource" DataTextField="Shift" DataValueField="Shift"> 
     </asp:DropDownList> 
     <br /> 
     <br /> 
&nbsp; New Status&nbsp;&nbsp;&nbsp; 
     <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="StatusDataSource" DataTextField="Status" DataValueField="Status"> 
     </asp:DropDownList> 
     <br /> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <br /> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:Button ID="Button1" runat="server" Text="Change Status" /> 
     <asp:SqlDataSource ID="ShiftDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:TheBoardConnectionString %>" SelectCommand="SELECT * FROM [Shifts]"></asp:SqlDataSource> 
     <asp:SqlDataSource ID="StatusDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:TheBoardConnectionString %>" SelectCommand="SELECT * FROM [Statuses]"></asp:SqlDataSource> 
     <asp:SqlDataSource ID="RunSP" runat="server" ConnectionString="<%$ ConnectionStrings:TheBoardConnectionString %>" SelectCommand="changeshiftstat" SelectCommandType="StoredProcedure"> 
      <SelectParameters> 
       <asp:ControlParameter ControlID="DropDownList1" Name="Shift" PropertyName="SelectedValue" Type="String" /> 
       <asp:ControlParameter ControlID="DropDownList2" Name="NewStat" PropertyName="SelectedValue" Type="String" /> 
      </SelectParameters> 
     </asp:SqlDataSource> 
    </div> 
    </form> 
</body> 
</html> 

C#代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Default2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 
} 

回答

0

您需要通过按钮的点击事件提交sql command。将CommandText设置为存储过程的名称,并将commandType设置为commandType.StoredProcedure。根据您想要执行的操作,您可以按照this打开阅读器,也可以按照this教程执行非查询命令。

+0

感谢您的帮助。这些例子讨论了取回结果。我真的不需要那个。你能指出我在哪里放置代码的方向吗? –

+0

有关如何在sql中执行非查询的示例,请参阅https://msdn.microsoft.com/en-us/library/80x06z3b.aspx。如果你没有任何参数,那就跳过这些行。 –

+0

https://msdn.microsoft.com/en-us/library/zwwsdtbk(v=vs.80).aspx < - 本教程将向您展示如何将事件处理程序添加到您的程序中。你会想要一个按钮点击事件。 –

相关问题