ASP .NET Dropdownlist.Selectedvalue not updating iterating through dynamically created controls
I have a problem with selectedvalue property in a dynamic created dropdownlist control not updating:
I have a custom control of type ctlProductosFormatos containing a combo and other controls. I create this custom controls dynamically in other place, and then I iterate through all the combos in the dynamically created controls, to update the items list on them copying from another dummy dropdownlist with the values I want.
The controls in this code are iterated right, and the combos are also filled right. My problem is that I want to maintain the selectedvalue on each combo at the same value that it has before the items update, but this fails.
If i step in the code and run only the iteration for the first combo, and jump outside the loop, this combo is right filled and with the right selected value, but if I run the full loop, all the combos are set with the same value than the last combo, so I suppose it's something related with something like I'm instantiating the same control for all the iterations, but it seem not to be that in my code.
Dim ControlFormato As ctlProductosFormatos
' Iterates through custom controls collection, IEnumerable(Of ctlProductosFormatos)
For Each ControlFormato In Controles
' Get the dropdownlist inside the current custom control
Dim ControlComboFind As Control = ControlFormato.FindControl("cmbFotoFormato")
Dim ControlCombo As DropDownList = CType(ControlComboFind, DropDownList)
' Get the currently selected value in the dropdownlist
Dim ValorSeleccionado As String = ControlCombo.SelectedValue
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
' Sets the current combo selected item with the previously saved one
ControlCombo.SelectedValue = ValorSeleccionado
Next
.net dynamic drop-down-menu selectedvalue
add a comment |
I have a problem with selectedvalue property in a dynamic created dropdownlist control not updating:
I have a custom control of type ctlProductosFormatos containing a combo and other controls. I create this custom controls dynamically in other place, and then I iterate through all the combos in the dynamically created controls, to update the items list on them copying from another dummy dropdownlist with the values I want.
The controls in this code are iterated right, and the combos are also filled right. My problem is that I want to maintain the selectedvalue on each combo at the same value that it has before the items update, but this fails.
If i step in the code and run only the iteration for the first combo, and jump outside the loop, this combo is right filled and with the right selected value, but if I run the full loop, all the combos are set with the same value than the last combo, so I suppose it's something related with something like I'm instantiating the same control for all the iterations, but it seem not to be that in my code.
Dim ControlFormato As ctlProductosFormatos
' Iterates through custom controls collection, IEnumerable(Of ctlProductosFormatos)
For Each ControlFormato In Controles
' Get the dropdownlist inside the current custom control
Dim ControlComboFind As Control = ControlFormato.FindControl("cmbFotoFormato")
Dim ControlCombo As DropDownList = CType(ControlComboFind, DropDownList)
' Get the currently selected value in the dropdownlist
Dim ValorSeleccionado As String = ControlCombo.SelectedValue
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
' Sets the current combo selected item with the previously saved one
ControlCombo.SelectedValue = ValorSeleccionado
Next
.net dynamic drop-down-menu selectedvalue
add a comment |
I have a problem with selectedvalue property in a dynamic created dropdownlist control not updating:
I have a custom control of type ctlProductosFormatos containing a combo and other controls. I create this custom controls dynamically in other place, and then I iterate through all the combos in the dynamically created controls, to update the items list on them copying from another dummy dropdownlist with the values I want.
The controls in this code are iterated right, and the combos are also filled right. My problem is that I want to maintain the selectedvalue on each combo at the same value that it has before the items update, but this fails.
If i step in the code and run only the iteration for the first combo, and jump outside the loop, this combo is right filled and with the right selected value, but if I run the full loop, all the combos are set with the same value than the last combo, so I suppose it's something related with something like I'm instantiating the same control for all the iterations, but it seem not to be that in my code.
Dim ControlFormato As ctlProductosFormatos
' Iterates through custom controls collection, IEnumerable(Of ctlProductosFormatos)
For Each ControlFormato In Controles
' Get the dropdownlist inside the current custom control
Dim ControlComboFind As Control = ControlFormato.FindControl("cmbFotoFormato")
Dim ControlCombo As DropDownList = CType(ControlComboFind, DropDownList)
' Get the currently selected value in the dropdownlist
Dim ValorSeleccionado As String = ControlCombo.SelectedValue
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
' Sets the current combo selected item with the previously saved one
ControlCombo.SelectedValue = ValorSeleccionado
Next
.net dynamic drop-down-menu selectedvalue
I have a problem with selectedvalue property in a dynamic created dropdownlist control not updating:
I have a custom control of type ctlProductosFormatos containing a combo and other controls. I create this custom controls dynamically in other place, and then I iterate through all the combos in the dynamically created controls, to update the items list on them copying from another dummy dropdownlist with the values I want.
The controls in this code are iterated right, and the combos are also filled right. My problem is that I want to maintain the selectedvalue on each combo at the same value that it has before the items update, but this fails.
If i step in the code and run only the iteration for the first combo, and jump outside the loop, this combo is right filled and with the right selected value, but if I run the full loop, all the combos are set with the same value than the last combo, so I suppose it's something related with something like I'm instantiating the same control for all the iterations, but it seem not to be that in my code.
Dim ControlFormato As ctlProductosFormatos
' Iterates through custom controls collection, IEnumerable(Of ctlProductosFormatos)
For Each ControlFormato In Controles
' Get the dropdownlist inside the current custom control
Dim ControlComboFind As Control = ControlFormato.FindControl("cmbFotoFormato")
Dim ControlCombo As DropDownList = CType(ControlComboFind, DropDownList)
' Get the currently selected value in the dropdownlist
Dim ValorSeleccionado As String = ControlCombo.SelectedValue
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
' Sets the current combo selected item with the previously saved one
ControlCombo.SelectedValue = ValorSeleccionado
Next
.net dynamic drop-down-menu selectedvalue
.net dynamic drop-down-menu selectedvalue
edited Nov 13 '18 at 2:05
Cœur
17.5k9104145
17.5k9104145
asked May 12 '13 at 0:52
tomasofentomasofen
8041914
8041914
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The problem is the ViewState. If you are modifying dynamically the items inside a DropDownList () it's ViewState won't be updated so when you posting back the values the framework will look after the sent value inside the control's ViewState but the value will be mising so the SelectedValue property will not be set. If you want to use the posted values then get them from Request.Form[ddlName.ClientID] (i'm not sure about that the ClientID will be the correct index but the main idea is this).
Thanks for the tip, but the problem was not related with this, because if i break the loop processing only one element, the item processed work fine. I finally get the idea that the problem was in the combo duplication method. It seems that the items were shared between combos using this method, and changing selectedvalue in one of them affect the others. I publish the change that solves my problem. Any way, thanks for your thinkings ;)
– tomasofen
May 12 '13 at 7:41
add a comment |
As i wrote in Peter's comment, the problem was in the combo items duplication, that was behaving as something like "sharing items". I replace the duplication line for this ugliest and longer code, and the problem was solved. Hope it helps.
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
' THIS LINE HAS BEEN COMMENTED AND REPLACED FOR THE FOLLOWING CODE
' ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
Dim LSource As ListItem
Dim LDestination As ListItem
For i = 0 To ComboPatron.Items.Count - 1
LSource = ComboPatron.Items(i)
LDestination = New ListItem(LSource.Text, LSource.Value)
ControlCombo.Items.Add(LDestination)
Next
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16503147%2fasp-net-dropdownlist-selectedvalue-not-updating-iterating-through-dynamically-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is the ViewState. If you are modifying dynamically the items inside a DropDownList () it's ViewState won't be updated so when you posting back the values the framework will look after the sent value inside the control's ViewState but the value will be mising so the SelectedValue property will not be set. If you want to use the posted values then get them from Request.Form[ddlName.ClientID] (i'm not sure about that the ClientID will be the correct index but the main idea is this).
Thanks for the tip, but the problem was not related with this, because if i break the loop processing only one element, the item processed work fine. I finally get the idea that the problem was in the combo duplication method. It seems that the items were shared between combos using this method, and changing selectedvalue in one of them affect the others. I publish the change that solves my problem. Any way, thanks for your thinkings ;)
– tomasofen
May 12 '13 at 7:41
add a comment |
The problem is the ViewState. If you are modifying dynamically the items inside a DropDownList () it's ViewState won't be updated so when you posting back the values the framework will look after the sent value inside the control's ViewState but the value will be mising so the SelectedValue property will not be set. If you want to use the posted values then get them from Request.Form[ddlName.ClientID] (i'm not sure about that the ClientID will be the correct index but the main idea is this).
Thanks for the tip, but the problem was not related with this, because if i break the loop processing only one element, the item processed work fine. I finally get the idea that the problem was in the combo duplication method. It seems that the items were shared between combos using this method, and changing selectedvalue in one of them affect the others. I publish the change that solves my problem. Any way, thanks for your thinkings ;)
– tomasofen
May 12 '13 at 7:41
add a comment |
The problem is the ViewState. If you are modifying dynamically the items inside a DropDownList () it's ViewState won't be updated so when you posting back the values the framework will look after the sent value inside the control's ViewState but the value will be mising so the SelectedValue property will not be set. If you want to use the posted values then get them from Request.Form[ddlName.ClientID] (i'm not sure about that the ClientID will be the correct index but the main idea is this).
The problem is the ViewState. If you are modifying dynamically the items inside a DropDownList () it's ViewState won't be updated so when you posting back the values the framework will look after the sent value inside the control's ViewState but the value will be mising so the SelectedValue property will not be set. If you want to use the posted values then get them from Request.Form[ddlName.ClientID] (i'm not sure about that the ClientID will be the correct index but the main idea is this).
answered May 12 '13 at 5:17
Peter KissPeter Kiss
8,81821937
8,81821937
Thanks for the tip, but the problem was not related with this, because if i break the loop processing only one element, the item processed work fine. I finally get the idea that the problem was in the combo duplication method. It seems that the items were shared between combos using this method, and changing selectedvalue in one of them affect the others. I publish the change that solves my problem. Any way, thanks for your thinkings ;)
– tomasofen
May 12 '13 at 7:41
add a comment |
Thanks for the tip, but the problem was not related with this, because if i break the loop processing only one element, the item processed work fine. I finally get the idea that the problem was in the combo duplication method. It seems that the items were shared between combos using this method, and changing selectedvalue in one of them affect the others. I publish the change that solves my problem. Any way, thanks for your thinkings ;)
– tomasofen
May 12 '13 at 7:41
Thanks for the tip, but the problem was not related with this, because if i break the loop processing only one element, the item processed work fine. I finally get the idea that the problem was in the combo duplication method. It seems that the items were shared between combos using this method, and changing selectedvalue in one of them affect the others. I publish the change that solves my problem. Any way, thanks for your thinkings ;)
– tomasofen
May 12 '13 at 7:41
Thanks for the tip, but the problem was not related with this, because if i break the loop processing only one element, the item processed work fine. I finally get the idea that the problem was in the combo duplication method. It seems that the items were shared between combos using this method, and changing selectedvalue in one of them affect the others. I publish the change that solves my problem. Any way, thanks for your thinkings ;)
– tomasofen
May 12 '13 at 7:41
add a comment |
As i wrote in Peter's comment, the problem was in the combo items duplication, that was behaving as something like "sharing items". I replace the duplication line for this ugliest and longer code, and the problem was solved. Hope it helps.
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
' THIS LINE HAS BEEN COMMENTED AND REPLACED FOR THE FOLLOWING CODE
' ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
Dim LSource As ListItem
Dim LDestination As ListItem
For i = 0 To ComboPatron.Items.Count - 1
LSource = ComboPatron.Items(i)
LDestination = New ListItem(LSource.Text, LSource.Value)
ControlCombo.Items.Add(LDestination)
Next
add a comment |
As i wrote in Peter's comment, the problem was in the combo items duplication, that was behaving as something like "sharing items". I replace the duplication line for this ugliest and longer code, and the problem was solved. Hope it helps.
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
' THIS LINE HAS BEEN COMMENTED AND REPLACED FOR THE FOLLOWING CODE
' ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
Dim LSource As ListItem
Dim LDestination As ListItem
For i = 0 To ComboPatron.Items.Count - 1
LSource = ComboPatron.Items(i)
LDestination = New ListItem(LSource.Text, LSource.Value)
ControlCombo.Items.Add(LDestination)
Next
add a comment |
As i wrote in Peter's comment, the problem was in the combo items duplication, that was behaving as something like "sharing items". I replace the duplication line for this ugliest and longer code, and the problem was solved. Hope it helps.
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
' THIS LINE HAS BEEN COMMENTED AND REPLACED FOR THE FOLLOWING CODE
' ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
Dim LSource As ListItem
Dim LDestination As ListItem
For i = 0 To ComboPatron.Items.Count - 1
LSource = ComboPatron.Items(i)
LDestination = New ListItem(LSource.Text, LSource.Value)
ControlCombo.Items.Add(LDestination)
Next
As i wrote in Peter's comment, the problem was in the combo items duplication, that was behaving as something like "sharing items". I replace the duplication line for this ugliest and longer code, and the problem was solved. Hope it helps.
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
' THIS LINE HAS BEEN COMMENTED AND REPLACED FOR THE FOLLOWING CODE
' ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
Dim LSource As ListItem
Dim LDestination As ListItem
For i = 0 To ComboPatron.Items.Count - 1
LSource = ComboPatron.Items(i)
LDestination = New ListItem(LSource.Text, LSource.Value)
ControlCombo.Items.Add(LDestination)
Next
answered May 12 '13 at 7:45
tomasofentomasofen
8041914
8041914
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16503147%2fasp-net-dropdownlist-selectedvalue-not-updating-iterating-through-dynamically-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown