locked
populate textboxes with objectdatasource RRS feed

  • Question

  • User-562051539 posted

    I used the formview with the objectdatasource.

    I need to do a lot of server validation when updating.  And when the formview is databound - for many of the datafields I have  some code  in the code behind (for ex-some controls are only shown for certain values, different pictures are shown depending on values etc).

    It's very annoying to always do formview.findcontrol.

    I would prefer to just have textboxes (and other controls) and set the objectdatasource update parameters to those controls.  My question is - how do I populate the textboxes with the select statement of the objectdatasource?

    Wednesday, November 21, 2012 1:39 AM

Answers

  • User3866881 posted

    I need to do a lot of server validation when updating.

    Hello,

    As far as I see, I think you can do this validation:

    1) If possible, please use javascript/jQuery/AJAX or other technologies to deal with that, because to transfer from server to client will take a period of time as well as energy. If there are too many people accessing your website, your efficiency will be very low.

    2) You can use Server-Side Validator controls instead of using FormView FindControl. Especially using Customized Validation Control:

    http://msdn.microsoft.com/en-us/library/f5db6z8k(v=VS.71).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2012 8:17 PM
  • User3866881 posted

    Wow - I learned a lot from this link - this will probably take care of my validation, so I can use the formview for my update.

    Hi,

    Yes——Laughing

    some of the fields in the formview are shown and others not.

    This, you have to bind all the fields to FormView and show them in Label, and you have to dynamically use FindControl to find them and hide them one by one according to certain situations.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2012 4:05 AM

All replies

  • User-126879547 posted

    Set TextBox text to ObjectDataSource select method

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Text = ObjectDataSource1.Select().ToString();
    }

    I am assuming that ObjectDataSource is returning text

    Wednesday, November 21, 2012 1:43 AM
  • User-562051539 posted

    Set TextBox text to ObjectDataSource select method

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Text = ObjectDataSource1.Select().ToString();
    }

    I am assuming that ObjectDataSource is returning text

    No- the objectdatasource is returning a dataset.  I am filling about 100 textboxes. 

    Wednesday, November 21, 2012 1:46 AM
  • User1528972941 posted

    Hi,

    Instead of using object data source for binding the data to the textboxes you can directly bind the textboxes from code behind file in this way:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
               
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NTierDemoDB"].ToString());
                SqlCommand cmd = new SqlCommand("Select * from Employee where PKEmpId=" + Convert.ToInt32(e.CommandArgument), con);
                cmd.CommandType = CommandType.Text;
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    txtEmpName.Text = dr["EmpName"].ToString();
                    txtEmpSal.Text = dr["EmpSalary"].ToString();
                    txtDOB.Text = dr["DateOfBirth"].ToString();
                    chkIsActive.Checked = Convert.ToBoolean(dr["IsActive"]);
                }
                dr.Close();
                con.Close();
            }
     
        }
    In the above code am getting the id from grid view, depending on your case you can get the id and
    use the code anywhere.
    Wednesday, November 21, 2012 7:30 AM
  • User-562051539 posted

    Hi,

    Instead of using object data source for binding the data to the textboxes you can directly bind the textboxes from code behind file in this way:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
               
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NTierDemoDB"].ToString());
                SqlCommand cmd = new SqlCommand("Select * from Employee where PKEmpId=" + Convert.ToInt32(e.CommandArgument), con);
                cmd.CommandType = CommandType.Text;
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    txtEmpName.Text = dr["EmpName"].ToString();
                    txtEmpSal.Text = dr["EmpSalary"].ToString();
                    txtDOB.Text = dr["DateOfBirth"].ToString();
                    chkIsActive.Checked = Convert.ToBoolean(dr["IsActive"]);
                }
                dr.Close();
                con.Close();
            }
     
        }
    In the above code am getting the id from grid view, depending on your case you can get the id and
    use the code anywhere.

    I did think of that, I just thought there is a faster way.  Is this better than using formview.findcontrol?

    Would you then also do the updating this way and not use the objectdatasource?

    Wednesday, November 21, 2012 8:53 AM
  • User3866881 posted

    I need to do a lot of server validation when updating.

    Hello,

    As far as I see, I think you can do this validation:

    1) If possible, please use javascript/jQuery/AJAX or other technologies to deal with that, because to transfer from server to client will take a period of time as well as energy. If there are too many people accessing your website, your efficiency will be very low.

    2) You can use Server-Side Validator controls instead of using FormView FindControl. Especially using Customized Validation Control:

    http://msdn.microsoft.com/en-us/library/f5db6z8k(v=VS.71).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2012 8:17 PM
  • User1528972941 posted

    yes, we can do the upate in the same way without using object data source:

    In button click event just write the code for updating it:

     
     protected void btnUpdate_Click(object sender, EventArgs e)
        {
            
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NTierDemoDB"].ToString());
            string strQuery = "Update Employee set EmpName='" + txtEmpName.Text + "',EmpSalary=" + txtEmpSal.Text + ",
    DateOfBirth='"
     + txtDOB.Text + "',IsActive='" + chkIsActive.Checked + 
    "'where PKEmpId="
     + Session["ID"];         SqlCommand cmd = new SqlCommand(strQuery, con);         cmd.CommandType = CommandType.Text;         con.Open();         cmd.ExecuteNonQuery();         con.Close();     }
    While editing the record we have to store the id in Session["ID"], which is used 
    while updating the same.
    Thursday, November 22, 2012 12:15 AM
  • User-562051539 posted

    pulsmartin

    I need to do a lot of server validation when updating.

    Hello,

    As far as I see, I think you can do this validation:

    1) If possible, please use javascript/jQuery/AJAX or other technologies to deal with that, because to transfer from server to client will take a period of time as well as energy. If there are too many people accessing your website, your efficiency will be very low.

    2) You can use Server-Side Validator controls instead of using FormView FindControl. Especially using Customized Validation Control:

    http://msdn.microsoft.com/en-us/library/f5db6z8k(v=VS.71).aspx

    Wow - I learned a lot from this link - this will probably take care of my validation, so I can use the formview for my update.

    But there is still my other question.  When selecting - depending on certain values in some of the datafields - some of the fields in the formview are shown and others not.  Also the dropdownlists I have within the formview will have different values to select from.  What would you do in this case, instead of formview.findcontrol when selecting

    Thursday, November 22, 2012 3:50 AM
  • User3866881 posted

    Wow - I learned a lot from this link - this will probably take care of my validation, so I can use the formview for my update.

    Hi,

    Yes——Laughing

    some of the fields in the formview are shown and others not.

    This, you have to bind all the fields to FormView and show them in Label, and you have to dynamically use FindControl to find them and hide them one by one according to certain situations.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2012 4:05 AM
  • User-562051539 posted

    if it can't be bypassed - that's what I'll do.

    Thanks.

    Thursday, November 22, 2012 4:18 AM