如果它在TemplateField中,如何在回发后保持文本框状态?

时间:2018-03-20 09:19:49

标签: c# asp.net gridview

我有一个包含<asp:Gridview>的aspx页面,并显示数据库中的数据。

在Gridview的标题上,我在每个列上放置了一个TextBox,可以在按Enter后用于从Gridview搜索数据。因此,在按Enter键后,它将重新绑定gridview,然后将显示搜索到的数据。工作正常,但插入文本框的文本消失。

我不希望重新绑定gridview后文本消失。因此,'用户可以继续在其他文本框中插入文本,并且gridview可以显示更具体的数据。'

这可能吗?或者有没有其他办法来实现我的目标?

修改

这是我的代码:

的Page_Load

SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestCS"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        PopulateData();
        onSearch();
    }        
}

搜索事件On_Click

protected void btnFilter_Click(object sender, EventArgs e)
{
    TextBox tbDelegate = (TextBox)TraineeGrid.HeaderRow.Cells[3].FindControl("newDelegate");
    TextBox tbRankPos = (TextBox)TraineeGrid.HeaderRow.Cells[4].FindControl("newRankPos");
    TextBox tbCompany = (TextBox)TraineeGrid.HeaderRow.Cells[5].FindControl("newCompany");
    TextBox tbCourse = (TextBox)TraineeGrid.HeaderRow.Cells[6].FindControl("newCourse");
    TextBox tbCenter = (TextBox)TraineeGrid.HeaderRow.Cells[7].FindControl("newCenter");
    TextBox tbInstructor = (TextBox)TraineeGrid.HeaderRow.Cells[8].FindControl("newInstructor");
    TextBox tbSdate = (TextBox)TraineeGrid.HeaderRow.Cells[9].FindControl("newStartDate");
    TextBox tbEdate = (TextBox)TraineeGrid.HeaderRow.Cells[10].FindControl("newEndDate");
    DropDownList tbCissued = (DropDownList)TraineeGrid.HeaderRow.Cells[11].FindControl("newCertIssued");
    TextBox tbCnumber = (TextBox)TraineeGrid.HeaderRow.Cells[12].FindControl("newCertNumber");
    TextBox tbRemark = (TextBox)TraineeGrid.HeaderRow.Cells[13].FindControl("newRemark");


    DataTable dt = new DataTable();

    using (con)
    {
        string sql = "select ID, Delegate, RankPos, Company, CourseTitle, "+
                     "TrainingCenter, Instructor, convert(varchar(10), " +
                     "StartDate, 101) as [StartDate], convert(varchar(10), "+
                     "EndDate, 101) as [EndDate], CertIssued, CertNumber, "+
                     "Remarks from Trainees ";

        if (tbDelegate.Text != string.Empty)
        {
            HasWhereClause = true;
            sql+= "where Delegate LIKE '%" + tbDelegate.Text + "%'";
        }

        if (tbRankPos.Text != string.Empty)
        {
            if (HasWhereClause)
            {
                sql += "and RankPos LIKE '%" + tbRankPos.Text + "%'";
            }
            else
            {
                HasWhereClause = true;
                sql += "where RankPos LIKE '%" + tbRankPos.Text + "%'";
            }               
        }

        if (tbCompany.Text != string.Empty)
        {
            if (HasWhereClause)
            {
                sql += "and Company LIKE '%" + tbCompany.Text + "%'";
            }
            else
            {
                HasWhereClause = true;
                sql += "where Company LIKE '%" + tbCompany.Text + "%'";
            }
        }

        if (tbCourse.Text != string.Empty)
        {
            if (HasWhereClause)
            {
                sql += "and CourseTitle LIKE '%" + tbCourse.Text + "%'";
            }
            else
            {
                HasWhereClause = true;
                sql += "where CourseTitle LIKE '%" + tbCourse.Text + "%'";
            }
        }

        if (tbCenter.Text != String.Empty)
        {
            if (HasWhereClause)
            {
                sql += "and TrainingCenter LIKE'%" + tbCenter.Text + "%'";
            }
            else
            {
                HasWhereClause = true;
                sql += "where TrainingCenter LIKE'%" + tbCenter.Text + "%'";
            }
        }

        if (tbInstructor.Text != String.Empty)
        {
            if (HasWhereClause)
            {
                sql += "and Instructor LIKE '%" + tbInstructor.Text + "%'";
            }
            else
            {
                HasWhereClause = true;
                sql += "where Instructor LIKE '%" + tbInstructor.Text + "%'";
            }
        }

        if (tbSdate.Text != String.Empty)
        {
            if (HasWhereClause)
            {
                sql += "and StartDate LIKE '%" + tbSdate.Text + "%'";
            }
            else
            {
                HasWhereClause = true;
                sql += "where StartDate LIKE '%" + tbSdate.Text + "%'";
            }
        }

        if (tbEdate.Text != String.Empty)
        {
            if (HasWhereClause)
            {
                sql += "and EndDate LIKE '%" + tbEdate.Text + "%'";
            }
            else
            {
                HasWhereClause = true;
                sql += "where EndDate LIKE '%" + tbEdate.Text + "%'";
            }
        }

        if (tbCissued.SelectedIndex != 0)
        {
            if (HasWhereClause)
            {
                sql += "and CertIssued LIKE '%" + tbCissued.SelectedValue + "%'";
            }
            else
            {
                HasWhereClause = true;
                sql += "where CertIssued LIKE '%" + tbCissued.SelectedValue + "%'";
            }
        }

        if (tbRemark.Text != String.Empty)
        {
            if (HasWhereClause)
            {
                sql += "and Remarks LIKE '%" + tbCissued.Text + "%'";
            }
            else
            {
                HasWhereClause = true;
                sql += "where Remarks LIKE '%" + tbCissued.Text + "%'";
            }
        }
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }
        if (dt.Rows.Count > 0)
        {
            TraineeGrid.DataSource = dt;
            TraineeGrid.DataBind();
        }
        else if (dt.Rows.Count == 0)
        {
            dt.Rows.Add(dt.NewRow());
            TraineeGrid.DataSource = dt;
            TraineeGrid.DataBind();
            TraineeGrid.Rows[0].Cells.Clear();
            TraineeGrid.Rows[0].Cells.Add(new TableCell());
            TraineeGrid.Rows[0].Cells[0].ColumnSpan = dt.Columns.Count;
            TraineeGrid.Rows[0].Cells[0].Text = "No Data Found";
            TraineeGrid.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
        }
}

