• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

How to format asp.net textbox value in javascript?

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
Saroj13
Asked:
Saroj13
  • 3
  • 2
1 Solution
 
Tom BeckCommented:
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
 
Saroj13Author Commented:
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
 
Tom BeckCommented:
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
 
Saroj13Author Commented:
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
 
Tom BeckCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now