Implementation method of asp. net simple page control assignment

  • 2021-08-12 02:27:53
  • OfStack

This article illustrates the method of asp. net simple page control assignment. Share it for your reference, as follows:


/// <summary>
///  Assignment   Table name, control name, and only 1 Data 
/// </summary>
protected void SetEvaluate(string TableName, string UpName, string Id)
{
    ContentPlaceHolder cph = (ContentPlaceHolder)Page.Master.FindControl("cph_context");
    UpdatePanel up = (UpdatePanel)cph.FindControl(UpName);
    DataTable dt = LOaPersonLogic.GetPersonTemp("select * from " + TableName + " where ID='" + Id + "'");
    for (int i = 0; i < dt.Columns.Count; i++)
    {
      // Collection header name  dt.Columns[i]
      // Set value dt.Rows[0][i].ToString()
      foreach (Control ctl in up.Controls[0].Controls)
      {
        if ((ctl is TextBox) && ctl.ID.Trim() == dt.Columns[i].ToString().Trim())
        {
          ((TextBox)ctl).Text = dt.Rows[0][i].ToString();
        }
        if ((ctl is DropDownList) && ctl.ID.Trim() == dt.Columns[i].ToString().Trim())
        {
          ((DropDownList)ctl).Items.FindByValue(dt.Rows[0][i].ToString().Trim()).Selected = true;
        }
      }
    }
}
/// <summary>
///  Generate sql  Modify sql
/// </summary>
/// <param name="TableName"> Table name </param>
/// <param name="WyId"> Only 1id Primary key </param>
/// <param name="UpName"></param>
/// <param name="Id"> Modify id</param>
protected string CreateSql(string TableName, string WyId, string UpName, string Id)
{
    string SQL = "update " + TableName + " set ";
    ContentPlaceHolder cph = (ContentPlaceHolder)Page.Master.FindControl("cph_context");
    UpdatePanel up = (UpdatePanel)cph.FindControl(UpName);
    foreach (Control ctl in up.Controls[0].Controls)
    {
      if (ctl is TextBox)
      {
        SQL = SQL + ctl.ID + "='" + ((TextBox)ctl).Text + "',";
      }
      if (ctl is DropDownList)
      {
        SQL = SQL + ctl.ID + "='" + ((DropDownList)ctl).SelectedItem.Value.Trim() + "',";
      }
    }
    if (SQL.IndexOf(',') > -1)
    {
      SQL = SQL.Substring(0, SQL.Length - 1) + " where " + WyId + "='" + Id + "'";
    }
    return SQL;
}

For more readers interested in asp. net, please check the topics of this site: "Summary of asp. net Operation json Skills", "Summary of asp. net String Operation Skills", "Summary of asp. net Operation XML Skills", "Summary of asp. net File Operation Skills", "Summary of asp. net ajax Skills" and "Summary of asp. net Cache Operation Skills".

I hope this article is helpful to everyone's asp. net programming.


Related articles: