Server mark error in ID - c#

Can someone please tell me where i'm wrong ?
<asp:TextBox ID="txtComentario_<%# Eval('Id').ToString() %>"
TextMode="MultiLine" runat="server"></asp:TextBox>
Error
Parser Error Message: Make the server is not formed correctly.

try the following
<asp:TextBox ID='<%# Eval("Id","txtComentario_{0}") %>' TextMode="MultiLine" Text="" runat="server"></asp:TextBox>

The ID property of a control can only be set using the ID attribute in the tag

you should use Bind instead of Eval ,The Bind method is typically used with input controls such as the TextBox .
Try like :-
<asp:TextBox ID='txtComentario_<%# Bind("Id") %>' TextMode="MultiLine" runat="server"></asp:TextBox>

If you want to add extra information to your textbox, you can add attribute to it.
TextBox1.Attributes.Add("customAttribute", "value");

Related

asp.net access control's name in another control without using code behind

I know this is very simple & so silly for me to ask.
Forgot what needs to be done.
How do i set the name property of txtNewPwd control as an attribute to txtConfPwd control's data-bv-identical-field attribute.
I do not want to use code-behind. tried #,=
<asp:TextBox ID="txtNewPwd" runat="server" CssClass="form-control " placeholder="New Password" TextMode="Password" required ClientIDMode="Static"
data-bv-stringLength="true" data-bv-stringLength-min="5" data-bv-stringLength-max="72" data-bv-stringLength-message="Enter 5-72 characters" MaxLength="72"
data-bv-notempty="true" data-bv-notempty-message="Enter New Password"></asp:TextBox>
<asp:TextBox ID="txtConfPwd" runat="server" CssClass="form-control " placeholder="Confirm Password" TextMode="Password" required
data-bv-stringLength="true" data-bv-stringLength-min="5" data-bv-stringLength-max="72" data-bv-stringLength-message="Enter 5-72 characters" MaxLength="72"
data-bv-notempty="true" data-bv-notempty-message="Enter New Password"
data-bv-identical="true"
data-bv-identical-field="<%# txtNewPwd.UniqueID %>"
data-bv-identical-message="The password and its confirm are not the same."
></asp:TextBox>
You could use jQuery if you do not want to use code behind.
<script type="text/javascript">
var name = $("#<%= txtNewPwd.ClientID %>").attr("name");
$("#<%= txtConfPwd.ClientID %>").attr("data-bv-identical-field", name);
</script>
You could also use
<asp:TextBox ID="txtConfPwd" runat="server" data-bv-identical-field='<%# txtNewPwd.UniqueID %>'></asp:TextBox>
This however requires DataBind(); to be called in code behind, and you don't want that.

Date Binding in TextBox in ListView

I have a ListView and in it there is a field with some date coming from the database. Now when I click on the Edit button, in the next Edit Page I want to show the exact date in the particular date field.
See the following screenshots:
Main View:
After clicking:
I have written the following code to bind the date:
<asp:TextBox runat="server" Text='<%#Bind("FirstVisitDate", "{0:MM/dd/yyyy}") %>' ID="FirstVisitDate" CssClass="form-control" TextMode="date" />
But this is not working.
Use this one instead of yours:
<asp:TextBox runat="server"
Text='<%# String.Format("{0:MM/dd/yyyy}", Eval("FirstVisitDate")) %>'
ID="FirstVisitDate" CssClass="form-control" TextMode="date" />
If you are still getting error you can convert it to DateTime see if it works:
Text='<%# String.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(Eval("FirstVisitDate")) %>'
also we don't have TextMode="date" ?? remove that one as well.
You could also try this way, which is way better than Eval in terms of performance:
Text='<%# String.Format("{0:MM/dd/yyyy}", ((System.Data.DataRowView)Container.DataItem)["FirstVisitDate"] %>'
if you want to show the format of the date, you should use placeholder instead like this:
also you could use type as a date like below:
<asp:TextBox runat="server" placeholder="mm/dd/yyyy" type="date" ... />
When using TextMode="date" you should always use the format yyyy-MM-dd (W3C standard)
<asp:TextBox Text='<%# Bind("data", "{0:yyyy-MM-dd}")%>' runat="server" ID="dataTextBox" TextMode="Date" CssClass="form-control"/>

unable to set value from server-side to a Textbox in client side

My code is like this
<asp:TextBox type="text" name="txtEndDate" Text="<%#library.GetDictionaryItem("ProfilePages_CVR nummer")%>" runat="server"></asp:TextBox>
Everything looks okay to me, But i don't know why its throwing an error
The server tag is not well formed.
Use single quotes like:
<asp:TextBox type="text" name="txtEndDate" Text='<%#library.GetDictionaryItem("ProfilePages_CVR nummer")%>' runat="server"></asp:TextBox>
The issue is that because of the ASP.net brackets which contain double quotes inside them
<asp:TextBox Text="<%# someMethod("someValue") %>" />
inside the Text field, you need to use single quotes instead of double quotes on that property, like this:
<asp:TextBox type="text" name="txtEndDate"
Text='<%# library.GetDictionaryItem("ProfilePages_CVR nummer")%>'
runat="server">
</asp:TextBox>
And it will work.
Note also that you are using the DataBinding notation ( <%# ) which will only work if your TextBox is inside a DataBound control, or you call DataBind on the control or page containing this TextBox.

Accessing codebehind field values through ASP.NET HTML code

simple question
in code behind(.cs) we have
string error="11000114S";
in ASP (.aspx) we have:
<asp:TextBox ID="text" runat="server" EnableViewState="false" AutoPostBack="false"/>
<asp:RequiredFieldValidator id="textValidator" runat="server" controlToValidate="text"
errormessage=(our string error="11000114S")>
So how to do this, assign value from cs-> saved error list to html ?
It is as simple as adding this to your .cs .. if I understand you correctly
string error="11000114S";
textValidator.ErrorMessage = error ; // or what ever you want
this should do
protected string error="11000114S";
<asp:TextBox ID="text" runat="server" EnableViewState="false" AutoPostBack="false"/>
<asp:RequiredFieldValidator id="textValidator" runat="server" controlToValidate="text"
errormessage='<%# error %>'>
You can assign error messages dynamically to your validators in your code behind. I.e.:
this.textValidator.ErrorMesssage = error;

Error handling when inserting a null value, asp.net c#

i have a webpage built in asp.net c#. it always a user to create a new record in a db table. there are there are two input fields, text and score. text cannot be a null value so if the user doesn't input text onsubmit, the page errors out. i want to throw in some simple error handling code in the code behind page. i've tried including an if/else on_inserted method but ran into some java script errors. any help would be apprieciated. thanks.
aspx page -----------------
<EditItemTemplate>
<customEditors:EditorWithCustomButtons_1 runat="server" ID="Editor1" Content='<%# Bind("userText") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<customEditors:EditorWithCustomButtons_1 runat="server" ID="Editor1" Content='<%# Bind("userText") %>' />
</InsertItemTemplate>
why not use RequiredFieldValidator validator? it works inside the grid
<asp:RequiredFieldValidator id="RequiredFieldValidator2"
ControlToValidate="TextBox1"
Display="Static"
ErrorMessage="*"
runat="server"/>
Also don't forget to use page is valid before saving to the database.
if (page.isValid){
//send to db
}

Categories

Resources