FormView Edit Template Issue

P

P. Yanzick

Hello,

I am creating an edit template for a FormView control, changing one of the
textboxes to a dropdown box. The dropdown will be populated from a simple
table with the primary key, and a description (i.e. a colors table, so there
is a colors ID and a color description).

The data source the FormView is pulling data from stores (in this example)
the primary key for the colors table. My ultimate goal is when the FormView
goes into an Edit mode that the description of the color will be set
correctly based off of the ID provided in the record the FormView is
displaying. As an example, if the colors ID is 1, and that happens to be
blue, the dropdown will default to the value of blue.

I've been trying various ways to get this to work, and am not having much
luck. Is there an example somewhere that someone is aware of to show how to
do this? The error I keep getting is below. I'm thinking it has to do with
the order the controls bind, but I do not know for sure. Any suggestions
would be appreciated.

Thanks
Paul

'DropDownList1' 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: 'DropDownList1' 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.

Stack Trace:

[ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is
invalid because it does not exist in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable
dataSource) +975
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +80
System.Web.UI.WebControls.ListControl.PerformSelect() +32
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.WebControls.FormView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding) +3047
System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable
data) +90
System.Web.UI.WebControls.FormView.PerformDataBinding(IEnumerable data)
+30
System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable
data) +126
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments,
DataSourceViewSelectCallback callback) +98
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.WebControls.FormView.DataBind() +23
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +91
System.Web.UI.WebControls.FormView.EnsureDataBound() +176
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
+101
System.Web.UI.Control.EnsureChildControls() +134
System.Web.UI.Control.PreRenderRecursiveInternal() +109
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4435
 
S

S. Justin Gengo [MCP]

Paul,

I'm doing exactly what you describe with a country dropdownlist.

I'm using the list's Databound event, which fires after the list's
databinding has completed so that all the items are in the list before I try
to set the value:

1) In the .aspx file's source view set the drop down's OnDataBound event to
call the correct subroutine. Note that the DataSourceId is calling one of
the new .NET 2.0 databinding datasource objects and that the DataText and
DataValue Fields are being set to columns being returned by that
datasource's select statement in order to generate the dropdown's list
items.

<asp:DropDownList ID="CountrysDropDownList" runat="server"
Font-Size="xx-small" DataSourceID="CountrysSqlDataSource"
DataTextField="CountryName" DataValueField="pk_CountryId"
OnDataBound="CountrysDropDownList_DataBound">

2) In the codebehind the cast the sender as the dropdownlist and then use
the form view's dataitem (a datarowview) to get the bound value for the
dropdownlist.

Protected Sub CountrysDropDownList_DataBound(ByVal sender As Object, ByVal e
As System.EventArgs)
Try
Dim CountrysDropDownList As DropDownList = CType(sender,
DropDownList)
Dim View As Data.DataRowView = FormView1.DataItem
CountrysDropDownList.SelectedValue =
View.Item("fk_CountryId").ToString
Catch ex As Exception
Master.ProcessException("There was an error while selecting the
country for edit mode:", ex)
End Try
End Sub

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
P. Yanzick said:
Hello,

I am creating an edit template for a FormView control, changing one of the
textboxes to a dropdown box. The dropdown will be populated from a simple
table with the primary key, and a description (i.e. a colors table, so
there is a colors ID and a color description).

The data source the FormView is pulling data from stores (in this example)
the primary key for the colors table. My ultimate goal is when the
FormView goes into an Edit mode that the description of the color will be
set correctly based off of the ID provided in the record the FormView is
displaying. As an example, if the colors ID is 1, and that happens to be
blue, the dropdown will default to the value of blue.

I've been trying various ways to get this to work, and am not having much
luck. Is there an example somewhere that someone is aware of to show how
to do this? The error I keep getting is below. I'm thinking it has to do
with the order the controls bind, but I do not know for sure. Any
suggestions would be appreciated.

Thanks
Paul

'DropDownList1' 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: 'DropDownList1' 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.

Stack Trace:

[ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is
invalid because it does not exist in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable
dataSource) +975
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +80
System.Web.UI.WebControls.ListControl.PerformSelect() +32
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.WebControls.FormView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding) +3047

System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable
data) +90
System.Web.UI.WebControls.FormView.PerformDataBinding(IEnumerable data)
+30

System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable
data) +126
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments,
DataSourceViewSelectCallback callback) +98
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.WebControls.FormView.DataBind() +23
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +91
System.Web.UI.WebControls.FormView.EnsureDataBound() +176

System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
+101
System.Web.UI.Control.EnsureChildControls() +134
System.Web.UI.Control.PreRenderRecursiveInternal() +109
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4435
 
P

P. Yanzick

Hello,

Thanks for the suggestion! I did manage to get it working in a different
way than you did actually. I was able to do it all via the interface
without having to do any coding at all! For some reason, I had to create a
new project, but once I did so, it worked fine.

Thanks for your help!

Paul
S. Justin Gengo said:
Paul,

I'm doing exactly what you describe with a country dropdownlist.

I'm using the list's Databound event, which fires after the list's
databinding has completed so that all the items are in the list before I
try to set the value:

1) In the .aspx file's source view set the drop down's OnDataBound event
to call the correct subroutine. Note that the DataSourceId is calling one
of the new .NET 2.0 databinding datasource objects and that the DataText
and DataValue Fields are being set to columns being returned by that
datasource's select statement in order to generate the dropdown's list
items.

<asp:DropDownList ID="CountrysDropDownList" runat="server"
Font-Size="xx-small" DataSourceID="CountrysSqlDataSource"
DataTextField="CountryName" DataValueField="pk_CountryId"
OnDataBound="CountrysDropDownList_DataBound">

2) In the codebehind the cast the sender as the dropdownlist and then use
the form view's dataitem (a datarowview) to get the bound value for the
dropdownlist.

Protected Sub CountrysDropDownList_DataBound(ByVal sender As Object, ByVal
e As System.EventArgs)
Try
Dim CountrysDropDownList As DropDownList = CType(sender,
DropDownList)
Dim View As Data.DataRowView = FormView1.DataItem
CountrysDropDownList.SelectedValue =
View.Item("fk_CountryId").ToString
Catch ex As Exception
Master.ProcessException("There was an error while selecting the
country for edit mode:", ex)
End Try
End Sub

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
P. Yanzick said:
Hello,

I am creating an edit template for a FormView control, changing one of
the textboxes to a dropdown box. The dropdown will be populated from a
simple table with the primary key, and a description (i.e. a colors
table, so there is a colors ID and a color description).

The data source the FormView is pulling data from stores (in this
example) the primary key for the colors table. My ultimate goal is when
the FormView goes into an Edit mode that the description of the color
will be set correctly based off of the ID provided in the record the
FormView is displaying. As an example, if the colors ID is 1, and that
happens to be blue, the dropdown will default to the value of blue.

I've been trying various ways to get this to work, and am not having much
luck. Is there an example somewhere that someone is aware of to show how
to do this? The error I keep getting is below. I'm thinking it has to
do with the order the controls bind, but I do not know for sure. Any
suggestions would be appreciated.

Thanks
Paul

'DropDownList1' 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: 'DropDownList1'
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.

Stack Trace:

[ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which
is invalid because it does not exist in the list of items.
Parameter name: value]
System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable
dataSource) +975
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +80
System.Web.UI.WebControls.ListControl.PerformSelect() +32
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.Control.DataBind() +31
System.Web.UI.Control.DataBindChildren() +236
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +178
System.Web.UI.WebControls.FormView.CreateChildControls(IEnumerable
dataSource, Boolean dataBinding) +3047

System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable
data) +90
System.Web.UI.WebControls.FormView.PerformDataBinding(IEnumerable data)
+30

System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable
data) +126
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments
arguments, DataSourceViewSelectCallback callback) +98
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99
System.Web.UI.WebControls.FormView.DataBind() +23
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +91
System.Web.UI.WebControls.FormView.EnsureDataBound() +176

System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
+101
System.Web.UI.Control.EnsureChildControls() +134
System.Web.UI.Control.PreRenderRecursiveInternal() +109
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Control.PreRenderRecursiveInternal() +233
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+4435
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top