65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ASP.NET 2.0 中选择 GridView 控件中的一行

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.50/5 (2投票s)

2007 年 8 月 1 日

CPOL
viewsIcon

30091

downloadIcon

208

在 ASP.NET 2.0 的 GridView 控件中选择/取消选择行的一种简单方法。

引言

这是一篇关于如何在 ASP.NET 2.0 中使用 XML 作为数据源选择 GridView 控件中的特定行 的简单文章。

使用代码

这段代码的作用是什么?它只是将 XML 加载到 GridView 控件中,然后当用户选中某个复选框时,文本框和按钮将被启用。可下载版本中包含 XML 文件 NewsData.xml

SelectingRow.aspx.csPage_Load 事件中,添加以下代码以从 NewsData.xml 文件加载 GridView。

DataSet ds = new DataSet(); //global dataset
int i = 0; //global variable 
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // for loading the values from xml to dataset
        ds.ReadXml(Server.MapPath("~\\NewsData.xml"));

        GridView1.DataSource = createDataSource(ds);
        GridView1.DataBind();
    }
}

createDataSource 创建 GridView 的数据源结构,并将该结构作为集合对象返回。

protected ICollection createDataSource(DataSet dsNew)
{
    DataTable dt = new DataTable();
    DataView dv;
    DataColumnCollection dc = dt.Columns;

    foreach (DataColumn dataColumn in dsNew.Tables[0].Columns)
    {
        dc.Add(dataColumn.ColumnName, typeof(System.String));
    }

    for (int i = 0; i <= dsNew.Tables[0].Rows.Count - 1; i++)
    {
        DataRow dr = dt.NewRow();
        foreach (DataColumn dColumn in dsNew.Tables[0].Columns)
        {
            dr[dColumn.ColumnName] = "";
        }
        dt.Rows.Add(dr);
    }
    dv = new DataView(dt);
    return dv;
}

chkEdit_CheckedChanged 用于获取当前行并启用/禁用该行的控件。

protected void chkEdit_CheckedChanged(object sender, EventArgs e)
{
    CheckBox checkEdit;
    TextBox textHeading;
    Button buttonDelete;
    GridViewRowCollection gvRowColl = GridView1.Rows;

    foreach (GridViewRow dr in gvRowColl)
    {
        /*second time when a check box is checked, then the below code clears the last- 
          selected row to null (which is in the session object) 
          and disables the controls of that row*/
        if (Session["SelectedRow"] != null)
        {
            int row = int.Parse(Session["SelectedRow"].ToString());

            //for finding the controls in the grid and typecasting it to the type of the -
            //the control in the itemtemplate 
            checkEdit = (CheckBox)gvRowColl[row].Cells[0].FindControl("chkEdit");
            textHeading = (TextBox)gvRowColl[row].Cells[1].FindControl("txtHeading");
            buttonDelete = (Button)gvRowColl[row].Cells[2].FindControl("btnDelete");

                //disabling the controls and unchecking the checkbox
                textHeading.Enabled = false;
                textHeading.ReadOnly = true;
                buttonDelete.Enabled = false;
                checkEdit.Checked = false;

                Session["SelectedRow"] = null;
            }
            checkEdit = (CheckBox)dr.Cells[0].FindControl("chkEdit");
            if (checkEdit.Checked)
            {
                //this session object stores the current 
                //selected row( current checked checkbox)
                Session["SelectedRow"] = dr.DataItemIndex;

                //for finding the controls in the grid and typecasting it to the type of the -
                //the control in the itemtemplate  
                textHeading = (TextBox)dr.Cells[1].FindControl("txtHeading");
                buttonDelete = (Button)dr.Cells[2].FindControl("btnDelete");

                //enabling the controls in the current checked row
                textHeading.Enabled = true;
                textHeading.ReadOnly = false;
                buttonDelete.Enabled = true;

                break;
            }
        }
    }

GridView1_RowDataBound 方法由 GridView.databind() 调用。 子控件与父控件的绑定就在这里发生。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    CheckBox checkEdit;
    TextBox textHeading;
    Button buttonDelete;

    checkEdit = (CheckBox)e.Row.FindControl("chkEdit");
    textHeading = (TextBox)e.Row.FindControl("txtHeading");
    buttonDelete = (Button)e.Row.FindControl("btnDelete");

    if (e.Row.RowIndex != -1)//if not header then
    {
        textHeading.Text = ds.Tables[0].Rows[i][0].ToString();
        
        //this condition doesnot stisfy on the very first
        //page load ie during page postback to itself.
        //this condition satisfies only during subsequent 
        //postbacks ie on clicking the button.
        if (Session["SelectedRow"] != null)
        {
            if (e.Row.RowIndex == 
                int.Parse(Session["SelectedRow"].ToString()))
            {
                checkEdit.Checked = true;
                textHeading.Enabled = true;
                textHeading.ReadOnly = false;
                buttonDelete.Enabled = true;
            }
        }
        i += 1;
    }
}

只需下载此简单实现的可用版本(SelectRow.aspxNewsData.xml),并将其添加到新的 Web 项目中即可!

推荐的文章,介绍以不同的方式实现此功能

历史

  • 版本 1.0。
© . All rights reserved.