Net Core教程

asp.net代码练习 work035 DetailsView的使用

本文主要是介绍asp.net代码练习 work035 DetailsView的使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

webform2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="work035.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:HyperLinkField DataNavigateUrlFields="user_id" DataNavigateUrlFormatString="WebForm1.aspx?id={0}" DataTextField="real_name" HeaderText="查看"></asp:HyperLinkField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

webform2.aspx.cs

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="work035.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:HyperLinkField DataNavigateUrlFields="user_id" DataNavigateUrlFormatString="WebForm1.aspx?id={0}" DataTextField="real_name" HeaderText="查看"></asp:HyperLinkField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work035.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>DetailsView的示例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DetailsView ID="DetailsView1" runat="server" Width="400px" AutoGenerateRows="False" OnDataBound="DetailsView1_DataBound">
            <Fields>
                <asp:BoundField DataField="user_id" HeaderText="user_id"/>
                <asp:BoundField DataField="user_name" HeaderText="user_name" />
                <asp:BoundField DataField="real_name" HeaderText="real_name" />
                <asp:BoundField DataField="age" HeaderText="age" />
                <asp:CheckBoxField DataField="sex" HeaderText="sex" />
                <asp:BoundField DataField="mobile" HeaderText="mobile" />
                <asp:BoundField DataField="email" HeaderText="email" />
                <asp:TemplateField HeaderText="性别">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlSex" runat="server">
                            <asp:ListItem Value="True">男</asp:ListItem>
                            <asp:ListItem Value="False">女</asp:ListItem>
                        </asp:DropDownList>
                        <asp:Label ID="lbSex" runat="server" Text='<%# Eval("sex") %>' Visible="false"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Fields>
            <EmptyDataTemplate>
                提示:没有任何记录
            </EmptyDataTemplate>
        </asp:DetailsView>
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace work035
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int userID;

                if (int.TryParse(Request.QueryString["id"], out userID))
                {
                    System.Data.SqlClient.SqlConnectionStringBuilder bu = new System.Data.SqlClient.SqlConnectionStringBuilder();
                    bu.DataSource = "(local)";
                    bu.InitialCatalog = "test";
                    bu.UserID = "sa";
                    bu.Password = "123456";

                    string conString = bu.ConnectionString;
                    System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conString);

                    string sql = "select * from userinfo where user_id=@id";
                    System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(sql, con);
                    adapter.SelectCommand.Parameters.AddWithValue("@id", userID);
                    System.Data.DataTable dt = new System.Data.DataTable();
                    adapter.Fill(dt);

                    DetailsView1.DataSource = dt;
                    DetailsView1.DataBind();

                }
                else
                {
                    System.Data.DataTable dt = new System.Data.DataTable();
                    DetailsView1.DataSource = dt;
                    DetailsView1.DataBind();
                }
            }
        }

        protected void DetailsView1_DataBound(object sender, EventArgs e)
        {
            DropDownList ddlSex = (DropDownList)DetailsView1.Rows[7].FindControl("ddlSex");
            Label lbSex = (Label)DetailsView1.Rows[7].FindControl("lbSex");
            ListItem item = ddlSex.Items.FindByValue(lbSex.Text);
            if (item != null)
            {
                item.Selected = true;
            }
        }
    }
}

sql

create database test;

create table UserInfo(
	user_id int identity(1,1) not null,
	user_name varchar(20) not null unique,
	real_name nvarchar(8) not null,
	age tinyint not null,
	sex bit not null,
	mobile varchar(14),
	phone varchar(14),
	email varchar(50) not null,
	primary key(user_id) 
);

insert into UserInfo values('zhangfei','张飞',36,1,'13011110001','8845996','zf@qq.com');
insert into UserInfo values('guanyu','关羽',38,1,'13011110002','8845995','gy@qq.com');
insert into UserInfo values('liubei','刘备',42,1,'13011110003','8845994','lb@qq.com');
insert into UserInfo values('zhaoyun','赵云',32,1,'13011110004','8845993','zy@qq.com');
insert into UserInfo values('huangzhong','黄忠',50,1,'13011110005','8845992','hz@qq.com');
insert into UserInfo values('caocao','曹操',48,1,'13011110006','8845991','cc@qq.com');
insert into UserInfo values('sunquan','孙权',33,1,'13011110007','8845990','sq@qq.com');
insert into UserInfo values('diaochan','貂蝉',20,0,'13011110008','8845880','dc@qq.com');
insert into UserInfo values('daqiao','大乔',21,0,'13011110009','8845881','dq@qq.com');
insert into UserInfo values('xiaoqiao','小乔',20,0,'13011110010','8845882','xq@qq.com');

 

这篇关于asp.net代码练习 work035 DetailsView的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!