GridView 自定义列,删除时付出提示:如 “ 确信要删 除 - 张三 - 吗? ” ,其中张三为当前行的姓名
GridView 自定义列,删除时给出提示:如 “ 确信要删 除 - 张三 - 吗? ” ,其中张三为当前行的姓名
前台:
<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 * 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>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="sid"
DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="sid" HeaderText="编号" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:HyperLinkField DataNavigateUrlFields="photo"
DataNavigateUrlFormatString="viewPhoto.aspx?photourl={0}" DataTextField="sname"
HeaderText="姓名" Target="_blank" />
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
<asp:CheckBoxField DataField="isking" HeaderText="班长" SortExpression="isking" />
<asp:TemplateField HeaderText="照片">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("photo") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<br />
<table style="width:100%;">
<tr><td><a href='viewPhoto.aspx?photourl=<%#Eval("photo") %>'>姓名:<%#Eval("sname") %></a> </td><td> </td><td> </td></tr>
<tr>
<td> </td>
<td> <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("photo") %>' Width="60px" Height="60px" ToolTip='<%#"这是:"+Eval("sname") %>' /> </td>
<td> </td>
</tr>
<tr>
<td>编号:<%#Eval("sid") %> </td>
<td>年龄:<%#Eval("age") %> </td>
<td> </td>
</tr>
</table>
</ItemTemplate>
<ControlStyle Height="60px" Width="60px" />
</asp:TemplateField>
<asp:ButtonField CommandName="delete" HeaderText="删除" Text="删除" />
<asp:ButtonField CommandName="initialPassword" HeaderText="初始化密码"
Text="初始化密码" />
</Columns>
<EmptyDataTemplate>
<table style="width:100%;">
<tr>
<td><a href='viewPhoto.aspx?photourl=<%#Eval("Eval") %>' target="_blank">姓名:<%#Eval("sname") %></a> </td>
</tr>
<tr>
<td>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("photo") %>' Width="60px" Height="60px" ToolTip='<%#"这是:"+Eval("sname") %>' />
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
后台:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "initialPassword")
{
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
string sid = e.Row.Cells[0].Text;
HyperLink link = e.Row.Cells[1].Controls[0] as HyperLink;
LinkButton lbtn = e.Row.Cells[6].Controls[0] as LinkButton;
if (lbtn != null)
{
lbtn.Attributes.Add("onclick", "return confirm('确定要删除编号为:"+sid+"的"+link.Text+"吗?')");
}
}
viewPhoto.aspx前台:
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
viewPhoto.aspx后台:
protected void Page_Load(object sender, EventArgs e)
{
this.Image1.ImageUrl = Request["photourl"];
}
前台:
<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 * 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>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="sid"
DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="sid" HeaderText="编号" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:HyperLinkField DataNavigateUrlFields="photo"
DataNavigateUrlFormatString="viewPhoto.aspx?photourl={0}" DataTextField="sname"
HeaderText="姓名" Target="_blank" />
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
<asp:CheckBoxField DataField="isking" HeaderText="班长" SortExpression="isking" />
<asp:TemplateField HeaderText="照片">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("photo") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<br />
<table style="width:100%;">
<tr><td><a href='viewPhoto.aspx?photourl=<%#Eval("photo") %>'>姓名:<%#Eval("sname") %></a> </td><td> </td><td> </td></tr>
<tr>
<td> </td>
<td> <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("photo") %>' Width="60px" Height="60px" ToolTip='<%#"这是:"+Eval("sname") %>' /> </td>
<td> </td>
</tr>
<tr>
<td>编号:<%#Eval("sid") %> </td>
<td>年龄:<%#Eval("age") %> </td>
<td> </td>
</tr>
</table>
</ItemTemplate>
<ControlStyle Height="60px" Width="60px" />
</asp:TemplateField>
<asp:ButtonField CommandName="delete" HeaderText="删除" Text="删除" />
<asp:ButtonField CommandName="initialPassword" HeaderText="初始化密码"
Text="初始化密码" />
</Columns>
<EmptyDataTemplate>
<table style="width:100%;">
<tr>
<td><a href='viewPhoto.aspx?photourl=<%#Eval("Eval") %>' target="_blank">姓名:<%#Eval("sname") %></a> </td>
</tr>
<tr>
<td>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("photo") %>' Width="60px" Height="60px" ToolTip='<%#"这是:"+Eval("sname") %>' />
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
后台:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "initialPassword")
{
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
string sid = e.Row.Cells[0].Text;
HyperLink link = e.Row.Cells[1].Controls[0] as HyperLink;
LinkButton lbtn = e.Row.Cells[6].Controls[0] as LinkButton;
if (lbtn != null)
{
lbtn.Attributes.Add("onclick", "return confirm('确定要删除编号为:"+sid+"的"+link.Text+"吗?')");
}
}
viewPhoto.aspx前台:
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
viewPhoto.aspx后台:
protected void Page_Load(object sender, EventArgs e)
{
this.Image1.ImageUrl = Request["photourl"];
}