Automatic TextBox Width - asp.net

There is a similar thread about this. But I want to have a multiline TextBox with automatic width (fit width to the larger row).
With this code I can have a multiline TextBox (automatic height)
<div style="float:left; white-space:nowrap ">
<asp:TextBox style="display:inline; overflow:hidden"
ID="txt1"
runat="server"
Wrap="false"
ReadOnly="true"
TextMode="MultiLine"
BorderStyle="none"
BorderWidth="0">
</asp:TextBox>
</div>
<div style="float:left">
<asp:TextBox ID="txt2" runat="server" Text="Example textbox"></asp:TextBox>
</div>
Code behind:
txt1.Rows= text.Split("|").Length ' Sets number of rows but with low performance
txt1.Text = text.Replace("|", Environment.NewLine)
Once again, thanks for your help.

You could try a linq approach:
string[] rows = text.Split('|');
int maxLength = rows.Max(x => x.Length);
txt1.Rows = rows.Length;
txt1.Columns = maxLength;

If you are open to using plugins like jquery,
you should look at autoresize plugins.
These will resize as a user types too.
Check out one autoresize
$(document).ready(function(){
$('textarea').autosize();
});

Joel Etherton give me a real good working code example of how to resolve this using Linq, but unfurtenly I can't use Linq.
Multiline TextBox auto Width using Linq (Joel Etherton's solution):
C#
string[] rows = text.Split('|');
int maxLength = rows.Max(x => x.Length);
txt1.Rows = rows.Length;
txt1.Columns = maxLength;
VB
Dim rows() As String = text.Split("|")
Dim maxLength As Integer = rows.Max(Function(x) x.Length)
txt1.Rows = rows.Length
txt1.Columns = maxLength
text = text.Replace("|", Environment.NewLine)
txt1.Text = text
Multiline TextBox auto Width solution 2
To achieve this "manually", I did this method to know the larger row's lenght. Is not the most efficiente one, but It worked for me:
Dim textRows() As String = text.Split("|")
For Each row As String In textRows
row = row.Trim
textToDisplay = String.Format("{0}{1}{2}", textToDisplay, row, Environment.NewLine)
If row.Length > maxRowLenght Then
maxRowLenght = row.Length
End If
Next
txt1.Rows = textRows.Length
txt1.Columns = maxRowLenght
txt1.Text = textToDisplay

Related

How to add attributes for a textbox column in gridview ? vb.net

When a user click in my text box i want to put value OK in text box, when he action double click i want to punt NOK in textbox. I know that is possible with attributes but how?
I have my column in grid view like this:
<asp:TemplateField HeaderText = "Rating" >
< ItemTemplate >
< asp:TextBox ID = "BOX_Rating" runat="server" Text='<%# Bind("Rating") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
I added values into my textbox like this (and to my table):
dr = dt.NewRow()
dr("Item_number") = Paragraphorder
dr("Station") = ActivityResource
dr("Verified_characteristic") = Description
dr("Measurement_techinque") = ActivityName
dr("Specification") = RequirementNom
dr("Frequency") = Frequency
dr("Responsible_person") = "VMO"
dr("Rating") = "OK"
dr("Result_1") = "OK"
Everything is good but when i try to put attributes for my textbox doesn't work.
I try this but doesn't work:
For i = 0 To GridView1.Rows.Count - 1
Dim Rating_BOX As TextBox = DirectCast(GridView1.Rows(i).FindControl("BOX_Rating"), TextBox)
Rating_BOX.Attributes.Add("onclick", "this.value = 'OK';")
Rating_BOX.Attributes.Add("ondblclick", "this.value = 'NOK';")
Next
My table is like this:
I want when a user click into textbox to put ok and when action double click to put NOK but my code doesn't react when i click into textbox.

asp checkboxlist item not rendering correct text

I use asp checkboxlist to have that result
But using html tag as item of the checkboxlist , asp is interpreting it as html. It works for simple text. Here is my result.
and here is the declaration and binding method
<asp:CheckBoxList ID="chklstreponse" runat="server">
</asp:CheckBoxList>
DataTable dtreponse = gq.GetRandom_Responses(Convert.ToInt32(idquest.Value));
chkList.DataSource = dtreponse;
chkList.DataTextField = "libelle";
chkList.DataValueField = "id";
chkList.DataBind();
I think you need to HtmlEncode the values in the RadioButtonList.
System.Net.WebUtility.HtmlEncode("<html>")
But you are binding a datatable directly, you either must do it in the source of the DataTable or loop all rows and encode them.
foreach (DataRow row in dtreponse.Rows)
{
row["libelle"] = System.Net.WebUtility.HtmlEncode(row["libelle"].ToString());
}
chkList.DataSource = dtreponse;
chkList.DataTextField = "libelle";
chkList.DataValueField = "id";
chkList.DataBind();
Try this.HtmlEncode makes sure that text is displayed correctly in the browser and not interpreted by the browser as HTML.
<asp:CheckBoxList ID="chklstreponse" runat="server"> </asp:CheckBoxList>
DataTable dtreponse = gq.GetRandom_Responses(Convert.ToInt32(idquest.Value));
chkList.DataSource = dtreponse;
chkList.DataTextField = Server.HtmlEncode("libelle");
chkList.DataValueField = "id";
chkList.DataBind();

Generate random alphanumeric string into password field

I want to generate a random alphanumeric string into password field when click a button.
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
code behind the button
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var stringChars = new char[8];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
var finalString = new String(stringChars);
txtPassword.Text = finalString.ToString();
this execute without any error.. but does not appear any text in textbox.
When I use
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
This works properly..
I have no any idea why my code is not working..
Please help me to solve this problem...
If you want to add text to password mode textfield then you need to write.
txtPassword.Attributes.Add("value", finalString.ToString());
Suggest to add using attributes
txtPassword.Attributes.Add("value", finalString.ToString());
please refer below link for more information on this.
http://www.tyronedavisjr.com/2008/05/23/aspnet-and-password-textbox-with-initial-value/

Telerik Radgrid How to add textbox column from code behind?

I am adding columns to RadGrid from code behind. In NeedDataSource event, I am binding a DataTable(with 10 columns) to the radgrid.
Everything's working well till here. But I would like to have text boxes in 2 columns(on load itself, not just in edit mode).
<telerik:RadGrid ID="RadGrid1" runat="server" ShowHeader="true"
OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender"
AutoGenerateColumns="true" >
<MasterTableView>
</MasterTableView>
</telerik:RadGrid>
If done declarative, the column definition shall be like this. But I want it accomplished from code behind.
<telerik:GridTemplateColumn HeaderText="Qty">
<ItemTemplate>
<input id="<%# this.GetUniqueId("Qty", Container.DataItem)%>" name="<%# this.GetUniqueId("Qty", Container.DataItem)%>" type="text" value="<%# Eval("Quantity")%>" size="2" maxlength="3" />
</ItemTemplate>
</telerik:GridTemplateColumn>
Create TemplateColumn like any other column type and set template object to ItemTemplate (and you can do same for HeaderTemplate and FooterTemplate). But you have to define custom template class witch will implement ITemplate.
You can find an example here :
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section4
Must you use a template column? If you can directly bind your columns to a datasource field, use the GridNumericColumn. This can be dynamically added from the code behind:
GridNumericColumn numColumn = new GridNumericColumn();
numColumn.UniqueName = "ColumnId";
numColumn.MaxLength = 20;
numColumn.HeaderText = "My Numeric Column";
numColumn.DataField = "Qty";
numColumn.DataFormatString =
myGrid.MasterTableView.Columns.Add(numColumn);
Use This.
GridTemplateColumn tempCol;
for (int i = 0; i < obj.Count; i++)
{
tempCol = new GridTemplateColumn();
this.gvwRejection.MasterTableView.Columns.Add(tempCol);
tempCol.ItemTemplate = new DynamicTemplateCoulmn"txtCategoryQty"+ , "numericTextBox");
tempCol.HeaderText = objRejectionCategoryMasterObject[i].CategoryName.Trim();
tempCol.UniqueName = "CategoryQty" + i;
tempCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
RejCategoryIDs[i] = objRejectionCategoryMasterObject[i].RejCategoryID;
}
tempCol = new GridTemplateColumn();
this.gvwRejection.MasterTableView.Columns.Add(tempCol);
tempCol.ItemTemplate = new DynamicTemplateCoulmn("txtTotal", "numericTextBoxReadOnly");
tempCol.HeaderText = "Total";
tempCol.UniqueName = "Total";
tempCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.FooterStyle.HorizontalAlign = HorizontalAlign.Right;

