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;
Related
I am trying to access a session variable in aspx but I think there is an issue with my syntax somewhere.
Under page load:
Session["UserName"]= username.Substring(8).ToString(); // This is ok as far as I know.
Issue is with the asp bit:
<asp:TextBox ID="ATextBox" runat="server" Text="<% Session["UserName"] %>" />
<asp:TextBox ID="ATextBox" runat="server" Text="<%# Session["UserName"] %>" />
I have also tried the above (one at a time) with .ToString() at the end too. But I keep getting errors:
Either The server tag is not well formed or the server tag contains % %
One thing that might make a difference is the Textbox is inside a GridView, (it is inside a ContentTemplate)but not a boundfield.
Try this:
<asp:TextBox ID="ATextBox" runat="server" Text='<%# Session["UserName"] %>' />
Not sure if it would help..
I'm trying to set a maximum length limit in a text area using a regex validator (if there is a better way to do this, please let me know). This is an ASP.Net webforms user control. The current code I have is
<asp:Panel ID="labeledTextBox" runat="server">
<asp:Label ID="label" CssClass="label" ClientIDMode="Static" runat="server"/>
<asp:TextBox ID="textBox" CssClass="labeledTextBox" TextMode="MultiLine" ClientIDMode="Static" runat="server"/>
<asp:Label ID="textBoxLengthLabel" CssClass="textBoxLengthLabel" ClientIDMode="Static" runat="server">Maximum <%= MaxLength %> characters</asp:Label>
<asp:RegularExpressionValidator ID="textboxLengthValidator" Display="Dynamic" ControlToValidate="textBox" ValidationExpression="^[\s\S]{0,<%= MaxLength %>}$" ErrorMessage="Test Message" ClientIDMode="Static" runat="server" />
</asp:Panel>
MaxLength is defined in the codebehind file as
public int MaxLength { get; set; }
The value I set shows up in the textBoxLengthLabel properly as, for example, Maximum 500 characters.
However, it is taken literally in the attribute for the validator. The markup on the client side is
<SPAN id=textboxLengthValidator
style="COLOR: red; DISPLAY: inline"
controltovalidate="textBox"
errormessage="Test Message"
validationexpression="^[\s\S]{0,<%= MaxLength %>}$"
isvalid="false"
display="Dynamic">Test Message</SPAN>
How can I use the property in the code-behind file in the attribute?
In the code behind on the page load or on submit you can simply add the atribute
textBox.Attributes.Add("maxlength", MaxLength.ToString());
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");
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
}
I'm having a bit of difficulty setting a variable from the code behind and utilising it in the ASP.NET page (setting it as the value inside a textbox). My webpage simply errors and says it does not exist in the current context. The variable is declared and set all in the Page_Load method.
Here is the relevant ASP.NET code. I assume you will not need to see the code behind, as I have tested outputting the variable via the codebehind (using Response.Write) and that works fine.
<asp:TemplateField HeaderText="Initial Path"
SortExpression="Initial_Path">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server"
Text='<%# initialPath %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox8" runat="server"
Text='<%# initialPath %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# initialPath %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Thanks :)
EDIT: Ok sure, here's the relevant parts of the code behind
string schedID = sched.SchedulerInstanceId;
JobDetail jobDetail2 = sched.GetJobDetail(Request.QueryString["JOB_NAME"], "sched1");
JobDataMap dataMap2 = jobDetail2.JobDataMap;
initialPath = dataMap2.GetString("initialPath");
Response.Write(initialPath);
The response.write is for debugging - it outputs the variable correctly so the variable is actually set
EDIT 2: Here is the code behind
public partial class EditJobDetails : System.Web.UI.Page
{
public string initialPath { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//Scheduler stuff for Quartz.NET, removed because of length
string schedID = sched.SchedulerInstanceId;
JobDetail jobDetail2 = sched.GetJobDetail(Request.QueryString["JOB_NAME"], "sched1");
JobDataMap dataMap2 = jobDetail2.JobDataMap;
initialPath = dataMap2.GetString("initialPath");
}
What's not working is just the <%= initialpath %> in the ASP form. It simply just doesn't show anything, like the variable is assigned nothing
In the codebehind you need to create it as a public property. Then you can use it all you want in the aspx page.
in code behind:
public string yourvar { get; set; }
in aspx:
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server"
Text='<%= yourvar %>'></asp:TextBox>
</EditItemTemplate>
What I don't see here is where the call to DataBind() is made. If it's made before is set then <%#initialPath%> will write its starting value (null, which gets written as an empty string in such a case). If it's not made at all, then <%#initialPath%> will never write anything. You need to make sure DataBind() is called at an appropriate time, such as at the end of Page_Load
<%# initialPath %> is a databinding expression meaning that ASP.NET will look for a initialPath property on the collection you are binding to. Obviously such property doesn't exist as this is a local variable.