曾俊的博客

Where there is a will , there is a way .有志者,事竟成...

原创 GridView行选择事件,单击GridView中某一行任意位置提出这条记录收藏

新一篇: Javascript实现日期的连动选择 | 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DataViewSelectRow : System.Web.UI.Page
{
   
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            lblStr.Text 
= "select  * from Products";
            DataSet ds 
= BindSqlServer(lblStr.Text);   //
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

    }

     
    
private DataSet BindSqlServer(string str)
    
{
        SqlConnection Conn 
= new SqlConnection("server=.;uid =sa ; pwd=;database = northwind");

        DataSet ds 
= new DataSet();
        SqlDataAdapter da 
= new SqlDataAdapter(str, Conn);

        da.Fill(ds);
        
return ds;
    }


    
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;

        DataSet ds 
= BindSqlServer("select  * from Products");
        GridView1.DataSource 
= ds;
        GridView1.DataBind();
    }

    
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    
{
        
//注册行单击事件
        e.Row.Attributes.Add("onclick""javascirpt:__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");
    }


    
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        Response.Write(GridView1.SelectedValue);  
//显示单示列的ID
        
//得到ID后将其付给一个变量

        lblStr.Text 
= "select * from products where Productid =" + GridView1.SelectedValue;
        DataSet ds 
= new DataSet();
        ds 
= BindSqlServer(lblStr.Text);

        
this.TextBox1.Text = ds.Tables[0].Rows[0]["QuantityPerUnit"].ToString(); 
    }

}
 

发表于 @ 2007年09月27日 09:41:00|评论(loading...)|编辑

新一篇: Javascript实现日期的连动选择 | 

评论

#xieyours 发表于2008-05-23 22:54:56  IP: 59.57.10.*
你的_doPostBack() 方法呢?可以贴出来吗
#zengjun24 发表于2008-05-25 11:57:10  IP: 221.221.152.*
__doPostBack()方法是Javascript 的一个函数。用来触发页面的PostBack事件
发表评论  


登录
Csdn Blog version 3.1a
Copyright © じ☆ve弑愛う