当数据不是来自databaound控件时,如何使用数据源的Insert方法?
假设我有一个 2 个文本框、一个按钮和一个数据源。这就是我想要的。当我单击该按钮时,数据源从两个文本框中收集数据并将它们插入到数据库中。
下面是代码: 插入联系人应该怎么做?
<script runat="server">
protected void SaveButton_Click(object sender, EventArgs e)
{
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<!-- TextBoxe-->
First Name: <asp:TextBox ID="FirstNameTextBox" runat="server"/><br />
Last Name: <asp:TextBox ID="LastNameTextBox" runat="server"/><br />
<!-- Bouton -->
<asp:Button ID="SaveButton" runat="server" Text="Save"
onclick="SaveButton_Click" />
<!-- DataSource -->
<asp:SqlDataSource ID="ContactSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ContactConnectionString %>"
InsertCommand="INSERT INTO [Contacts] ([FirstName], [LastName]) VALUES FirstName, @LastName)"
SelectCommand="SELECT * FROM [Contacts]">
<InsertParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
Let's say, I have a 2 TextBox, a button, and a DataSource. This is what I want. When I click the buton, the DataSource collects data from both textbox and inserts them to the database.
Below is the code: What should I do the insert a a contact?
<script runat="server">
protected void SaveButton_Click(object sender, EventArgs e)
{
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<!-- TextBoxe-->
First Name: <asp:TextBox ID="FirstNameTextBox" runat="server"/><br />
Last Name: <asp:TextBox ID="LastNameTextBox" runat="server"/><br />
<!-- Bouton -->
<asp:Button ID="SaveButton" runat="server" Text="Save"
onclick="SaveButton_Click" />
<!-- DataSource -->
<asp:SqlDataSource ID="ContactSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ContactConnectionString %>"
InsertCommand="INSERT INTO [Contacts] ([FirstName], [LastName]) VALUES FirstName, @LastName)"
SelectCommand="SELECT * FROM [Contacts]">
<InsertParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是你可以这样做......
simply you can do this....