?
Solved

How to format asp.net textbox value in javascript?

Posted on 2013-01-05
5
Medium Priority
?
605 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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…
Suggested Courses

764 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