ASP.NET - Setting Height & Width to a multiline TextBox as per the data in it
Asked By Muzaffar Mohammed on 26-Oct-10 08:39 AM
I have a MultiLine TextBox to which I add text like this:
TextBox tb4 = new TextBox();
tb4.TextMode = TextBoxMode.MultiLine;
tb4.BorderWidth = 0;
tb4.Attributes.Add("style","overflow :hidden");
tb4.Text = //data form database
Every time i execute the code, i'll get different data with different size(Width & Hieght)
How can I set the height & width of the TextBox to be exactly right to display all the content?
Nowshad M replied to Muzaffar Mohammed on 26-Oct-10 08:49 AM
Hi,
Try this
TextBox tb4 = new TextBox();
tb4.TextMode = TextBoxMode.MultiLine;
tb4.BorderWidth = 0;
tb4.Style.Add("Height",height);//height=string which holds ur custom value
tb4.Style.Add("Width",width);//width=string which holds ur custom value
Vasanthakumar D replied to Muzaffar Mohammed on 26-Oct-10 11:31 AM
Hi,
you can do this pro grammatically by calculating rows and colums...
TextBox tb4 = new TextBox();
tb4.TextMode = TextBoxMode.MultiLine;
tb4.BorderWidth = 0;
tb4.Attributes.Add("style","overflow :hidden");
string strText = //data form database;
tb4.Text = strText;
string[] strArr = strText.Split("\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (strArr.Length == 0)
{
tb4.Rows = 1;
}
else
{
int iColSize = 0;
foreach (string str in strArr)
{
if (iColSize < str.Length)
{
iColSize = str.Length;
}
}
tb4.Rows = strArr.Length + 1;
tb4.Columns = iColSize;
}
Regards,
Vasanth
sri sri replied to Muzaffar Mohammed on 26-Oct-10 11:52 AM
hi,
instead of textbox, use literal control in your page.
<asp:Literal id="lit" runat="server" />
// codebehind
lit.Text = "Your text here"
Muzaffar Mohammed replied to sri sri on 27-Oct-10 12:24 AM
sri sri replied to Muzaffar Mohammed on 27-Oct-10 11:59 AM
bebikash jee replied to Vasanthakumar D on 02-Dec-10 06:16 AM
its working but my textbox width is fixed
help