Click here to Skip to main content
14,644,700 members
Rate this:
Please Sign up or sign in to vote.
hi,
i have dropdownlist with tow element when i chose one of them i want to change text of may label i have add this instruction to my function javascript and i have this

The Controls collection can not be modified because the control contains code blocks (that is to say <% ...% > )


and this is my code

function TChanged() {
            if (document.getElementById("cmb_choi").value == "HeureP") {

                document.getElementById('<%=lblnbr.ClientID%>').value = 'Nbr heure';
            }
           else if (document.getElementById("cmb_choi").value == "JourP") {

                document.getElementById('<%=lblnbr.ClientID%>').text = 'Nbr Jour';
            }
}




<asp:DropDownList ID="cmb_choi" CssClass="drp" runat="server" AppendDataBoundItems="True" onchange="TChanged()">
                       <asp:ListItem Text="--Période--" Value="Période"></asp:ListItem>
                       <asp:ListItem Text="Jour" Value="JourP"></asp:ListItem>
                       <asp:ListItem Text="heur" Value="MoisP"></asp:ListItem>
 </asp:DropDownList>
<asp:Label ID="lblnbr" runat="server" Text="Label"></asp:Label>
Posted
Updated 15-Jul-20 23:06pm
Rate this:
Please Sign up or sign in to vote.

Solution 4

Hi,

Try with below script, this will update label value on selection changes of dropdown control.

function TChanged() 
{
   var control = document.getElementById('<%= cmb_choi.ClientID %>');
   var selectedvalue = control.options[control.selectedIndex].value;

   if (selectedvalue == "HeureP") 
    {
       document.getElementById('<%=lblnbr.ClientID%>').innerHTML = 'Nbr heure';
    }
   else if (selectedvalue == "JourP") 
    {
       document.getElementById('<%=lblnbr.ClientID%>').innerHTML = 'Nbr Jour';
    }
}
   
Comments
Member 11573837 27-May-15 15:42pm
   
hi VJ04,
i have try your solution but i get same error
[no name] 27-May-15 20:57pm
   
Hi,

I have tried with below code in Chrome browser and it looks to be working to me. Is your script loading in browser? I dont know how to attach code here otherwise I will upload my sample code.

Let me know if this helps your problem.

<form id="form1" runat="server">
<script type="text/javascript">
function TChanged() {
var control = document.getElementById('<%= cmb_choi.ClientID %>');
var selectedvalue = control.options[control.selectedIndex].value;

if (selectedvalue == "MoisP") {

document.getElementById('<%=lblnbr.ClientID%>').innerHTML = 'Nbr heure';
}
else if (selectedvalue == "JourP") {

document.getElementById('<%=lblnbr.ClientID%>').innerHTML = 'Nbr Jour';
}
}
</script>

<asp:DropDownList ID="cmb_choi" CssClass="drp" runat="server" AppendDataBoundItems="True" onchange="TChanged()">
<asp:ListItem Text="--Période--" Value="Période">
<asp:ListItem Text="Jour" Value="JourP">
<asp:ListItem Text="heur" Value="MoisP">

<asp:Label ID="lblnbr" runat="server" Text="Label">
</form>
Member 11573837 28-May-15 4:30am
   
hi VJ04,
it work whene i have add i new page but whene i want to add in my page wich i word i have same error in my page i work with scriptmanager and update manager i think this's the problème
[no name] 28-May-15 8:03am
   
Well i don't think scriptmanager or updatepanal make any issues here.. this is still works with it as below. I ave tried the same code by adding master page and content page where i put both script and control in content page where whole content page is in update panel from master container, and it worked in both cases.

Are you doing something different? if possible can you include some code, i will try to help my best :)

Sample 1 - with both script and control on same web page
<form id="form1" runat="server">
<asp:ScriptManager ID="sm1" runat="server" />

<script type="text/javascript">
function TChanged() {
var control = document.getElementById('<%= cmb_choi.ClientID %>');
var selectedvalue = control.options[control.selectedIndex].value;

if (selectedvalue == "MoisP") {

document.getElementById('<%=lblnbr.ClientID%>').innerHTML = 'Nbr heure';
}
else if (selectedvalue == "JourP") {

document.getElementById('<%=lblnbr.ClientID%>').innerHTML = 'Nbr Jour';
}
}
</script>
<asp:UpdatePanel ID="updatePanel" runat="server">
<contenttemplate>
<asp:DropDownList ID="cmb_choi" CssClass="drp" runat="server" AppendDataBoundItems="True" onchange="TChanged()">
<asp:ListItem Text="--Période--" Value="Période">
<asp:ListItem Text="Jour" Value="JourP">
<asp:ListItem Text="heur" Value="MoisP">

<asp:Label ID="lblnbr" runat="server" Text="Label">


</form>


Sample 2 - With master and content page
// This is master page
<form id="form1" runat="server">
<asp:ScriptManager ID="sm1" runat="server" />
<div>Hi
<asp:UpdatePanel ID="updatePanel" runat="server">
<contenttemplate>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" />


</div>
</form>

// This is content page
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<script type="text/javascript">
function TChanged() {
var control = document.getElementById('<%= cmb_choi.ClientID %>');
var selectedvalue = control.options[control.selectedIndex].value;

if (selectedvalue == "MoisP") {

document.getElementById('<%=lblnbr.ClientID%>').innerHTML = 'Nbr heure';
}
else if (selectedvalue == "JourP") {

document.getElementById('<%=lblnbr.ClientID%>').innerHTML = 'Nbr Jour';
}
}
</script>
<asp:DropDownList ID="cmb_choi" CssClass="drp" runat="server" AppendDataBoundItems="True" onchange="TChanged()">
<asp:ListItem Text="--Période--" Value="Période">
<asp:ListItem Text="Jour" Value="JourP">
<asp:ListItem Text="heur" Value="MoisP">

<asp:Label ID="lblnbr" runat="server" Text="Label">

[no name] 29-May-15 7:48am
   
Sure.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

Try this

<asp:dropdownlist id="cmb_choi" cssclass="drp" runat="server" appenddatabounditems="True" onchange="TChanged(); return false;" >
                       <asp:listitem text="--Période--" value="Période"></asp:listitem>
                       <asp:listitem text="Jour" value="JourP"></asp:listitem>
                       <asp:listitem text="heur" value="MoisP"></asp:listitem>
 </asp:dropdownlist>
<asp:label id="lblnbr" runat="server" text="Label" ></asp:label>


Please refer this link http://stackoverflow.com/questions/6986504/how-to-set-text-to-label-with-jquery

Let me know if I miss understood the question.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 5

HTML
<input type="hidden" value="" ClientIDMode="Static" runat="server" id='hid'/>


SCRIPT
<script>
        function mostrar(id) {
            $(document).ready(function () {
                document.getElementById("hid").setAttribute("value", id);
                });
            };        
    </script>


CodeBehid
protected void imgSeleccion_Click(object sender, ImageClickEventArgs e)
        {   
            string aux = hid.Value;
        }


Probado en vs2019

la clave es esto:
ClientIDMode="Static"
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100