DataGrid中超级链接列之使用技巧

现在网站上用超级链接列的地方非常多,比如,一个新闻标题,你要先看它的详细信息,一个学生名字,点击它,然后看他 / 她的详细信息,它可以这样做,要么转到另外一个页面去,也可以用 javascript 弹出一个小页面,我觉得这样看起来会比较舒服点,因为,不用每次都转到另外一个页面去,在这里我把两种效果都做出来了, 供大家和自己以后参考。(这里我使用 Northwind 中的 Employees 这个表的数据,数据控件是 DataGrid Repeater

效果一:转到另外一个页面去

 

第一个页面前台代码:

                                          DataTextField="FirstName" HeaderText="姓氏">

 

第一个页面后台代码:

private void Page_Load(object sender, System.EventArgs e)

              {

                     // 在此处放置用户代码以初始化页面

                     SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");

                     con.Open();

                     SqlCommand cmd=new SqlCommand("select * from Employees",con);

                     SqlDataReader sdr=cmd.ExecuteReader();

                     this.DataGrid1.DataSource=sdr;

                     this.DataBind();

                     con.Close();

                     sdr.Close();

                    

                    

              }

第二个页面的前台代码:

                                                       

                                                              

                                                                     

                                                                            

                                                                            

                                                                     

                                                                     

                                                                            

                                                                     

                                                                     

                                                                            

                                                                            

                                                                     

                                                              

员工姓氏:<%#DataBinder.Eval(Container.DataItem,"FirstName")%> 员工名字:<%#DataBinder.Eval(Container.DataItem,"LastName")%>
员工生日:<%#DataBinder.Eval(Container.DataItem,"BirthDate")%>
称谓:<%#DataBinder.Eval(Container.DataItem,"TitleOfCourtesy")%> 籍贯:<%#DataBinder.Eval(Container.DataItem,"City")%>

                                                       

                                                

第二页面的后台代码:

private void Page_Load(object sender, System.EventArgs e)

              {

                     // 在此处放置用户代码以初始化页面

               string ID=     Request.QueryString["id"].ToString();

                     SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");

                     con.Open();

                     SqlCommand cmd=new SqlCommand("select * from Employees where EmployeeID='"+ ID+"'",con);

                     SqlDataReader sdr=cmd.ExecuteReader();

                     this.Repeater1.DataSource=sdr;

                     this.DataBind();

                     con.Close();

                     sdr.Close();

 

              }

效果二:使用javascript弹出小页面

 

第一个页面前台:

新的页面','directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,height=400,width=500')"

                                          DataTextField="LastName" HeaderText="名字">

其余的都和第一种做法一样的。

 

 

 

你可能感兴趣的:(DataGrid控件使用技巧)