【问题标题】:Change text in specific row in gridview using checkbox使用复选框更改gridview中特定行中的文本
【发布时间】:2015-09-28 17:40:50
【问题描述】:

我想更改网格视图中的特定文本,下面是一张图片:

例如,如果我单击复选框按钮,特定行的“状态”文本将更改为“已验证”。

这是我背后的aspx代码:

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using MSSQLConnector;

    namespace SoftwareAnalysisAndDesign.SAD
    {
        public partial class TeacherPage : System.Web.UI.Page
        {
            private MSConnector connector = new MSConnector();
            private DataSet SubjectlistData;
            private DataTable SubjectlistTable;
            string query = null;
            protected void Page_Load(object sender, EventArgs e)
            {

                 if (!IsPostBack)
                 {
                     //Populate The Select Tag with Subjects
                     SubjectList.DataSource = Session["TeacherSubjectList"];
                     SubjectList.DataTextField = "CourseNo";
                     SubjectList.DataValueField = "CourseNo";
                     SubjectList.DataBind();
                 }
        }
    }     
   protected void TeacherSubjects_Click(object sender, EventArgs e)
    {
        string getText = SubjectList.SelectedItem.Text;
        //Connection String
        connector.ConnectionString = "Data Source=keith;Initial Catalog=SAD;Integrated Security=True";

        query = "select StudentID,CourseNo,CourseDescription,Units,Day,StartTime,EndTime,Room,Instructor,Amount,Status from assessmentform where CourseNo = '" + getText + "'";

        SubjectlistData = connector.ExecuteQuery(query);
        SubjectlistTable = SubjectlistData.Tables[0];

        //Add a colum check row
        SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));

        Session["ValidateSubject"] = SubjectlistTable;

        Response.Redirect("ValidateSubjectTeacher.aspx");
    }

我使用这个在我的行中添加一个复选框到我的gridview:

        //Add a colum check row
        SubjectlistTable.Columns.Add("Check", Type.GetType("System.Boolean"));

        Session["ValidateSubject"] = SubjectlistTable;

我使用会话将我的 gridview 传递到另一个页面, ValidatePage 后面的 aspx 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SoftwareAnalysisAndDesign.SAD
{
    public partial class ValidateSubjectTeacher : System.Web.UI.Page
    {
        CheckBox check = new CheckBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ValidateSubject"] == null)
            {
                Response.Redirect("TeacherPage.aspx", true);
            }

            if (!IsPostBack)
            {
                ValidateSubject.DataSource = Session["ValidateSubject"];
                ValidateSubject.DataBind();
            }

            //I add my checkbox in the last row using this
            foreach (GridViewRow row in ValidateSubject.Rows)
            {
                check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox;
                check.Enabled = true;
            }
        }


        protected void ValidateSubject_Click(object sender, EventArgs e)
        {
            if(check.Checked)
            {
                //This condition here is I want to change the Status when check to validated.
            }
        }
    }

在我使用会话后面的代码中,我将所有数据绑定在 DataTable(FirstPage);

DataSet ds = connector.ExecuteQuery(query);
DataTable dt = dt.Table[0];
dt.DataBind();
Session["SubjectList"] = dt;

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您可以像这样使用NamingContainer 属性:-

    protected void ValidateSubject_Click(object sender, EventArgs e)
    {
        CheckBox chk= (CheckBox)sender;
        GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give you row
        grvRow.Cells[10].Text = "Validated"; //Updated the cell text in that row.
    
        //Or if Status is a control like label then //
        Label StatusLabel = (Label)grvRow.FindControl("StatusLabel");
        StatusLabel.Text = "Validated";
    }
    

    或者,您也可以使用RowDataBound 事件。

    更新:

    由于您直接绑定网格,即 AutoGenerateColumns 设置为 true,因此您必须以编程方式附加复选框的事件处理程序,如下所示:-

    //I add my checkbox in the last row using this
    foreach (GridViewRow row in ValidateSubject.Rows)
    {
        check = row.Cells[row.Cells.Count - 1].Controls[0] as CheckBox;
        check.Enabled = true;
        check.CheckedChanged += ValidateSubject_Click;  //Bind the event
        check.AutoPostBack = true;  //Set the AutoPostBack property to true
    }
    

    现在,在您的事件中,首先在复选框的帮助下找到该行,然后像这样更新Status 列:-

    protected void ValidateSubject_Click(object sender, EventArgs e)
    {
        CheckBox chk= (CheckBox)sender;  
        GridViewRow grvRow = (GridViewRow)chk.NamingContainer;//This will give you row 
        if(chk.Checked)
        {
            grvRow.Cells[10].Text = "Validated";
        }
    }
    

    【讨论】:

    • grvRow.Cells[n].Text中的[n]是什么?
    • @laurencekeithalbano - n 是您的 Status 列的单元格位置。但是如果你已经将Status 定义为一个类似标签的控件,最好找到它使用单元格索引。
    • 如果我想用这个先生改变我的gridview列,我该怎么做?
    • @laurencekeithalbano - 嘿,我现在知道了,您正在直接绑定网格,因此您还必须动态绑定事件。检查我的更新,它应该可以工作。
    • 感谢@Rahl Singh 先生,这是我一直在寻找的,现在我需要在单击“验证”按钮时更新数据库中的查询,非常感谢! :)
    【解决方案2】:

    您可以使用这样的扩展方法:

    public static class myExtensionsMethods
    {
        public static void validateRow(this object sender)
        {
            int columnIndex = 10;
            GridViewRow myRow = ((Control)sender).Parent as GridViewRow;
            myRow.Cells[columnIndex].Text = "Validated";
        }
    }
    

    扩展方法将允许您以这种方式调用它

    protected void ValidateSubject_Click(object sender, EventArgs e)
    {
        if (check.Checked)
        {
            sender.validateRow();
        }
    }
    

    【讨论】:

    • 代码 sender.validateRow() 得到一个错误'object'不包含'validateRow'的定义并且没有扩展方法'validateRow'
    • 您必须在类中添加方法才能使其工作。发布更新代码
    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    相关资源
    最近更新 更多