How to update multiple div contents using C#?

How to update multiple div contents using C#?



In a Get Response from an API, I need to update the div's accordingly.
Below is my HTML Code


<div class="col-md-2 mb-4">
<div class="card mb-4" id="roomName_1" runat="server">
<div class="card-header text-center">
Room 1
</div>
<div class="card-body" style="background-color:#ff0000;color:white;">
<p>
<asp:Literal Text="Organizer" runat="server" ID="organiser_1" />
<br />
<label id="from_1">From</label>
-
<label id="to_1">To</label>
<br />
<asp:Literal Text="Subject" runat="server" ID="subject_1" />
</p>
</div>
</div>
</div>



There are 24 rooms like this, and for each room I have to update Organizer, From, To, Subject which I get from a GET Request.


private void GetRoom(string uri)
var request = WebRequest.Create(uri);
string text;
var response = (HttpWebResponse)request.GetResponse();
request.ContentType = "application/json; charset=utf-8";
using(var sr = new StreamReader(response.GetResponseStream()))
text = sr.ReadToEnd();
List<RoomConfigurationViewInfo> roomObject = JsonConvert.DeserializeObject < List<RoomConfigurationViewInfo>> (text);
foreach(var value in roomObject)
Response.Write(value.Organizer);
Response.Write(value.Subject);






So, Is it possible to Update multiple Div's using C#? & if not then how can I achieve this.



Please suggest.





You can't. You need to use AJAX in the client side (Link here api.jquery.com/jquery.get ). Also, your method in the controller must not be void because you need the data to be passed to the view.
– Lewis86
Aug 22 at 13:22




2 Answers
2



I found a way that you can achieve this:



At first, add ID to your first tag: <div id="rooms" runat="server" class="col-md-2 mb-4">


<div id="rooms" runat="server" class="col-md-2 mb-4">



Now in code behind, you can try this:


private void GetRoom(string uri)

var request = WebRequest.Create(uri);
string text;
var response = (HttpWebResponse)request.GetResponse();
request.ContentType = "application/json; charset=utf-8";
using(var sr = new StreamReader(response.GetResponseStream()))

text = sr.ReadToEnd();
List<RoomConfigurationViewInfo> roomObject = JsonConvert.DeserializeObject < List<RoomConfigurationViewInfo>> (text);

rooms.InnerHtml = "";

for(int i = 0; i < roomObject.Count; i++)

string strOrganizer = roomObject[i].Organizer;
string strSubject = roomObject[i].Subject;
string strFrom = roomObject[i].From;
string strTo = roomObject[i].To;

rooms.InnerHtml += "<div class="card mb-4">"
+ "<div class="card-header text-center">"
+ "Room " + i + "";
+ "</div>"
+ "<div class="card-body" style="background-color:#ff0000;color:white;">"
+ "<p>"
+ "<asp:Literal Text="" + strOrganizer "" runat="server" ID="organiser_" + i + ""/>"
+ "<br />"
+ "<label>" + strFrom + "</label>"
+ "-"
+ "<label>" + strTo + "</label>"
+ "<br />"
+ "<asp:Literal Text="" + strSubject + "" runat="server" ID="subject_" + i + "" />"
+ "</p>"
+ "</div>"
+ "</div>";





I hope it helps you



It depends if you are doing it dynamically or not. If you are simply loading the page and updating the divs, pass it as a variable and then iterate through it.



Your code on your page would be something like this:


<% foreach (var myItem in g) %>
<p>
<%= myItem.Organizer%>
<br />
<label id="from_1">From</label>
-
<label id="to_1">To</label>
<br />
<%= myItem.Subject%>
</p>
<% %>



If you are trying to do it dynamically, the commenter above is correct and you would have to do it through jQuery/Javascript.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