Solved

How to format asp.net textbox value in javascript?

Posted on 2013-01-05
5
582 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
  • 3
  • 2
5 Comments
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now