<asp:TextBox id="txtComment" runat="server" Columns="50" MaxLength="50" />
<asp:TextBox id="txtComment2" runat="server" Columns="50" MaxLength="50" />
I have the two textboxes above on a web form. How can I mash them together and make them appear as one text box?
Did you try making a user control or custom control ?
Ideally, create a user control with both your text-boxes and apply the following styling so that they appear together.
First Text box
border-right-width: 0 px
Second Text box
border-left-width: 0 px
You can try putting them right next to each other on the page and styling their borders so they either have no border or the touching sides have no borders.
#txtComment, #txtComment2 {margin:0;padding:0; display-inline; border:none}
Assuming what you actually want is an imperceptible difference between the first and second fields: why not make one textbox, and split it server-side; e.g.
<asp:TextBox id="txtComment" runat="server" Columns="50" MaxLength="50" Visible="false" />
<asp:TextBox id="txtComment2" runat="server" Columns="50" MaxLength="50" Visible="false"/>
<asp:TextBox id="shownTxtComment" runat="server" Columns="100" MaxLength="100"/>
private void splitComment()
{
txtComment.Text = String.Left(shownTxtComment.Text, 49); //first 50 characters
txtComment2.Text = String.Mid(shownTxtComment.Text, 50); //characters 51 thru end
}
call splitComment() in your postback function, and you'll keep the hidden fields up-to-date on every postback.
You might also consider doing the same thing client-side with javascript.
*a cleaner approach would be to remove the txtComment2 web control altogether, set txtComment length to 100, and simply split/handle the substrings on the server, but since it's not altogether clear why you actually want two separate textboxes to look like one textbox, I can't say whether that addresses your need.
I guess I am curious as to why you would want to do this. As Thomas mentioned, it seems to go against usability. Are you looking for something like TextArea
Related
I have a problem with the trigger of the OnCheckedChanged. When I click the checkbox (switch toggle), the OnCheckedChanged is not firing. I already tried many solutions like trigger it in JS part but no luck.
Below is the checkbox code:
<input type="checkbox" data-toggle="toggle" data-onstyle="info" data-offstyle="secondary" data-on="Ja" data-off="Nee" data-size="xs" runat="server"
oncheckedchanged="showProductFoto_CheckedChanged" AutoPostBack="true"
style="margin-top: -5px;" runat="server" id="showProductFoto" onchange="IncludeWithoutPhotos(this)" ClientIDMode="Static"/>
Well, then get the css working for the check box, and then you back to using the standard asp.net controls - and they are a pure joy to use, and easy to use.
So, say I have this check box:
<asp:CheckBox ID="CheckBox1"
runat="server" Text="Confirm to send email"
OnCheckedChanged="CheckBox1_CheckedChanged" >
We get this ugly thing:
so, I can see your motivation to want to dump that ugly above.
but, a check box gets renderd as a html input (type = checkbox), and also puts in a label for you.
So, you can style it anyway you want.
Say, like this:
<style>
.bigcheck input {width:28px;height:28px;cursor:pointer;
box-shadow: 5px 5px 5px grey}
.bigcheck label {position:absolute;margin-left:15px;margin-top:10px}
</style>
<asp:CheckBox ID="CheckBox1" CssClass="bigcheck"
runat="server" Text="Confirm to send email"
OnCheckedChanged="CheckBox1_CheckedChanged" />
And now we get this:
so, you still have quite much unlimited css you can apply against that check box.
in an old ASP.NET Web Forms application I got this warning in the aspx code of a page:
Attribute 'fullname' is not a valid attribute for element 'input'
Why was written in this way?
And there is a way to resolve it?
You only get a warning. The simple issue is that the developer just made up a attribute, and shoved in some value. this is legal, and I often do this for say a standard text box.
so, I might for example place 5 or 10 text boxes and controls inside of a div, say like this:
<div id="EditRecord" runat="server" style="float:left;display: normal;border:solid 2px;padding:5px">
<div style="float:left" class="iForm">
<label>HotelName</label>
<asp:TextBox ID="txtHotel" runat="server" f="HOtelName" width="280" /> <br />
<label>First Name</label>
<asp:TextBox ID="tFN" runat="server" f="FirstName" Width="140" /> <br />
<label>Last Name</label>
<asp:TextBox ID="tLN" runat="server" f="LastName" Width="140" /> <br />
<label>City</label>
<asp:TextBox ID="tCity" runat="server" f="City" Width="140" /> <br />
<label>Province</label><asp:TextBox ID="tProvince" runat="server" f="Province" Width="75"></asp:TextBox> <br />
</div>
etc. etc. etc.
Note in above, I wanted to define what data base column for a routine to "fill out" the above controls.
So, I have a routine I call with a data row.
Call fLoader(EditRecord, rstData.Rows(0))
the above routine looks for any control with a "f=datacolum", and fills out the controls for me.
So, I get this:
In other words, my custom "f" attribute allows me to pull data from a database, and fill out the web page - and without having to write new code each time. (and I have a reverse routine - writes out all controls with f="some data column" back to the database. With this simple concept, then I am able to have any web page read/write data to/from the database - and not have to write that same code over and over. In effect, I get a whole crud system, and all achieved by a simple concept of adding a "made up" attribute "f" that defines the data base column to use.
So, you can in VS have that warning suppressed, but I as noted OFTEN make and add some custom values. Even for a button in a grid view, you might have this:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button ID="cmdView" runat="server" Text="Button"
OnClick="cmdView_Click"
ProductID = '<%# Eval("ProductID") %>'
CustomerID = '<%# Eval("CustomerID") %>'
/>
And then in code behind, you have this:
Protected Sub cmdView_Click(sender As Object, e As EventArgs)
Dim btn As Button = sender
Debug.Print(btn.Attributes("CustomerID").ToString())
Debug.Print(btn.Attributes("ProductID").ToString())
So, we often just make up some attribute, and it is legal to do so in most cases.
So, that button - say dropped into a grid view now can pass values to the button click - even values not displayed in the gridview.
So, you can just as a general rule ignore the warning. It is not super common, but a lot of developers often will add and make up their own attributes, and client side code (JavaScript) or even code behind as above shows is then free to use the values in those custom, or "made up" attributes. In most cases, you get a warning, but other then that, it not a huge deal nor issue. You of course do have to be carful, since such custom attributes in most cases don't have automatic view state, and their values will not persist correctly for a round trip (post-back, and page return). However, for expressions or values such as above example, then its not a problem.
When I use autocompletion in VisualStudio 2010 within my .aspx application, there are different default completions at closing control tags:
<asp:CheckBox />
<asp:Label></asp:Label>
Is there a explaination for this behaviour?
<asp:CheckBox></asp:CheckBox>
<asp:Label />
Wouldn't be invalid.
This is because ASP.NET's Label control is decorated with the ParseChildrenAttribute, with ParseChildren(false) while CheckBox isn't.
You can support the same behavior whith your custom controls, for example, with the following code, Visual Studio will behave like Label if you use MyControl in the web form editor:
[ParseChildren(false)]
public class MyControl : WebControl
{
...
}
The label closing is like that
<asp:Label runat="server"></asp:Label>
because usually we type something between
<asp:Label runat="server" ID="lblOne">better start programming now</asp:Label>
that is not the case for checkbox, that we type inside of it
<asp:CheckBox runat="server" Text="enable" ID="cbOne" />
We have on both elements the Text field, why on the one we prefer to write out side... Look at this example, on Label, or On other similar controls the text that we may have to write may include characters that are not allowed inside the Text Property, maybe a complex css style or what ever... The check box from the other side is only include a small text (yes, not, something like that)
<asp:Label ID="lblLabel" runat="server">
This is a <b>"label"</b>
<br />And one more line
</asp:Label>
and more example that compiles
<asp:Label ID="lblLabel" runat="server">
This is a <b>"label"</b>
<br />And one more line
<asp:Literal runat="server" ID="ltrOneMore">One more Control Inside</asp:Literal>
</asp:Label>
---but this is not compile--
<asp:Label ID="lblLabel2" runat="server"
Text="This is a <b>"label"</b>
<br /> and one more line"
/>
At the final end is a decision that the makes make - maybe we need to ask them for the real real reason.
Now this is also not compile
<asp:CheckBox runat="server" ID="cbMyChbx">one<asp:CheckBox>
check box when is render on page is use two controls, one input and one label, so they maybe need to help user to understand that the text is not going on the input control.
<asp:CheckBox />
Because the element has no content, you can close the tag with /> instead of using a separate closing tag
<asp:Label></asp:Label> or <asp:Label />
Displays static text on a Web Forms page and allows you to manipulate it programmatically.
Learn more about it Web Server Control
All the answers above are valid, but something additional. All the asp controls are eventually rendered as HTML controls and that also defines how the asp controls behave. For e.g. it is not necessary that text in a label is always set as
<asp:Label runat="server" ID="lblOne">better start programming now</asp:Label>
it can be also done as follows
<asp:Label runat="server" ID="lblOne" Text="better start programming"></asp:Label>
now both are correct format, so it is not valid to say that any control which needs content will have a separate closing tag. It also depends on how it rendered in HTML. for e.g by default asp Label is rendered as a span and doesnt conform to XHTML standards. Hope this makes it clear, always think of how it will be rendered and ASP tries to adhere to what eventually will be rendered.
cheers
I'm making a custom module for mojoPortal CMS which needs to allow the client to add an affiliate into the database. As far as I can tell, this requires creating a .ascx file and then installing that using the administration toolbar in the Web interface to get it to a point where I can put it into a page, as http://www.mojoportal.com/hello-world-developer-quick-start.aspx.
The form is simple enough, but the values in the text boxes just stay empty when I submit, though the file upload works fine. The code:
<asp:Label ID="Label1" runat="server" Text="Company Name" AssociatedControlID="CompanyName">
</asp:Label>
<asp:TextBox ID="CompanyName" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Company Description" AssociatedControlID="CompanyDescription"></asp:Label>
<asp:TextBox ID="CompanyDescription" TextMode="MultiLine" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="Company Logo" AssociatedControlID="CompanyLogo"></asp:Label>
<asp:FileUpload ID="CompanyLogo" runat="server" />
<asp:Button ID="SubmitButton" runat="server" Text="Add Affiliate" />
EnableViewState for the page and the controls is enabled
The text box is not set to ReadOnly, and there is no funky JavaScript dynamically modifying elements (at least, I didn't set any).
I can work around this by using HTML elements, and get the values using Request.Form. The information is actually there, I can see it in the Request.Form, but I would have to get that by something like Request.Form[CompanyName.ClientId.Replace("_","$")] or Request.Form[6] which both seem very messy and IIRC aren't really the way things are supposed to roll in .NET. Besides, having worked until 3 last night, I really want to know what the answer is now!
Any thoughts anyone?
What I had done was not created a click event for the button, relying on the fact that it was posting to the server (like I would in PHP). Oops! When I added a click event, then the text boxes retained their value when I was within that method.
I want to keep TextBox attribute Read Only in web form. But when I keep this Read Only , On PostBackits values get vanishes and I get empty text. How can I achieve the same functionality without loosing TextBox values.
You can achieve this by preventing user to enter values in the textbox, thus add following attributes to the textbox. It will maintain values even on postback also.
onkeypress="return false;"
Change it by removing the ReadOnly=”true” from the tag , we will add it in the code.
Now in the code add the following :
TextBox1.Attributes.Add(“readonly”, “readonly”);
You probably have it like this right now:
<asp:TextBox ID="MyTextBox" runat="server" Enabled="false" />
Just change this to:
<asp:TextBox ID="MyTextBox" runat="server" ReadOnly="true" />
And it will send its value over postback like all other form elements while still being read only.