using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class SelectOne : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["selectedBest"] = "ASP.Net";
bind();
}
}
protected void bind()
{
DataTable dt = new DataTable();
dt.Columns.Add("Include", typeof(Boolean));
dt.Columns.Add("Name", typeof(String));
dt.Rows.Add(new Object[] { false, "C#" });
dt.Rows.Add(new Object[] { false, "ASP.Net" });
dt.Rows.Add(new Object[] { false, "SQL Server" });
dt.Rows.Add(new Object[] { false, "VB.Net" });
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
// See if this matches our saved selection.
if (e.Row.Cells[1].Text.Equals(ViewState["selectedBest"].ToString()))
e.Row.Cells[2].Text = e.Row.Cells[2].Text.Replace("value", "checked value");
// Set the value to the RowIndex so it is unique.
e.Row.Cells[2].Text = e.Row.Cells[2].Text.Replace("{0}", e.Row.RowIndex.ToString());
}
protected void btnSave_Click(object sender, EventArgs e)
{
int total = 0;
//Count of the number that our checked.
foreach (GridViewRow gvr in this.GridView1.Rows)
{
if (gvr.RowType != DataControlRowType.DataRow)
continue;
if (((CheckBox)gvr.Cells[0].FindControl("cbSelected")).Checked)
total++;
}
//Save the selected Best.
ViewState["selectedBest"] =
this.GridView1.Rows[Convert.ToInt32(Request.Form["RBBest"])].Cells[1].Text;
//Normally you would be saving to the database here.
this.lblMessage.Text = "You selected " + total.ToString()
+ " with " + ViewState["selectedBest"].ToString() + " as the best.";
bind();
}
}