Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I having a textbox on .ascx page. textbox is field at the time of pageload. textbox is readonly. now I want size of textbox as per the text in it. how to do this?

I use following java script:

<script type="text/javascript">
function setHeight(txtdesc) {
txtdesc.style.height = txtdesc.scrollHeight + "px";
}
</script>

for this following textbox

ASP.NET
<asp:TextBox ID="suggText" runat="server" OnTextChanged="setHeight(this)" CommandArgument="<%#Container.DataItemIndex %>"  Text='<%# DataBinder.Eval(Container.DataItem,"suggetion") %>' Font-Size="Smaller" TextMode="MultiLine" Enabled="False"   OnCommand="txtSuggestion_Command" Height="20px" ReadOnly="True"></asp:TextBox>



but it gives error that


C#
'ASP.controls_suggestions_ascx' does not contain a definition for 'setHeight' and no extension method 'setHeight' accepting a first argument of type 'ASP.controls_suggestions_ascx' could be found (are you missing a using directive or an assembly reference?)



please help

What I have tried:

I use following java script:

<script type="text/javascript">
function setHeight(txtdesc) {
txtdesc.style.height = txtdesc.scrollHeight + "px";
}
</script>

for this following textbox

ASP.NET
<asp:TextBox ID="suggText" runat="server" OnTextChanged="setHeight(this)" CommandArgument="<%#Container.DataItemIndex %>"  Text='<%# DataBinder.Eval(Container.DataItem,"suggetion") %>' Font-Size="Smaller" TextMode="MultiLine" Enabled="False"   OnCommand="txtSuggestion_Command" Height="20px" ReadOnly="True"></asp:TextBox>



but it gives error that


C#
'ASP.controls_suggestions_ascx' does not contain a definition for 'setHeight' and no extension method 'setHeight' accepting a first argument of type 'ASP.controls_suggestions_ascx' could be found (are you missing a using directive or an assembly reference?)



please help
Posted
Updated 12-Apr-16 6:19am
Comments
CHill60 12-Apr-16 11:24am    
It might be a silly question but did you put your javascript function setHeight in controls_suggestions.ascx ?

1 solution

You're trying to call a client-side method (setHeight) from a server-side event (TextChanged). Either use the onchange attribute:
ASP.NET
<asp:TextBox ID="suggText" runat="server" onchange="setHeight(this)" ...

or wire the event-handler up from Javascript:
JavaScript
var element = document.querySelector("textarea[name$=suggText]");
element.addEventListener("change", function(){
    setHeight(this);
});
 
Share this answer
 
Comments
Kishor-KW 12-Apr-16 14:07pm    
NO,textbox size is not changed or adjust as per contain in it. but using onchange event error is not occurring. still my goal is not hit
Richard Deeming 12-Apr-16 14:09pm    
Well that event is only going to fire when the content of the textbox changes on the client. Since you've set the textbox to be disabled, the content will never change, so the event will never fire.

You need to call your setHeight method in the body's load event instead.
Kishor-KW 12-Apr-16 14:11pm    
yes u r r8. is there any event to do this. Thank you for reply :)
Richard Deeming 12-Apr-16 14:16pm    
body.addEventListener("load", function(){
   var element = document.querySelector("textarea[name$=suggText]");
   setHeight(element);
});


That should work in modern browsers. If you need to support old browsers - particularly old version of IE - then you'll need to modify it, or use a library like jQuery[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900