简体   繁体   English

如何设置gridview文本框高度以避免在itemTemplate中滚动

[英]How to set gridview textbox height to avoid scrolling in itemtemplate

Hello and thanks for looking - I've searched for this and cant get it ! 您好,感谢您的关注-我已经搜索过此内容,无法获取!

I am trying to set the height of a text box which is in an ItemTemplate in a gridview. 我试图设置在gridview的ItemTemplate中的文本框的高度。 The gridview is bound from a datatable in the code behind. gridview是从后面代码中的数据表绑定的。 I am trying to be able to set the height of the textbox to prevent scrolling. 我试图设置文本框的高度以防止滚动。 The width is set (30% of container). 设置宽度(容器的30%)。 the Gridview sits in an Updatepanel. Gridview位于Updatepanel中。

The text contains a mixture of text and carriage returns. 文本包含文本和回车符的混合。

I can do this via javascript but this seems to require setting from eg onkeyup 我可以通过javascript做到这一点,但这似乎需要从onkeyup进行设置

  function textAreaAdjust(o) {
        o.style.height = o.scrollHeight;
    }

but i cant get this to happen on rowdatabound so when the text is placed in the textbox, it is constrained by the height of the height of the gridview row. 但是我无法在rowdatabound上发生这种情况,因此当文本放置在文本框中时,它受gridview行高度的高度限制。

here is the code for the itemtemplate 这是itemtemplate的代码

  <asp:TemplateField HeaderText="Jobs / Notes" HeaderStyle-CssClass="JobNotesCol">
            <EditItemTemplate>
                 <asp:TextBox ID="JobNotesTB"  runat="server" Text='<%# Bind("JobNotes") %>' OnTextChanged="TB_TextChanged" TextMode="MultiLine"  style="min-height:98%; width:98%;" onkeyup="textAreaAdjust(this)" AutoPostBack="True" ></asp:TextBox>
            </EditItemTemplate>
             <ItemTemplate>
                <asp:TextBox ID="JobNotesTB"  runat="server" Text='<%# Bind("JobNotes") %>' OnTextChanged="TB_TextChanged" TextMode="MultiLine"  style="min-height:98%; width:98%;" onkeyup="textAreaAdjust(this)" AutoPostBack="True" ></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

So my question is how should i do this - can i work out the height it needs to be and just set this from the c# onrowdatabound ?, or can i fire the java when the row is bound ? 所以我的问题是我应该怎么做-我可以计算出所需的高度并仅通过c#onrowdatabound设置它吗?还是可以在绑定行时触发Java?

I am restrained by having to use IE8 and so the handy looking jquery autosizing textbox is out ! 我不得不使用IE8受到限制,因此方便查找的jquery自动调整大小文本框不可用! - until we move into 19th century that is ! -直到我们进入19世纪!

thanks for any advice ! 感谢您的任何建议!

For others ...... Here is the jquery function that enables simply adding "expand" to the class of the textbox. 对于其他人......这是jquery函数,可以简单地将“ expand”添加到文本框的类。 full article here : http://www.sitepoint.com/build-auto-expanding-textarea-2/ 全文在这里: http : //www.sitepoint.com/build-auto-expanding-textarea-2/

 (function ($) { // jQuery plugin definition $.fn.TextAreaExpander = function (minHeight, maxHeight) { // var hCheck = !($.browser.msie || $.browser.opera); var hCheck = !( $.browser.opera); // resize a textarea function ResizeTextarea(e) { // event or initialize element? e = e.target || e; // find content length and box width var vlen = e.value.length, ewidth = e.offsetWidth; if (vlen != e.valLength || ewidth != e.boxWidth) { if (hCheck && (vlen < e.valLength || ewidth != e.boxWidth)) e.style.height = "0px"; var h = Math.max(e.expandMin, Math.min(e.scrollHeight, e.expandMax)); e.style.overflow = (e.scrollHeight > h ? "auto" : "hidden"); e.style.height = h + "px"; e.valLength = vlen; e.boxWidth = ewidth; } return true; }; // initialize this.each(function () { // is a textarea? if (this.nodeName.toLowerCase() != "textarea") return; // set height restrictions var p = this.className.match(/expand(\\d+)\\-*(\\d+)*/i); this.expandMin = minHeight || (p ? parseInt('0' + p[1], 10) : 0); this.expandMax = maxHeight || (p ? parseInt('0' + p[2], 10) : 99999); // initial resize ResizeTextarea(this); // zero vertical padding and add events if (!this.Initialized) { this.Initialized = true; $(this).css("padding-top", 0).css("padding-bottom", 0); $(this).bind("keyup", ResizeTextarea).bind("focus", ResizeTextarea); } }); return this; }; })(jQuery); // initialize all expanding textareas jQuery(document).ready(function () { jQuery("textarea[class*=expand]").TextAreaExpander(); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM