日期:2014-05-18 浏览次数:20415 次
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) BindGrid(); } private void BindGrid() { GridView1.DataSource = CreateData(); GridView1.DataBind(); } private DataTable CreateData() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("User name", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Chinese name", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("English name", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("E-mail", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Sex", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Telephone", System.Type.GetType("System.String"))); dt.Columns.Add(new DataColumn("Comments", System.Type.GetType("System.String"))); for (int i = 0; i < 30; i++) { DataRow dr = dt.NewRow(); dr["User name"] = "Msotec" + i.ToString(); dr["E-mail"] = "zcyhappy@msn.com"; dr["Chinese name"] = "微软技术论坛"; dr["English name"] = "Msotec"; dr["Telephone"] = "13864732837"; dr["Comments"] = "www.msotec.com"; dr["Sex"] = "1"; dt.Rows.Add(dr); } return dt; } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; BindGrid(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dt = CreateData(); (e.Row.FindControl("rdoButtonSex") as RadioButtonList).Items.FindByValue(dt.Rows[e.Row.RowIndex]["Sex"].ToString()).Selected = true; } } protected void btnGet_Click(object sender, EventArgs e) { for (int i = 0; i < GridView1.Rows.Count; i++) { RadioButtonList rdoButton = new RadioButtonList(); rdoButton = (RadioButtonList)GridView1.Rows[i].Cells[6].FindControl("rdoButtonSex"); Response.Write("行号:" + i.ToString() + "RadioButtonList的值:" + rdoButton.SelectedItem.Text + "<br />"); } }