gridview 的使用过程中,某一行显示的内容有限,有时候需要显示更为全面的内容,就需要根据选中行的主键,将全部信息进行单独页面的显示。
一、页面拖入gridview控件,进行设置
为了显示数据库的数据,做了对gridview的数据绑定设置。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="z-index: 100; left: 78px; position: absolute;
top: 38px; color:Red; font-size:10pt;" Width="430px" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="ID" HeaderText="序号" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30px" ItemStyle-ForeColor="blue" />
<asp:BoundField DataField="name" HeaderText="姓名" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px" ItemStyle- ForeColor="blue"/>
<asp:BoundField DataField="url" HeaderText="图片地址" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="300px" ItemStyle- ForeColor="blue" />
</Columns>
</asp:GridView>
二、修改页面头部
加入, EnableEventValidation="false"
EnableEventValidation 的使用说明:(具体为什么要这么设置的原因现在我还没有弄明白)
获取或设置一个值,该值指示页面验证回发事件,还是验证回调事件。
如果页面验证事件,则为 true;否则为 false。默认值为 true。
此事件验证机制可降低未经授权的回发请求和回调带来的风险。当 EnableEventValidation 属性设置为 true 时,ASP.NET 仅允许在回发请求或回调期间可由控件引发的事件。
说明
此事件验证机制可消除未经授权的回发请求和回调带来的风险。通过此模型,控件可在呈现期间注册其事件,然后在回发或回调期间验证这些事件。默认情况下,ASP.NET 中的所有事件驱动控件均使用此功能。
强烈建议不要禁用事件验证。如果确实需要禁用事件验证,请确保不会构造出对应用程序产生意料之外影响的回发。
在大多数情况下,请通过在 Web.config 文件中设置 @ Page 指令的 enabledEventValidation 特性或页元素的 enableEventValidation 特性来设置 EnableEventValidation 属性。不能在代码中设置 EnableEventValidation 属性。
三、后台程序,绑定数据,打开页面
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//进行数据的绑定操作
string strcon = @"Data Source=www-4a3e4793e05\SQLEXPRESS;Initial Catalog=shiyanku;Integrated Security=True";
SqlConnection conn = new SqlConnection(strcon);//链接数据库
conn.Open();
//读取信息,进行显示
string sqlstr = "select * from sctp";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
da.Dispose();
ds.Clear();
ds.Dispose();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("style", "white-space:nowrap");
e.Row.Attributes.Add("style", "height:30px");
//排第一列的序号
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
//修改鼠标经过是行背景的颜色
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标经过时
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
//鼠标移出时
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
//e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
//e.Row.Height = Unit.Pixel(43);
//e.Row.Attributes.Add("style", "height:43px");
//进行涉及事件触发,关键是要把点击的哪一行传递出去
String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "Select$" + e.Row.RowIndex.ToString());
e.Row.Attributes.Add("onclick", evt); //点击后,通过这个evt 来触发 SelectedIndexChanged事件或SelectedIndexChanging事件
//e.Row.Attributes.Add("onclick","javascript:_doPostBack('"+this.UniqueID+"','Select$"+e.Row.RowIndex+"');");
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Session["url"] = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Response.Write("<script >window.open('2.aspx','单位上报详情','height=400,width=800,top=200,left=300,status=no,help=no,resizable=yes,scrollbars=yes')</script>");
//Response.Write("<script >window.open('detail/123.aspx','单位上报详情','height=400,width=800,top=200,left=300,status=no,help=no,resizable=yes,scrollbars=yes')</script>");
//Response.Write("<script >window.open('detail/XXYwwDetail.aspx','被分局、市局采用的维稳信息','height=400,width=800,top=200,left=300,status=no,help=no,resizable=yes,scrollbars=yes')</script>");
//Response.Write("<script >window.open('Default.aspx','单位上报详情','height=400,width=800,top=200,left=300,status=no,help=no,resizable=yes,scrollbars=yes')</script>");
}
四、打开的新的页面,内容自己填写,接收传递过来的信息
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["url"].ToString();
}