---
title: "How to assign hiddenfield value to select2 on postback"  
description: "How to assign hiddenfield value to select2 on postback"  
author: "Anonymous User"  
published: 2015-01-26  
updated: 2015-01-27  
canonical: https://www.mindstick.com/forum/12902/how-to-assign-hiddenfield-value-to-select2-on-postback  
category: "c#"  
tags: ["asp.net", "jquery"]  
reading_time: 1 minute  

---

# How to assign hiddenfield value to select2 on postback

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to assign [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) to select2 [control](https://www.mindstick.com/blog/197/asp-dot-net-repeater-control) from a hiddenfield in clientside script. Value is not assigned to select2 control after [postback](https://www.mindstick.com/blog/342/postback-in-asp-dot-net) for the following code.

```
$(document).ready(function () {     $("#cboIndustry").select2();      $.getJSON(uriSector+ '/' + 'GetIndustrySectors')           .done(function (data) {               $.each(data, function (key, item) {                  $("#cboIndustry").append($("<option></option>").val(item.IndustrySectorID).html(item.IndustrySectorName));               });           });      $("#cboIndustry").on('change', function () {            if ($("#cboIndustry").val() != "-1") {                  var id = $("#cboIndustry").val();                $('#HiddenIndustrySectorID').val(id);                SelectedName = $('#cboIndustry option:selected').text();                $('#HiddenIndustrySectorName').val(SelectedName);            }        });    var SelectedIndustry = $('#HiddenIndustrySectorID').val();    $("#cboIndustry").select2().select('val',SelectedIndustry); });
```

**However value get assigned if I [put](https://answers.mindstick.com/qa/111890/can-you-explain-the-difference-between-put-and-post-http-methods) [alert](https://answers.mindstick.com/qa/30927/what-is-an-alert) before assigning**

[var](https://www.mindstick.com/forum/33920/what-is-different-var-and-dynamic-types-in-c-sharp) SelectedIndustry = $('#HiddenIndustrySectorID').val();

alert(SelectedIndustry);

$("#cboIndustry").select2().select('val',SelectedIndustry);

// These steps I have included, for retaining value in select2 on postback.

What could be the reason? Please help me.

## Replies

### Reply by Manoj Bhatt

```
$(document).ready(function () {    $("#cboIndustry").select2();     $.getJSON(uriSector+
'/' + 'GetIndustrySectors')       .done(function (data)
{           $.each(data,
function (key, item) {              $("#cboIndustry").append($("<option></option>").val(item.IndustrySectorID).html(item.IndustrySectorName));        });    //This change solves my problem     var SelectedIndustry= $('#HiddenIndustrySectorID').val();     $("#cboIndustry").select2().select('val',SelectedIndustry);  });  $("#cboIndustry").on('change',
function () {        if ($("#cboIndustry").val()!= "-1") {            var id = $("#cboIndustry").val();            $('#HiddenIndustrySectorID').val(id);            SelectedName= $('#cboIndustry option:selected').text();            $('#HiddenIndustrySectorName').val(SelectedName);        }    }); });
```


---

Original Source: https://www.mindstick.com/forum/12902/how-to-assign-hiddenfield-value-to-select2-on-postback

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
