I want to adjust the height according to the amount of text is pushing into the textbox.
It is virtually impossible (or at least not practical) to obtain height required to show the text in the textarea element (TestBox with TextMode="MultiLine"). Therefore, I suggest that you use the div element and Literal control instead.
Shown below is a sample:
.aspx.cs
using System;
using System.Data;
namespace WebForms1
{
public partial class WebForm42 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = CreateData();
GridView1.DataBind();
GridView2.DataSource = CreateData();
GridView2.DataBind();
}
}
protected DataTable CreateData()
{
var table = new DataTable();
table.Columns.Add(new DataColumn("id", typeof(int)));
table.Columns.Add(new DataColumn("name", typeof(string)));
table.Columns.Add(new DataColumn("descr", typeof(string)));
var row = table.NewRow();
row["id"] = 1;
row["name"] = "aaa";
row["descr"] = "Multiline asp:textbox (into a asp:gridview) auto adjust " +
"it's height according to the amount of text";
table.Rows.Add(row);
row = table.NewRow();
row["id"] = 2;
row["name"] = "bbb";
row["descr"] = "I would like to consult about a issue I am facing.";
table.Rows.Add(row);
row = table.NewRow();
row["id"] = 3;
row["name"] = "ccc";
row["descr"] = "I have a gridview where there is a textbox that is filled " +
"from a SQL DB when such info is called from a click event, " +
"but I want to adjust the height according to the amount of " +
" text is pushing into the textbox. I have tried with VB.NET " +
" and JavaScript but it does not work.";
table.Rows.Add(row);
return table;
}
}
}
.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm42.aspx.cs"
Inherits="WebForms1.WebForm42" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
div.style1 {
font-size: 14px;
width: 520px;
margin-left: 48px;
overflow: auto;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h2>textarea (TestBox with TextMode="MultiLine")</h2>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:TemplateField HeaderText="description">
<ItemTemplate>
<asp:TextBox ID="descr" runat="server" Text='<%# Eval("descr")%>'
MaxLength="430" ReadOnly="true" BorderWidth="0px"
BorderColor="transparent" BackColor="#FFFFFF"
ForeColor="#283A52" Font-Size="14px" Width="520px"
Height="80px" Style="margin-left: 48px; resize: none; object-fit:
scale-down; border-radius: 0px; padding-left: 0px; padding-top: 0px"
Font-Names="arial" TextMode="MultiLine">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<h2>div tag instead of textarea</h2>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="name" HeaderText="name" />
<asp:TemplateField HeaderText="description">
<ItemTemplate>
<div class="style1">
<asp:Literal ID="Literal1"
runat="server"
Text='<%# Eval("descr") %>'>
</asp:Literal>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
Reault