Solved

How to format asp.net textbox value in javascript?

Posted on 2013-01-05
5
585 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now