Auto resize Html.TextBoxFor
I have this text-box on the page:
<%= Html.TextBoxFor(m => m.STPData.ProjectName, new { @class = "economicTextBox", disabled="disab开发者_StackOverflow社区led", propertyName = "STPData.ProjectName", onchange = "UpdateField(this);" })%>
How can I make this so every time it renders it fits to the text entered?
Unless you use some monospace font like currier new, you cannot predict how much px will it take. But you can always estimate, 10 pixels per character or similar. Then some user has large fonts which you cannot detect, and everything looks wrong ... I suggest that you rethink this feature, from UX perspective or tech challenges!
Maybe something like this, with helper method that multiplies string length with average pixels per char (10?):
<%= Html.TextBoxFor(m => m.STPData.ProjectName,
new { @class = "economicTextBox",
style="width:"+Helper.PixelsForString(m.STPData.ProjectName,10)+"px;",
disabled="disabled",
propertyName = "STPData.ProjectName",
onchange = "UpdateField(this);" })%>
精彩评论