绑定SqlDataSource实现增删改查(FormView为例)
2014-05-12 14:50
169 查看
页面拖入SqlDataSource,点击右上角三角号进行数据源配置,指定需要的数据库后选择指定的表和列,此时会自动生成查询SQL语句。再点击旁边的高级按钮,勾选生成INSERT、UPDATE、DELETE语句。VS会自动生成相应的SQL语句和参数。点击确定按钮完成DATASOURCE的配置。
拖入的FORMVIEW控件选择数据源为刚才已配置的SQLDATASOURCE。现在FORMVIEW控件已经可以完成增删改查功能。
如果要为FormView设定一个可以查询上一条和下一条记录的功能,需要改变VS自动生成的SelectCommand语句,加上WHERE查询条件并加上参数
<SelectParameters>
<asp:Parameter Name="ParameterName" Type="String" />
</SelectParameters>
在上下条数据按钮的点击事件中加上
SqlDataSource1.SelectParameters["ParameterName_id"].DefaultValue = "";//设定每次点击上下条数据按钮时WHERE查询条件的参数值。
此外,VS自动生成的ItemTemplate等各部门都可以自行修改。
比如将模板中的Course栏从自带的Textbox转为DropDownList
可以在EditItemTemplate和InsertItemTemplate中将原来的Course改为
<asp:DropDownList ID="DDL" runat="server" Width="153px">
<asp:ListItem></asp:ListItem>
<asp:ListItem>计算机</asp:ListItem>
<asp:ListItem>英语</asp:ListItem>
<asp:ListItem>语文</asp:ListItem>
</asp:DropDownList>
同时写上INSERT和UPDATE事件按钮 <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" Text=" 保 存 " OnClick="UpdateButton_Click"></asp:Button>
<asp:Button ID="InsertButton" runat="server" CausesValidation="True" Text=" 保 存 " OnClick="InsertButton_Click"></asp:Button>
在后台为Course参数赋值:
protected void UpdateButton_Click(object sender, EventArgs e)
{
SqlDataSource1.UpdateParameters["course"].DefaultValue = ((DropDownList)this.FormView1.FindControl("DDL")).SelectedValue;
this.FormView1.UpdateItem(true);
}
protected void InsertButton_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["course"].DefaultValue = ((DropDownList)this.FormView1.FindControl("DDL2")).SelectedValue;
this.FormView1.InsertItem(false);
}
拖入的FORMVIEW控件选择数据源为刚才已配置的SQLDATASOURCE。现在FORMVIEW控件已经可以完成增删改查功能。
如果要为FormView设定一个可以查询上一条和下一条记录的功能,需要改变VS自动生成的SelectCommand语句,加上WHERE查询条件并加上参数
<SelectParameters>
<asp:Parameter Name="ParameterName" Type="String" />
</SelectParameters>
在上下条数据按钮的点击事件中加上
SqlDataSource1.SelectParameters["ParameterName_id"].DefaultValue = "";//设定每次点击上下条数据按钮时WHERE查询条件的参数值。
此外,VS自动生成的ItemTemplate等各部门都可以自行修改。
比如将模板中的Course栏从自带的Textbox转为DropDownList
可以在EditItemTemplate和InsertItemTemplate中将原来的Course改为
<asp:DropDownList ID="DDL" runat="server" Width="153px">
<asp:ListItem></asp:ListItem>
<asp:ListItem>计算机</asp:ListItem>
<asp:ListItem>英语</asp:ListItem>
<asp:ListItem>语文</asp:ListItem>
</asp:DropDownList>
同时写上INSERT和UPDATE事件按钮 <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" Text=" 保 存 " OnClick="UpdateButton_Click"></asp:Button>
<asp:Button ID="InsertButton" runat="server" CausesValidation="True" Text=" 保 存 " OnClick="InsertButton_Click"></asp:Button>
在后台为Course参数赋值:
protected void UpdateButton_Click(object sender, EventArgs e)
{
SqlDataSource1.UpdateParameters["course"].DefaultValue = ((DropDownList)this.FormView1.FindControl("DDL")).SelectedValue;
this.FormView1.UpdateItem(true);
}
protected void InsertButton_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["course"].DefaultValue = ((DropDownList)this.FormView1.FindControl("DDL2")).SelectedValue;
this.FormView1.InsertItem(false);
}
相关文章推荐
- 点击DataGirdView的单元格实现增删改查。
- GridView绑定SqlDataSource以其自带功能实现增删查改等操作(不写后台代码)一
- GridView绑定SqlDataSource以其自带功能实现增删查改等操作 二
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- Asp.Net22.0中ObjectDataSource+Formview实现添加,修改
- SQL实现增删改查操作
- Mybatis(一)—实现对数据库的增删改查操作
- 使用DbUtils实现增删改查——ResultSetHandler 接口的实现类
- JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查
- SpringMVC+Spring Data JPA实现增删改查操作
- 初学JavaWeb-SSM框架实现增删改查——查
- RxBinding的使用,实现数据和View的绑定
- 笔记:堆的增删改查(数组实现)
- FormView中下拉框 (DropDownList) 的数据绑定
- winform 窗体实现增删改查(CRUD)共用模式
- Mysql的增删改查语句简单实现
- Asp.Net 2.0中ObjectDataSource+Formview实现添加,修改
- 实现GridView控件的多项功能:改变背景色、和FormView控件同步、隐藏指定列、添加删除确认、跳转到指定页。。[原创]