473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

FormView.ChangeMode and DataBind

Hi

I have a FormView control within a UserControl. After I DataBind I need to
change the CurrentMode depending on a value from the ObjectDataSource Select
method. When I do this the FormView DataBinds again. Also in the first
DataBound event the controls are null so I have to check for null even
though I know they are there.

I would prefer not to call my select method twice. Also the first databound
event seems pointless.

What is the recommended approach here?
Thanks
Andrew

Aug 13 '07 #1
5 7616
Hi Andrew,

FormView control uses templates to define UI for different modes. If you
start with one mode and call data bind controls from current template are
bound to data source. If you change the mode after first bidning you also
discard current template with all bounded values and data binding has to be
called again to fill in controls from template for new mode.

For second question about null controls. When exactly do you bind fot the
first time?

Regards,
Ladislav
"J055" wrote:
Hi

I have a FormView control within a UserControl. After I DataBind I need to
change the CurrentMode depending on a value from the ObjectDataSource Select
method. When I do this the FormView DataBinds again. Also in the first
DataBound event the controls are null so I have to check for null even
though I know they are there.

I would prefer not to call my select method twice. Also the first databound
event seems pointless.

What is the recommended approach here?
Thanks
Andrew

Aug 14 '07 #2
Hi Andrew,

For your first question about ChangeMode will cause the data re-bound, I
think is by design behavior. Whenever there's any property that is changed,
the data needs to be re-bound. Internally the data bound control will use a
property called RequiresDataBinding to track this. This is how DataSource
controls are designed to work magically without you manually call
DataBind(). You may also noted that whenver you change GridView.EditIndex,
it will also cause the GridView to rebind.

I'm not very clear about your second question "in the first DataBound event
the controls are null", would you please depict more about it? Thanks.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 14 '07 #3
Hi

Thanks for the information. My problem with the null controls problem
happens as follows:

When I change the ChangeMode to FormViewMode.ReadOnly in the DataBound event
the controls in the FormView ItemTemplate are null. It looks like these are
not created when the ChangeMode is changed after the DataBind. When the
DataBound event fires again they are not null again.

protected void fvPub_DataBound(object sender, EventArgs e)
{
if (Status == Status.Published)
{
fvPub.ChangeMode(FormViewMode.ReadOnly);
}
Label myLabel = (Label)fvPub.FindControl("myLabel");
myLabel.Text = "stuff"; // NullReferenceException
}

I can't change the FormViewMode until I have the called the select methos so
I wonder what the best approach is to avoid a double databind and the
problems with null controls in the first DataBound event. I know I can test
for null for each control first but I think this would be fixing a problem
which shouldn't exist in the first place.

Thanks
Andrew
Aug 15 '07 #4
Hi Andrew,

Since you have to rely on the data returned from ObjectDataSource's
selected value to change the mode, I don't think there's a way to avoid
binding twice, unless you first query the data manually to get the flag --
but I think it's still querying the data twice so it's probably no use.

For the null reference issue, I believe this is a common issue that caused
by the fact that FindControl only finds a child control in the direct
children collection. For controls in a template, they're in another
container and cannot be found using FormView.FindControl.

protected void FormView1_DataBound(object sender, EventArgs e)
{
TextBox txt = (TextBox)FindControlRecursive(FormView1, "IdTextBox");
}

public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}

return null;
}
Above code should help you find any child controls in the hierarchy.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 15 '07 #5
Hi Andrew,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help. Thanks.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Gaab | last post by:
I am trying to use some of the new controls in VS 2005 but am having some difficulty. My business logic layer returns Collections of my business objects. I am used to just assigning these...
2
by: sck10 | last post by:
Hello, I have a LinkButton that I use to open a FormView in ReadOnly mode "ShowList", and a LinkButton to open the same FormView in the Insert mode "NewRecord". If the user clicks "ShowList"...
1
by: scartin | last post by:
I'm fairly new to working with ASP web controls, and am running into what seems to be a ridiculous problem that I'm hoping will be a breeze for an experienced ASP developer. I have a GridView...
3
by: Geek | last post by:
Guys, I am working on a project, and I thought using FormView will help good but I am running into a issue. What I have done is that in Form View in Footer template icluded a Link Button which...
1
by: Trev | last post by:
Hi, I'm hoping that someone in this group can shed some light on an issue I'm having with a Formview. I have a Web User Control (.ascx) with a Formview. The Formview contains 2 Multiviews,...
4
by: Mark Olbert | last post by:
I'm trying to understand how to respond to mode changing events in a FormView control. I'm not using datasource controls, so I have to do more of the plumbing myself. Do I have to call the...
1
by: Brad Baker | last post by:
I have created a usercontrol (ascx file) which contains a formview, inside of the formview I have an EditItemTemplate and an ItemTemplate. Both the EditItemTemplate and ItemTemplate have link...
5
by: rsdev | last post by:
Hi Guys, I can't get to the bottom of this one. I have a formview in an updatepanel and I switch between ReadOnly and Edit mode. No problem the first time round but when I try on the second go...
4
by: pankajsingh5k | last post by:
Hi guys, These question is for all the experts... Please help me before my brain explodes The problem is again with the formview control.. I have a formview and i have to use it that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.