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?
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:
protectedvoid GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["NTierDemoDB"].ToString());
SqlCommand cmd = newSqlCommand("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.
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:
protectedvoid GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["NTierDemoDB"].ToString());
SqlCommand cmd = newSqlCommand("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?
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:
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:
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
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——
pulsmartin
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.
pulsmartin
Member
108 Points
309 Posts
populate textboxes with objectdatasource
Nov 21, 2012 05:39 AM|LINK
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?
RameshRajend...
Star
7983 Points
2099 Posts
Re: populate textboxes with objectdatasource
Nov 21, 2012 05:43 AM|LINK
Set TextBox text to ObjectDataSource select method
I am assuming that ObjectDataSource is returning text
pulsmartin
Member
108 Points
309 Posts
Re: populate textboxes with objectdatasource
Nov 21, 2012 05:46 AM|LINK
No- the objectdatasource is returning a dataset. I am filling about 100 textboxes.
sravanisrav
Member
470 Points
107 Posts
Re: populate textboxes with objectdatasource
Nov 21, 2012 11:30 AM|LINK
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:
Thanks & Regards
Sravani
pulsmartin
Member
108 Points
309 Posts
Re: populate textboxes with objectdatasource
Nov 21, 2012 12:53 PM|LINK
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?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: populate textboxes with objectdatasource
Nov 22, 2012 12:17 AM|LINK
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
sravanisrav
Member
470 Points
107 Posts
Re: populate textboxes with objectdatasource
Nov 22, 2012 04:15 AM|LINK
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:
Thanks & Regards
Sravani
pulsmartin
Member
108 Points
309 Posts
Re: populate textboxes with objectdatasource
Nov 22, 2012 07:50 AM|LINK
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
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: populate textboxes with objectdatasource
Nov 22, 2012 08:05 AM|LINK
Hi,
Yes——
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.
pulsmartin
Member
108 Points
309 Posts
Re: populate textboxes with objectdatasource
Nov 22, 2012 08:18 AM|LINK
if it can't be bypassed - that's what I'll do.
Thanks.