Solved

How to format asp.net textbox value in javascript?

Posted on 2013-01-05
5
598 Views
Last Modified: 2013-01-31
Hi,

I have a asp.net textbox and I am seeting value to textbox in javascript. How to style the asp.net textbox value through javascript?

I want to display
Product1: subProduct11,subProduct12, subproduct12; Product2: Subproduct21, subproduct22; Product3: subproduct31, subproduct32

A) I am having this type of string. How to display Product1 Product2 Product3 in bold?

B) after 80 characters in string how to display characters from 81 in second line of textbox?
 <asp:TextBox ID="lblCurrentProductGroups" Width="470px"    runat="server" TextMode="MultiLine"   style="overflow:hidden;"></asp:TextBox>

<asp:button id="btn" runat="server" onclientclick="getdata()">

<script type="javascript">
function getdata(slblProductCodes) {
              document.frm.formview_txttProductGroups.value = slblProductCodes;
                  }
0
Comment
Question by:Saroj13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38748471
Adding TextMode="Multiline" to an asp:TextBox creates a textarea in the rendered page.  Making individual words display in bold in a textarea is not cross browser compatible. Why a textbox instead of a label or some other container? What are you trying to achieve?
0
 

Author Comment

by:Saroj13
ID: 38748501
If i use label, then when i click form submit after postback label is not retaining the value.

Thats why i use textbox. is there any way to retain the values in asp.net label after postback....?

1. If asp.net label, then how to retain the values after postback
2. if asp.net textbox, then how to style the text to bold
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38748615
Text boxes and text areas are made to allow users to enter text. Labels are for static presentation of text. You can retrieve the value of either during a postback. What are your requirements? Do you require user input or are you just displaying information? If user input, then on what part of the text? If the word "Product1" will not be editable by the user but "subProduct11,subProduct12, subproduct12" will be, then perhaps the "Product1" is a label and "subProduct11,subProduct12, subproduct12" is a text box. I would need to understand the function of this before I can make a suggestion.
0
 

Author Comment

by:Saroj13
ID: 38748625
Product1, Subproduct1, subproduct2 is a checkboxlist. After selection of checkboxlist, i am displaying the selected items in the textbox.
But I want the ProductItems should be bold
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 38749027
To capture the user's selections and retrieve them as a string in your code behind use an asp:HiddenField. Use an asp:Label to display the selections to the user without postback.

Something like this:
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblProductGroups" runat="server"></asp:Label>  
    <asp:CheckBoxList ID="testCheckBoxList" runat="server">
        <asp:ListItem>Product1: subProduct11,subProduct12, subproduct12</asp:ListItem>
        <asp:ListItem>Product2: Subproduct21, subproduct22</asp:ListItem>
        <asp:ListItem>Product3: subproduct31, subproduct32</asp:ListItem>
    </asp:CheckBoxList> 
    <asp:HiddenField ID="checkedValues" runat="server" /> 
    </div>
    </form>
    <script type="text/javascript">
    var CHK = document.getElementById("<%=testCheckBoxList.ClientID%>");
    var checkbox = CHK.getElementsByTagName("input");
    for(var i = 0; i < checkbox.length; i++){
        checkbox[i].setAttribute("onchange", "FillLabel();");
    }

    function FillLabel()
    {
        var labelArray = new Array();       
        var label = CHK.getElementsByTagName("label");
        for (var i = 0; i < checkbox.length; i++)
        {
            if (checkbox[i].checked)
            {
                labelArray.push(label[i].innerHTML);
            }
        }
        var labelText = "";
        var display = "";
        var hiddenValue = "";
        for(var j = 0; j < labelArray.length; j++){
            labelText = labelArray[j].toString();
            hiddenValue += labelText + " ";
            display += "<b>" + labelText.substring(0, labelText.indexOf(":")) + "</b>";
            display += labelText.substring(labelText.indexOf(":")) + "</br>";
        }
        document.getElementById("<%=checkedValues.ClientID%>").value = hiddenValue;
        document.getElementById("<%=lblProductGroups.ClientID%>").innerHTML = display;
    }
    </script>
</body>

Open in new window

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question