数据绑定功能

private void PopulateData()
{

    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestCS"].ConnectionString))
    {
        string sql = "select ID, Delegate, RankPos, Company, CourseTitle, TrainingCenter, Instructor, convert(varchar(10), " +
            "StartDate, 101) as [StartDate], convert(varchar(10), EndDate, 101) as [EndDate], CertIssued, CertNumber, Remarks " +
            "from Trainees Order By ID Asc";


        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
   }

    if (dt.Rows.Count > 0)
    {            
        TraineeGrid.DataSource = dt;            
        TraineeGrid.DataBind();
    }

    else
    {
        dt.Rows.Add(dt.NewRow());
        TraineeGrid.DataSource = dt;
        TraineeGrid.DataBind();
        TraineeGrid.Rows[0].Cells.Clear();
        TraineeGrid.Rows[0].Cells.Add(new TableCell());
        TraineeGrid.Rows[0].Cells[0].ColumnSpan = dt.Columns.Count;
        TraineeGrid.Rows[0].Cells[0].Text = "No Data Found";
        TraineeGrid.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
    }
}

OnSearch功能

private void onSearch()
{
    Button btnFilter = (Button)TraineeGrid.HeaderRow.Cells[2].FindControl("gvFilter");
    TextBox tbDelegate = (TextBox)TraineeGrid.HeaderRow.Cells[3].FindControl("newDelegate");
    TextBox tbRankPos = (TextBox)TraineeGrid.HeaderRow.Cells[4].FindControl("newRankPos");
    TextBox tbCompany = (TextBox)TraineeGrid.HeaderRow.Cells[5].FindControl("newCompany");
    TextBox tbCourse = (TextBox)TraineeGrid.HeaderRow.Cells[6].FindControl("newCourse");
    TextBox tbCenter = (TextBox)TraineeGrid.HeaderRow.Cells[7].FindControl("newCenter");
    TextBox tbInstructor = (TextBox)TraineeGrid.HeaderRow.Cells[8].FindControl("newInstructor");
    TextBox tbSdate = (TextBox)TraineeGrid.HeaderRow.Cells[9].FindControl("newStartDate");
    TextBox tbEdate = (TextBox)TraineeGrid.HeaderRow.Cells[10].FindControl("newEndDate");
    DropDownList tbCissued = (DropDownList)TraineeGrid.HeaderRow.Cells[11].FindControl("newCertIssued");
    TextBox tbCnumber = (TextBox)TraineeGrid.HeaderRow.Cells[12].FindControl("newCertNumber");
    TextBox tbRemark = (TextBox)TraineeGrid.HeaderRow.Cells[13].FindControl("newRemark");

    tbDelegate.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbRankPos.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbCompany.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbCourse.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbCenter.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbInstructor.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbSdate.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbEdate.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbCissued.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbCnumber.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
    tbRemark.Attributes.Add("OnKeyPress", "return controlEnter('" + btnFilter.ClientID + "',event)");
}

2 个答案:

答案 0 :(得分:0)

在搜索按钮上单击:

    protected void btnSearch_Click(object sender, EventArgs e)
        {
//this code should be executed before any other code to ensure it is executed.
            ViewState["Values"] += TextBox1.Text;
        }

在gridview行数据绑定方法(http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx):

  protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox t = (TextBox) e.Row.FindControl("TextBox1");
        t.Text = ViewState["Values"].ToString();   
    }   
  }

如果不存在,请阅读如何创建rowdatabound方法

答案 1 :(得分:0)

您需要执行此操作以填充Updating事件的值 这将使用数据填充NewValues。

GridView gv = (GridView)sender;
 for (int i = 0; i < GridViewInput.Columns.Count; i++)
 {
  DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as 
  DataControlFieldCell;
  gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, 
   DataControlRowState.Edit, true);
  }