I am getting the following error in a listview that has an InsertItemTemplate and an EditItemTemplate where the code is used in both places:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Line 213: <td>
Line 214: <asp:RequiredFieldValidator ID="AgencyBizNameValidatorAdd" runat="server" ErrorMessage="*" ControlToValidate="ddlAgencyBizName" ValidationGroup="add" Font-Size="Large" Font-Bold="True" />
Line 215: <asp:DropDownList ID="ddlAgencyBizName" DataSourceID="SqlDataSourceAgencyBiz" runat="server" DataTextField="AgencyBizName" DataValueField="agencyBizID" SelectedValue='<%# Bind("agencyBizID")%>'>
Line 216: </asp:DropDownList>
Line 217: </td>
The SqlDataSource ...
<asp:SqlDataSource
ID="SqlDataSourceAgencyBiz"
runat="server"
ConnectionString="<%$ ConnectionStrings:dbExecutiveConnectionString %>"
SelectCommand="SELECT 0 as agencyBizID, '' as agencyBizName FROM vFraudAgencyBiz UNION SELECT agencyBizID, Name
as agencyBizName from vFraudAgencyBiz " SelectCommandType="Text">
</asp:SqlDataSource>
The workaround that I have used in this ListView was to look at the problem from a different perspective.I noticed that the problem was happening because the user was interested in saving all values of the cascading relation on the record using the 2-way databinding method BIND.
So what I tried is that instead of attempting to save all the values of the fields in the cascading list relation to one record, I would save only the primary key of the lowest
item in the cascading relationship.
For example in the example of car make-model-color relationship, the color selection is not unique to the model and make selection.Therefore you save all 3 values (make-model-color).
But if you were to change the underlying data such as the color record for a particular make and model has a different primary key than the color record for another make and model then all you need to BIND on the record is the primary key of the color (the
lowest level in the cascading relationship).The problem of selecting the unique color record becomes a problem of user interface handling events and no longer a problem of databinding data to a template control.
Similarly in my demo,
I have demonstrated the case of (country-province-city) relationship.Instead of saving on the address record the keys for all three selections, I save only the primary key of the city.The process of populating the cascading dropdown lists does no longer interfere with the databidnding mechanism for the templated control.
Very nice website :). Still though this is a fundamental issue with databinding in general. When the DropDownList tries to DataBind itself it has no DataBinding context.
This won't work in an InsertItemTemplate today the workaround is to set it manually in the ItemCreated event of the ListView.
David, I have the following code, is this what you were talking about?
Protected
Sub ListView1_ItemInserting(ByVal sender
As
Object,
ByVal e
As ListViewInsertEventArgs)
Dim ddlAgencyBizName
As DropDownList =
DirectCast(ListView1.InsertItem.FindControl("ddlAgencyBizName"),
DropDownList)
If ddlAgencyBizName
IsNot
Nothing
Then
e.Values("agencyBizID") = ddlAgencyBizName.SelectedValue
End
If
End
Sub
You are correct that during the Item insertion the databinding context is empty.
The error message that the user reported happens during both the Edit and the Insert process.
The problem that I proposed a solution for was the problem that causes the reported error message upon both editing and insertion.
In the demos on my website, I managed to solve the problem (for both editing and inserting a record that contains a cascading list) by changing the structure of the data that is
involved in displaying a cascading list to the user.The change is to make the records displayed on the lowest level of the cascading lists unique per every combination of the other members involved in the cascading relationship. You
can see it working on my website. Therefore it works.
You are correct that during the Item insertion the databinding context is empty.
The error message that the user reported happens during both the Edit and the Insert process.
The problem that I proposed a solution for was the problem that causes the reported error message upon both editing and insertion.
In the demos on my website, I managed to solve the problem (for both editing and inserting a record that contains a cascading list) by changing the structure of the data that is
involved in displaying a cascading list to the user.The change is to make the records displayed on the lowest level of the cascading lists unique per every combination of the other members involved in the cascading relationship. You
can see it working on my website. Therefore it works.
You just found a workaround for the issue. This doesn't work because of the same reasons I stated before. It has nothing to do with the inserting event and has everything to do with the fact that DataBound controls will databind themselves. Don't get me
wrong I'm not disputing the fact that you made it work.
Yes but instead of the ItemInserting event ItemCreated event.
I added the following and the page no longer bombs when opening the Insert Item in the listview.
Protected
Sub ListView1_ItemCreated(ByVal sender
As
Object,
ByVal e
As ListViewItemEventArgs)
If e.Item.ItemType = ListViewItemType.InsertItem
Then
Dim ddlAgencyBizName
As DropDownList =
DirectCast(ListView1.InsertItem.FindControl("ddlAgencyBizName"),
DropDownList)
If ddlAgencyBizName
IsNot
Nothing
AndAlso ddlAgencyBizName.Items.Count = 0
Then
strSQL =
"SELECT 0 as agencyBizID, '' as agencyBizName FROM vFraudAgencyBiz UNION SELECT agencyBizID, agencyBizName from vFraudAgencyBiz"
objDS.Clear()
myDA =
New SqlDataAdapter(strSQL, myConn)
' ## pass sql to server
myDA.Fill(objDS)
' ## get the data
ddlAgencyBizName.DataSource = objDS
ddlAgencyBizName.DataBind()
End
If
End
If
End
Sub
It still bombs on the edit. Here is the error message:
'ddlAgencyBizName' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'ddlAgencyBizName' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
CherylH
Member
18 Points
51 Posts
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a data...
Feb 16, 2009 07:14 PM|LINK
I am getting the following error in a listview that has an InsertItemTemplate and an EditItemTemplate where the code is used in both places:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Line 213: <td>
Line 214: <asp:RequiredFieldValidator ID="AgencyBizNameValidatorAdd" runat="server" ErrorMessage="*" ControlToValidate="ddlAgencyBizName" ValidationGroup="add" Font-Size="Large" Font-Bold="True" />
Line 215: <asp:DropDownList ID="ddlAgencyBizName" DataSourceID="SqlDataSourceAgencyBiz" runat="server" DataTextField="AgencyBizName" DataValueField="agencyBizID" SelectedValue='<%# Bind("agencyBizID")%>'>
Line 216: </asp:DropDownList>
Line 217: </td>
The SqlDataSource ...
<asp:SqlDataSource ID="SqlDataSourceAgencyBiz" runat="server" ConnectionString="<%$ ConnectionStrings:dbExecutiveConnectionString %>" SelectCommand="SELECT 0 as agencyBizID, '' as agencyBizName FROM vFraudAgencyBiz UNION SELECT agencyBizID, Name as agencyBizName from vFraudAgencyBiz " SelectCommandType="Text"> </asp:SqlDataSource>Any insight would be greatly appreciated.
Databinding dropDownList Bind()
webswapp
Participant
1852 Points
321 Posts
Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a ...
Feb 16, 2009 10:25 PM|LINK
I have examined this issue in a series of articles available through this link:
http://webswapp.com/categories/ASPNET3/CascadingLists/ListView.aspx
Phillip Williams,
MCITP Database Developer 2008
MCPD Enterprise Application Developer 3.5
http://www.webswapp.com
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a ...
Feb 16, 2009 10:45 PM|LINK
This won't work in an InsertItemTemplate today the workaround is to set it manually in the ItemCreated event of the ListView.
Senior SDE, ASP.NET Team, Microsoft
webswapp
Participant
1852 Points
321 Posts
Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a ...
Feb 16, 2009 11:00 PM|LINK
Hi David,
The workaround that I have used in this ListView was to look at the problem from a different perspective. I noticed that the problem was happening because the user was interested in saving all values of the cascading relation on the record using the 2-way databinding method BIND.
So what I tried is that instead of attempting to save all the values of the fields in the cascading list relation to one record, I would save only the primary key of the lowest item in the cascading relationship.
For example in the example of car make-model-color relationship, the color selection is not unique to the model and make selection. Therefore you save all 3 values (make-model-color). But if you were to change the underlying data such as the color record for a particular make and model has a different primary key than the color record for another make and model then all you need to BIND on the record is the primary key of the color (the lowest level in the cascading relationship). The problem of selecting the unique color record becomes a problem of user interface handling events and no longer a problem of databinding data to a template control.
Similarly in my demo, I have demonstrated the case of (country-province-city) relationship. Instead of saving on the address record the keys for all three selections, I save only the primary key of the city. The process of populating the cascading dropdown lists does no longer interfere with the databidnding mechanism for the templated control.
Regards,
Phillip Williams,
MCITP Database Developer 2008
MCPD Enterprise Application Developer 3.5
http://www.webswapp.com
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a ...
Feb 16, 2009 11:36 PM|LINK
Very nice website :). Still though this is a fundamental issue with databinding in general. When the DropDownList tries to DataBind itself it has no DataBinding context.
Senior SDE, ASP.NET Team, Microsoft
CherylH
Member
18 Points
51 Posts
Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a ...
Feb 17, 2009 02:50 PM|LINK
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a ...
Feb 17, 2009 04:35 PM|LINK
Yes but instead of the ItemInserting event ItemCreated event.
Senior SDE, ASP.NET Team, Microsoft
webswapp
Participant
1852 Points
321 Posts
Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a ...
Feb 17, 2009 04:39 PM|LINK
Hi David,
You are correct that during the Item insertion the databinding context is empty.
The error message that the user reported happens during both the Edit and the Insert process.
The problem that I proposed a solution for was the problem that causes the reported error message upon both editing and insertion.
In the demos on my website, I managed to solve the problem (for both editing and inserting a record that contains a cascading list) by changing the structure of the data that is involved in displaying a cascading list to the user. The change is to make the records displayed on the lowest level of the cascading lists unique per every combination of the other members involved in the cascading relationship. You can see it working on my website. Therefore it works.
Phillip Williams,
MCITP Database Developer 2008
MCPD Enterprise Application Developer 3.5
http://www.webswapp.com
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a ...
Feb 17, 2009 04:50 PM|LINK
You just found a workaround for the issue. This doesn't work because of the same reasons I stated before. It has nothing to do with the inserting event and has everything to do with the fact that DataBound controls will databind themselves. Don't get me wrong I'm not disputing the fact that you made it work.
Hope this helps
Senior SDE, ASP.NET Team, Microsoft
CherylH
Member
18 Points
51 Posts
Re: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a ...
Feb 17, 2009 04:53 PM|LINK
I added the following and the page no longer bombs when opening the Insert Item in the listview.
Protected Sub ListView1_ItemCreated(ByVal sender As Object, ByVal e As ListViewItemEventArgs) If e.Item.ItemType = ListViewItemType.InsertItem Then Dim ddlAgencyBizName As DropDownList = DirectCast(ListView1.InsertItem.FindControl("ddlAgencyBizName"), DropDownList) If ddlAgencyBizName IsNot Nothing AndAlso ddlAgencyBizName.Items.Count = 0 ThenstrSQL =
"SELECT 0 as agencyBizID, '' as agencyBizName FROM vFraudAgencyBiz UNION SELECT agencyBizID, agencyBizName from vFraudAgencyBiz"objDS.Clear()
myDA =
New SqlDataAdapter(strSQL, myConn) ' ## pass sql to servermyDA.Fill(objDS)
' ## get the dataddlAgencyBizName.DataSource = objDS
ddlAgencyBizName.DataBind()
End If End If End Sub It still bombs on the edit. Here is the error message:'ddlAgencyBizName' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
editItemtemplate dropdownlist SelectedValue