Create labels dynamicly on ASP.NET (VB)

I want to create labels in my page dynamicly, for example the user will choose in a textbox the number of labels, and I will display the number of this label with .text = "XYZ".
Thanks.
The quick and dirty method (this example adds 10 labels and literals to a PlaceHolder on an ASP.NET page:
Dim c As Integer = 0
While c < 10
Dim lab As New Label()
Dim ltr As New Literal()
lab.Text = c.ToString()
ltr.Text = "<br/>"
PlaceHolder1.Controls.Add(lab)
PlaceHolder1.Controls.Add(ltr)
C+=1
End While
Look at using a Repeater control:
Using the ASP.NET Repeater Control
Data Repeater Controls in ASP.NET
There's a number of things that will need to be done to make this work, but to simply dynamically create controls and add them to the page, you will need a Placeholder on your ASPX page:
<asp:TextBox ID="txtLabelCount" runat="server" />
<asp:Button ID="btnCreate" runat="server" Text="Create" /><br />
<asp:Placeholder ID="PlaceHolder1" runat="server" />
Then, in btnCreate's click event handler:
' Number of labels to create. txtLabelCount should be validated to ensure only integers are passed into it
Dim labelCount As Integer = txtLabelCount.Text
For i As Integer = 0 To labelCount - 1
' Create the label control and set its text attribute
Dim Label1 As New Label
Label1.Text = "XYZ"
Dim Literal1 As New Literal
Literal1.Text = "<br />"
' Add the control to the placeholder
PlaceHolder1.Controls.Add(Label1)
PlaceHolder1.Controls.Add(Literal1)
Next

Resources