读书人

大哥们看下小弟我这个关于gridview里c

发布时间: 2012-02-28 13:06:34 作者: rapoo

大哥们看下我这个关于gridview里checkbox的问题
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default5.aspx.cs " Inherits= "Default5 " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
 
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Style= "left: 430px;
position: absolute; top: 424px " Text= "Button " />
<asp:GridView ID= "GridView1 " runat= "server " Style= "left: 205px; position: absolute;
top: 114px " AutoGenerateColumns= "False " Height= "237px " Width= "374px ">
<Columns>
<asp:BoundField DataField= "id " HeaderText= "序号 " />
<asp:BoundField DataField= "Fname " HeaderText= "用户名 " />
<asp:BoundField DataField= "password " HeaderText= "密码 " />
<asp:TemplateField> <ItemTemplate> <asp:CheckBox ID= "checkbox1 " runat=server /> </ItemTemplate> </asp:TemplateField>
</Columns>
</asp:GridView>
 

</div>
</form>
</body>
</html>


public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session[ "constr "] = WebConfigurationManager.ConnectionStrings[0].ConnectionString;
SqlDataSource mysource = new SqlDataSource((string)Session[ "constr "], "select * from test ");


GridView1.DataSource = mysource;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)

{
CheckBox ck = (CheckBox)GridView1.SelectedRow.Cells[0].Controls[0];
if (ck.Checked)
{
Response.Write(GridView1.SelectedRow.Cells[0].Text);
}
}

我这样哪里出错了,出现这样的错误.(未将对象引用设置到对象的实例。
)
我的目的是.我在gridview里checkbox选中任意行用button来输出我所选行的某个字段




[解决办法]
Response.Write(GridView1.SelectedRow.Cells[0].FindControl( 'checkbox1 ').value)

读书人网 >asp.net

热点推荐