独自幸福快乐京巴1依依不舍孤独
在 2.0中,加入了许多新的功能和控件,相比 1.0/1.1,在各方面都有了很大的提高要加强业务学习。其中,在数据控件方面,增加了不少控件,其中的Gridview控件功能十分强大。在本文中,将探讨Gridview控件中的一些功能特性和用法,如果各位读者对Gridview控件不大了解,可以通过《使用 2.0中的Gridview控件》一文,来对Gridview控件有个初步的认识。
1、使用Gridview插入新记录
在Gridview控件中,可以实现插入新记录的操作(见《使用 2.0中的Gridview控件》)一文,但如果想实现在Gridview中,实现在Gridview控件的最后一行,提供一个空白行给用户输入要输入的记录,那无疑是很方便的。下面将介绍其实现方法。
首先反映出我国在社会主义法治国家建设过程中亟需解决的一些突出问题。,我们打算在让用户进行选择,当用户需要新增一记录时,便点击新增按钮,之后在Gridview的最后一行里,显示一个空白行,让用户按字段进行输入,如下图所示:
当用户决定不输入新空白记录时,可以按"cancel"按钮返回,该空白行消失。要实现这样的效果,我们可以充分利用Gridview的footer的模版功能进行自定义,因为有3列,所以,在每一列的footer模版中,定义如下:
<asp:Gridview ID="Gridview1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="CustomerIDLabel" Runat="Server"><%# Eval("CustomerID") %></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="CustomerIDTextBox" Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="CompanyNameLabel" Runat="Server"><%# Eval("CompanyName") %></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="CompanyNameTextBox" Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:DropDownList ID="ContactTitleDropDownList" Runat="server" DataSourceID="SqlDataSource2" DataTextField="ContactTitle" DataValueField="ContactTitle">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"
ConnectionString="server=localhost;uid=sa;password=xxx;database=northwind">
</asp:SqlDataSource>
<asp:Button ID="Button1" Runat="server" Text="Add" OnClick="Button1_Click" />
<asp:Button ID="CancelButton1" Runat="server" Text="Cancel" OnClick="CancelButton1_Click" />
</FooterTemplate>
<ItemTemplate>
<asp:DropDownList ID="ContactTitleDropDown" SelectedValue=’<%# Bind("ContactTitle") %>’ Runat="Server" DataSourceID="SqlDataSource3" DataTextField="ContactTitle" DataValueField="ContactTitle" ></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"
ConnectionString="server=localhost;uid=sa;password=xxxx;database=northwind" EnableCaching="True">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
以上为Gridview的代码,可以看到,在第一,二列的<foottemplate>列中,分别提供了customerid和companyname两个文本框以供用户输入,在第三列的<footertemplate>列中,以dropdownlistbox的形式来显示contracttitle.。其中,请注意第三列的footertemplate中的add和cancel两个按钮的,它们的事件代码如下
<script runat="server">
void CancelButton1_Click(object sender, EventArgs e)
{
owFooter = false;
}
void AddButton1_Click(object sender, EventArgs e)
{
owFooter = true;
}
//点add按钮时,将新增的记录更新到数据库中去
void Button1_Click(object sender, EventArgs e)
{
TextBox customerID = ndControl("CustomerIDTextBox") as TextBox;
TextBox companyName = ndControl("CompanyNameTextBox") as TextBox;
DropDownList ContactTitle = ndControl("ContactTitleDropDownList") as DropDownList;
sertParameters["CustomerID"].DefaultValue = xt;
sertParameters["CompanyName"].DefaultValue = xt;
sertParameters["ContactTitle"].DefaultValue=lectedValue;
sert();
}
</script>
其中的cancel按钮的事件,用来取消显示Gridview的footer模版,因此设置showfooter属性为false,而addbutton1按钮,是当用户决定新增记录时点选的,此时将设置showfooter属性为true,以显示各列的foottemplate,从而达到显示新的一个空白行的目的。
而在更新代码button1_click事件中,将首先使用ndcontrol的方法,将用户新增的各字段的值提取出来,然后分别赋值给sqldatasource的insertparameters集合(注意要一一对应),最后使用sqldatasource的insert方法,就可以成功向数据库增加一条新记录了。
另外,为了在窗体加载时,显示数据库northwind中customers表的数据,需要设置sqldatsource1的属性,如下代码:
<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactTitle]) VALUES (@CustomerID, @CompanyName, @ContactTitle)"
SelectCommand="SELECT top 5 [CustomerID], [CompanyName], [ContactTitle] FROM [Customers]"
ConnectionString="server=localhost;uid=sa;password=XXXXX;database=northwind">
<InsertParameters>
<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>
<asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>
<asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>
</InsertParameters>
</asp:SqlDataSource>
其中,必须设置insertcommand和selectcommand属性,设置数据提取和插入的语句,并且要设置好insertparameters集合中,各字段的类型和名称即可。
五官面部整形跌打损伤应该怎么处理
妇科千金片哪有卖
- 上一页:台湾推动机床零部件产值倍增计划
- 下一页:美版4100起一图看完各国新
-
把女朋友当女儿养的男生也比较聪明 女孩子本来就是你越宠爱 她越可爱的 你给她足够的安全感和保护 她也
把女朋友当女儿饲的男生也非常聪明 女孩子本来就是你越好宠爱 她越好可爱...
2023-11-26
-
《潜行者2:切尔诺贝利之心》自此开启预购!共三版本
《不速之客者2:车里雅宾斯克自始》现已重启预购!共三版 不速之客生存单...
2023-11-22
-
消息称特斯拉拟让上海工厂员工闭环工作到六月月份
据追问部分人所称,杜邦美国公司原先让其上海工厂的员工在保证了系统中工...
2023-11-21
-
立而今,碰到这菜我绝不错过,1次买20斤晒一晒,冬天焖肉太香了
白露后,碰到这菜我不应错过,1次买20斤,摊腊夏末蒸鱼肉太香了 大家好,...
2023-11-13
-
为什么有些炼油厂绝对不能去?原因就是这3点,说的越狠理越真!
很多人缘故进入社才会的时候, 可能因为没什么经验, 找将近一份福利待遇...
2023-11-09
-
39岁玄彬结婚后更有男人味!新整片穿衬衫秀肌肉,有抬头纹都帅气
近日,Facebook爆借助于了都由玄彬的最近品味整片,只好话说自从结了婚之后...
2023-11-08