使用模版列完成课上例子的实现,用一列显示 全部信息,同时完成修改的功能

来源:互联网 发布:阿里云自己搭建hadoop 编辑:程序博客网 时间:2024/05/29 10:07

1 使用自定义列显示班级学生信息,要求能够进 行删除和修改,删除时给出提示:如 “ 确信要删 除 - 张三 - 吗? ” ,其中张三为当前行的姓名

前台:

<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
InsertCommand="INSERT INTO [student] ([sname], [classid], [sex], [age], [isking], [photo]) VALUES (@sname, @classid, @sex, @age, @isking, @photo)"
SelectCommand="SELECT [sid], [sname], [classid], [sex], [age], [isking], [photo] FROM [student]"
UpdateCommand="UPDATE [student] SET [sname] = @sname, [classid] = @classid, [sex] = @sex, [age] = @age, [isking] = @isking, [photo] = @photo WHERE [sid] = @sid">
<DeleteParameters>
<asp:Parameter Name="sid" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
<asp:Parameter Name="sid" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan"
BorderWidth="1px" CellPadding="2" DataKeyNames="sid" ForeColor="Black"
GridLines="None" Height="202px" Xonrowcommand="GridView1_RowCommand"
Xonrowdatabound="GridView1_RowDataBound" style="margin-right: 0px" Width="602px">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:BoundField DataField="sid" HeaderText="编号" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="姓名" SortExpression="sname" />
<asp:BoundField DataField="classid" HeaderText="班级" SortExpression="classid" />
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
<asp:CheckBoxField DataField="isking" HeaderText="班长" SortExpression="isking" />
<asp:ImageField DataImageUrlField="photo" DataImageUrlFormatString="{0}"
HeaderText="照片">
</asp:ImageField>
<asp:ButtonField CausesValidation="True" CommandName="delete" HeaderText="删除"
Text="删除" />
</div>
</form>
</body>

后台:

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

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
string id = e.Row.Cells[0].Text;
string name = e.Row.Cells[1].Text;
LinkButton btn1 = e.Row.Cells[7].Controls[0] as LinkButton;
if (btn1 != null)
{
btn1.Attributes.Add("onclick","return confirm('确信要删除编号为:"+id+"的"+name+"吗?')");
}

}
}

2 使用模版列完成课上例子的实现,用一列显示 全部信息,同时完成修改的功能

前台:

<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="sid" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None" Xonrowcommand="GridView1_RowCommand"
Xonrowdatabound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="sid" HeaderText="学号" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:HyperLinkField DataNavigateUrlFields="photo" DataTextField="sname"
DataTextFormatString="点击查看{0}" HeaderText="姓名"
NavigateUrl="~/GridView/img.aspx" Target="_blank" />
<asp:BoundField DataField="classid" HeaderText="班号" SortExpression="classid" />
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />


<asp:CheckBoxField DataField="isking" HeaderText="班长" SortExpression="isking" />
<asp:TemplateField HeaderText="照片">
<ItemTemplate>
<table style="width:100%;">
<tr>
<td>
<a href='img.aspx?photourl=<%#Eval("photo") %> ' target="_blank">姓名:<%#Eval("sname") %></a></td>
</tr>
<tr>
<td>
<asp:Image ID="Image2" runat="server" imageurl='<%#Eval("photo") %>' Width="60px" Height="60px" ToolTip='<%#"这是:"+Eval("sname") %>'/>
</td>
</tr>
<tr>
<td>
编号:<%#Eval("sid") %>&nbsp;&nbsp;&nbsp;
年龄:<%#Eval("age") %></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField HeaderText="删除" Text="删除" />
<asp:ButtonField CommandName="initialPassword" Text="初始化密码" />
<asp:CommandField HeaderText="选项" ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<EmptyDataTemplate>
<table style="width:100%;">
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
</table>
</EmptyDataTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

</div>
</form>

img 的前台:

<div>

<asp:Image ID="Image1" runat="server"
ImageUrl="" />

</div>

后台

public partial class img : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Image1.ImageUrl = Request["photourl"];
}
}

原创粉丝点击