I am trying to set a label to show some text when an error occurs. When I make this error occur, the label throws a NullReferenceException.
Here is the label.Text code from the code behind:
if (userEmail != null)
{
//If the same email exists
pnlError.Visible = Visible;
lblError.Text = "Error: The email you have entered is already assigned to an account.";
}
When I build, I get no errors, which would suggest to me that it is able to find it in the ASPX code.
Here it is in the markup:
<asp:Panel ID="pnlError" runat="server" Visible="false" EnableViewState="false">
<label id="lblError"></label>
</asp:Panel>
As you can see it is wrapped in a panel. I can change the visibility of the panel just fine in the same function as the Label.Text
And here it is defined in the aspx.designer.cs:
protected global::System.Web.UI.WebControls.Panel pnlError;
protected global::System.Web.UI.WebControls.Label lblError;
It is worth mentioning, that whenever I change any other WebControl elements in the markup, such as a button or panel, the aspx.design.cs regenerates, but it fails to include the lblError label. I have tried deleting and then regenerating the design manually to no avail.
Since the label is inside of a panel you need to find it:
if (userEmail != null)
{
//If the same email exists
pnlError.Visible = Visible;
var lblError= ((Label)(pnlError.FindControl("lblError")));
if(lblError != null)
{
lblError.Text = "Error: The email you have entered......";
}
}
Edit:
You better use asp control
<asp:Label ID="lblError" runat="server" ></asp:Label>
then you dont need to find it
pnlError.Visible = Visible;
lblError.Text = "Error: The email you have entered......";
Related
In My Application server side validation function is not working.even function is not called. i have put debugger on thuat function but it is not stopped ny debugger .i.e. function is not called
<asp:TextBox type="text" ID="txtMobilePhone" runat="server" ClientIDMode="Static" CausesValidation="true"/>
<asp:CustomValidator ID="cvMobilePhone" runat="server" OnServerValidate="cvMobilePhone_ServerValidate"
Text="Mobile Phone already exist in this Reward Program." ErrorMessage="Mobile Phone already exist in this Reward Program."
Display="Dynamic" ValidationGroup="vgStep2" ControlToValidate="txtMobilePhone" CssClass="error"></asp:CustomValidator>
<asp:RequiredFieldValidator ID="rfvMobilePhone" runat="server" ControlToValidate="txtMobilePhone"
ErrorMessage="Mobile Phone is required." CssClass="error" ValidationGroup="vgStep2"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="cvMobilePerVal" runat="server" ClientValidationFunction="validateEmailOrMobilePerVal"
Display="Dynamic" ValidationGroup="vgStep2"></asp:CustomValidator>
<asp:Button ID="btnStep2Upper" runat="server" ClientIDMode="Static" OnClick="btnSaveContactClick" Text="Save" ValidationGroup="vgStep2" vg="vgStep2" OnClientClick="return ClientValidate();" />
Server Side Code
protected void cvMobilePhone_ServerValidate(object source, ServerValidateEventArgs value)
{ /* I have put debugger here but control is not coming here*/
/* my validation logic*/
protected void cvMobilePhone_ServerValidate(object source, ServerValidateEventArgs value)
{
if (txtMobilePhone.Text.Trim() != "")
{
RewardProgramDataContext db = new RewardProgramDataContext();
Boolean f = false;
string MobilePhone = cmnFunc.RemoveMobilePhoneFormat(txtMobilePhone.Text.Trim());
if (Request["id"] != null)
{
var cData = db.spContactSelectAllSingle(new Guid(Request["id"])).SingleOrDefault();
if (cData != null)
{
if (cmnFunc.RemoveMobilePhoneFormat(cData.MobilePhone) == MobilePhone)
{
f = true;
value.IsValid = true;
}
}
}
if (f == false)
{
var res = db.spContactDuplicateMobile(new Guid(ddlContactList.SelectedValue), MobilePhone).SingleOrDefault();
if (res.Column1 <= 0)
{
value.IsValid = true;
customIsValid = true;
}
else
{
value.IsValid = false;
customIsValid = false;
}
}
}
}
now when i click submit button all clent side validation working but serside custom validator is not calling
You forget to set the ControlToValidate property?
<asp:CustomValidator ID="cvMobilePhone" runat="server" ControlToValidate="txtMobilePhone" OnServerValidate="cvMobilePhone_ServerValidate"
Text="Mobile Phone already exist in this Reward Program." ErrorMessage="Mobile Phone already exist in this Reward Program."
Display="Dynamic" ValidationGroup="vgStep2" CssClass="error"></asp:CustomValidator>
You have a combination of two different things causing this behaviour.
Firstly, note that although—as has been said by others—you do not have to specify ControlToValidate, doing so restricts the circumstances in which the server-side custom validation event will fire. Specifically, if you leave it unset, the event always fires on postback, whereas if you set it, the event only fires when the control identified by ControlToValidate has a non-empty value.
Secondly, by specifying OnClientClick, you are telling the framework that you will take care of client-side validation, which will now not fire unless you call it from your OnClientClick function. Although you have not included your ClientValidate function in your question, I suspect you are not doing so, which leaves your RequiredFieldValidator powerless to prevent the postback.
In combination, these two things mean that
the postback occurs despite the empty textbox, and
the server-side custom validation does not fire on postback, because of the empty textbox.
You can call the client validation from your custom function using Page_ClientValidate()), which will be present in your page script since the page contains validators.
function ClientValidate() {
if (Page_ClientValidate()) {
//do custom validation, maybe return false
return true;
}
else {
return false;
}
}
I added a regex validator but its not showing anything on the page, basically the validation is done somewhere else i just needed to fire up. Here is the validator
<div>
<asp:RequiredFieldValidator
ID="RegularNoCardAccepted" runat="server"
ControlToValidate="txtCreditCardNumber"
CssClass="Error" Display="Dynamic">
</asp:RequiredFieldValidator>
</div>
And here is how I am trying to fire up, in reality i dont need it to check against a regular expression, I am just not sure how to make it pop up when it meets this condition
if (CardNotAccepted())
{
//Find the validator located somewhere in the master page.
RequiredFieldValidator reqVal =
FindControlRecursive(this.Page.Master, "RegularNoCardAccepted")
as RequiredFieldValidator;
if (reqVal != null)
{
//The code goes through here but it never shows.
reqVal.Enabled = true;
reqVal.Text = "Credit Card Type is not accepted";
reqVal.Visible = true;
reqVal.Validate();
}
return;
}
ASP.NET FieldValidators work automatically (assuming the Enabled property is set to true) on POST events. Here is an example of use: http://www.w3schools.com/aspnet/showasp.asp?filename=demo_reqfieldvalidator
I got this ASPxTextBox:
<dxe:ASPxTextBox ID="txtFirstName" runat="server">
<ClientSideEvents LostFocus="function(s, e) {
if (s.GetText() == '')
{
s.SetIsValid(false);
s.errorText = 'Please enter First Name.';
}
}"></ClientSideEvents>
<ValidationSettings ErrorDisplayMode="ImageWithTooltip">
</ValidationSettings>
</dxe:ASPxTextBox>
But the errorText displayed isn't what I have set inside the LostFocus event. Instead, the default errorText which is "Invalid value" is displayed in the tool tip.
Any ideas? Thanks
In order to accomplish this task, it is necessary to:
enable “required” validation (set the ValidationSettings.RequiredField.IsRequired property to “true”;
specify the “required” errorText (ValidationSettings.RequiredField.ErrorText);
force editor validation by setting the ValidationSettings.ValidateOnLeave property to “true”:
<dxe:ASPxTextBox ID="txtFirstName" runat="server">
<ClientSideEvents LostFocus="function(s, e) {
s.Validate();
}"></ClientSideEvents>
<ValidationSettings ErrorDisplayMode="ImageWithTooltip" ValidateOnLeave="true">
<RequiredField IsRequired="true" ErrorText="Please enter First Name." />
</ValidationSettings>
</dxe:ASPxTextBox>
I happen to solve the problem:
if (s.GetText() == '')
{
s.errorText = 'Please enter First Name.';
s.SetIsValid(false);
}
Modifying the value of errorText should come first before setting IsValid property of the ASPxTextBox to false.
(I want to laugh at my stupidness! OMG)
Thanks for those who helped. :)
Title is a bit of a mouthful, but I'm a bit stuck on this issue.
I have a search result page with has a custom control that has a Repeater. The ItemTemplate in the repeater is a PlaceHolder so I can construct the Item in a particular format; more specifically, I have a string of 'diseases' that come in the form of Disease1|disease2|disease3 and need to be given links for each disease.
Now for some code:
The following is the SearchResultControl.ascx
<asp:Panel ID="SearchResultPanel" runat="server" ScrollBars="Auto">
<asp:Repeater ID="Repeater1" runat="server"
onitemcreated="Repeater1_ItemCreated"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:PlaceHolder ID="ItemTemplatePlaceHolder" runat="server">
</asp:PlaceHolder>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan="6"><hr /></td>
</tr>
</SeparatorTemplate>
</asp:Repeater>
</asp:Panel>
The code behind: SearchResultControl.ascx.cs
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem != null)
{
PlaceHolder placeHolder = e.Item.FindControl("ItemTemplatePlaceHolder") as PlaceHolder;
Control searchResultItem = Page.LoadControl("SearchResultItem.ascx");
DataRow row = (e.Item.DataItem as DataRowView).Row;
if (row != null)
{
string diseaseState = row["DiseaseStates"] as string;
searchResultItem.GetType().GetProperty("DiseaseStates").SetValue(searchResultItem, diseaseState, null);
placeHolder.Controls.Add(searchResultItem);
}
}
}
(Full disclosure, I got this idea from this question)
The SetValue calls the DiseaseStates property in SearchResultItem which in turn calls the following method to build the links, set the text, and the events:
private void BuildDiseaseStateLabels(string diseaseStates)
{
PlaceHolder placeHolder = FindControl("DiseaseStatePlaceHolder") as PlaceHolder;
string[] diseaseStateSplit = diseaseStates.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
int count = diseaseStateSplit.Length;
foreach (string diseaseState in diseaseStateSplit)
{
LinkButton diseaseStateLink = new LinkButton();
diseaseStateLink.Text = diseaseState;
//diseaseStateLink.Click += new EventHandler(OnDiseaseStateLinkClick);
diseaseStateLink.CommandArgument = "<%# Eval(\"PatientID\")+ \";\" + Eval(\"PatientStatus\")+ \";\" + Eval(\"Age\")+ \";\" + Eval(\"DiseaseStates\")%>";
diseaseStateLink.CommandName = "OnDiseaseStateLinkClick";
//diseaseStateLink.Command += new CommandEventHandler(OnDiseaseStateLinkClick);
placeHolder.Controls.Add(diseaseStateLink);
if (count != 0)
{
Label splitLabel = new Label();
splitLabel.Text = "|";
placeHolder.Controls.Add(splitLabel);
}
}
}
This is the layout for the SearchResultItem
<div id="SearchResultItemDiv" class="MainSearchResultItem">
<asp:PlaceHolder ID="DiseaseStatePlaceHolder" runat="server">
</asp:PlaceHolder>
</div>
Initially I tried setting the Click event, but that doesn't work at all. I then set the CommandArgument and CommandName but that didn't seem to do the trick. I figured the Command event might need to be set, but again, no luck. I should note that when a link is clicked the Repeater1_ItemCreated in SearchResultControl.ascx.cs is called. But since there is no data in e.Item.DataItem is null and I lose the results.
In one of the questions regarding the same issue, it was suggested that the OnItemCommand be added, but even that doesn't get called.
I also read A Stumper of an ASP.NET Question and A Stumper of an ASP.NET Question: SOLVED!, to no avail.
What could I possibly be doing wrong? All of the correct event hookups seem there, I'm checking for IsPostBack and not doing DataBind() again. blaaargharaggh
Help is always greatly appreciate.
I believe you're running into this issue because the LinkButton controls are recreated too late in the page lifecycle. You have to remember that when the page is posted back, the control technically does not exist anymore, so the event handler cannot be fired.
If you can recreate the LinkButton controls somewhere before the Page_Load event is reached, like OnInit for example, everything should work fine.
For simple pages the above usually works very well, but there are circumstances where recreating controls during OnInit requires a lot of overhead, such as storing counters or arrays in ViewState so you can keep track of the controls that need to be recreated after postback. In these situations I would suggest taking a look at the DynamicControlsPlaceHolder by Denis Bauer. This control is capable of persisting dynamic controls without any addtional code required, which is pretty awesome.
Here's a link to the latest version:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
The problem might be that you're not doing the DataBind() again.
Because you're building the buttons on the fly, when the page post backs, the buttons haven't been created and are unable to work out which click event it should be firing.
Try getting rid of the IsPostBack check, so each time the page loads, you're re-build the repeater.
I'm currently working with a listview in which I want an htmltablecell to possess the onclick property which is driven by the codebehind rather than a javascript.. However I'm guessing that's pretty much a dream getting it to obey the C# code... Anyways this is what I want it to run:
protected void show_anm(object sender, EventArgs e)
{
Label hiddenc = (Label)listview1.FindControl("hidden");
Alert.Show(hiddenc.Text);
}
and here's the Alert class
public static class Alert
{
public static void Show(string message)
{
string cleanMessage = message.Replace("'", "\\'");
string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
}
}
}
The point is creating a listview with a two conditional tablecells, one which appears only when a certain condition is met and the other appears every other time (that's alredy sorted out). Where the one demanding a condition is Clickable, and upon clicking it it'll display an Alertbox with Data from a specific DB cell...
Sorry if my language and the question seemes off, English isn't my native language and I haven't doused myself in Coffe yet.
Any help on the matter would be most appritiated
EDIT1*
<asp:Listview ................
<ItemTemplate>
<tr ......>
<td id=default .....>
<asp:label ........ Text='<%# eval("stuff") %> />
</td>
<td id=conditional onclick=alert()..........>
<asp:label ......... Text='<%# eval("stuff") %> />
</td>
<td id=hidden visible=false ...........>
<asp:label ......... Text='<%#eval("stuff i want in alert") %>' />
.....
<script tyupe="text/javascript">
function alert()
{
var msg = document.getElementById("tried with label id and tablecell id nothing seemingly worked").value;
alert(msg);
}
</script>
I recently made a workaround that shows the data I want to display in the labels tooltip but I'd still prefer the alertbox to work properly as it feels more natural to click something.
Edit2 In case anyone is wondering I used the ItemDataBound event to bind the visibility of cells default and conditional within an if clause to make sure the control exists and the conditions are met.
I am confused as to why you're doing what you're doing. Why do you want the codebehind to handle an onclick event of a htmltablecell when you are pumping out javascript to show an alert anyway?
Why not just handle the whole logic within Javascript?
A postback from a htmltablcell will also require javascript
Set your tablecell to call a javascript function which would obtain the alert text from the hidden value and display that;
function ShowAlert()
{
var message = document.getElementbyId("hidden").value;
alert.show(message);
}