ASP .NET: CustomValidator doesn't launch its ServerValidate method - c#

the aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MyWebForm.aspx.cs" Inherits="MyWebApplication.MyWebForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height: 334px">
<form id="form1" runat="server">
<div style="height: 338px">
<asp:TextBox ID="MyTextBox" runat="server" AutoPostBack="True"></asp:TextBox>
<br />
<asp:CustomValidator ID="MyCustomValidator" runat="server"
ControlToValidate="MyTextBox" ErrorMessage="My error message"
onservervalidate="Foo2"></asp:CustomValidator>
</div>
</form>
</body>
</html>
the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyWebApplication
{
public partial class MyWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
bool Foo1(string bar)
{
if (bar == "bar")
return true;
return false;
}
protected void Foo2(object source, ServerValidateEventArgs args)
{
args.IsValid = this.Foo1(this.MyTextBox.Text);
} // breakpoint
}
}
I set breakpoint in the Foo2 function (comment), but debugger hasn't visited it. I tried writing diffrent text, not writting anything and writting "bar" and pushing enter and tab after that, but it still doesn't work. I mean text changing action. Is it possible?

There's no submit button. You need to add a button to submit the form, which will then trigger page validation.
<asp:Button runat="server" id="myButton" Text="Submit" />

Related

JQuery conflict with C# InnetHtml

When adding the JQuery JS include the InnetHTML is not working:
My HTML code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="aaa.aspx.cs" Inherits="aaa" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css"
/>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="ppdiv" runat="server">
</div>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
</form>
</body>
</html>
My C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class aaa : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ppdiv.InnerHtml = "aaa";
}
}
when include JQUery the InnetHTML is not working. Where is the error and how to fix it.
Thanks in advance
i believe it is because you are using runat="server" in head within which you add the jquery. jquery and javascript for client side, and runat="server" posts the data to server. remove runat="server" from head and check.

ASP.net gridview, autorefresh, txt as source and maintain scroll position

I want this gridview to emulate a console, the text file I have is a server console log.
I want it to:
*Refresh when the file changes.(thus the timer)
*Scroll bar position maintained when updated, unless at bottom.
*When scroll bar position is at the bottom,stay at the bottom after databind update.
Current Problem: On timer, databind() moves the scrollbar position back to the top, and with a refresh so often you can't even scroll down at all.
Here is my current code. Thank you for your help!
asp.net
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TEST CONSOLE.aspx.cs" Inherits="WebApplication1.TEST_CONSOLE" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="30" ontick="Timer1_Tick"></asp:Timer>
<div style="width: 100%; height: 400px; overflow: scroll">
<asp:GridView ID="GridView1" runat="server" MaintainScrollPositionOnPostback="true">
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication1
{
public partial class TEST_CONSOLE : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StreamReader objStreamReader = new StreamReader(Server.MapPath("TextFile.txt"));
var arrText = new List<string>();
// ' Loop through the file and add each line to the ArrayList
while( objStreamReader.Peek() >= 0){
arrText.Add(objStreamReader.ReadLine());
}
//' Close the reader
objStreamReader.Close();
//' Bind the results to the GridView
GridView1.DataSource = arrText;
GridView1.DataBind();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
GridView1.DataBind();
}
}
}
Found most of answer here
http://forums.asp.net/t/1220070.aspx?how+to+maintain+scroll+position+in+gridview+or+datagrid+during+postback
using ajax and these two functions
function Func() {//document.getElementById("hdnScrollTop").value
document.getElementById("divScroll").scrollTop = document.getElementById("hdnScrollTop").value;
}
function Func2() {
var s = document.getElementById("divScroll").scrollTop;
document.getElementById('hdnScrollTop').value = s;

how to set html tag dir attribute from rtl to ltr and vice versa programmatically in c sharp

I'm trying to build a multilingual web site on asp.net and i need to set html tag dir attribute from rtl to ltr and vice versa programmatically in c sharp.
For now i have 3 languages english, hebrew and russian and i need to switch between them.
I need to change direction of a site every time i change from ltr to rtl language and from rtl to ltr language
here the example of my masterpage
please help me
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="CSS/Site-RTL.css" rel="stylesheet" type="text/css" />
</head>
<body id="html" runat="server">
<form id="theForm" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder" runat="server">
</asp:ContentPlaceHolder>
</div>
<div style="margin-top: 20px;">
<asp:LinkButton ID="btnSetEnglish" runat="server" Text="English" CommandArgument="en-GB"
OnClick="RequestLanguageChange_Click"></asp:LinkButton>
<asp:LinkButton ID="btnSetHebrew" runat="server" Text="Hebrew" CommandArgument="he-IL"
OnClick="RequestLanguageChange_Click"></asp:LinkButton>
<asp:LinkButton ID="btnSetRussian" runat="server" Text="Russian" CommandArgument="ru-RU"
OnClick="RequestLanguageChange_Click"></asp:LinkButton>
</div>
<div>
<asp:SiteMapDataSource ID="smdsMaster" runat="server" OnDataBinding="Page_Load" />
<asp:Menu ID="MenuMaster" runat="server" CssClass="menu" DataSourceID="smdsMaster"
Orientation="Horizontal" StaticDisplayLevels="2" StaticSubMenuIndent="16px" MaximumDynamicDisplayLevels="1">
<DataBindings>
<asp:MenuItemBinding DataMember="SiteMapNode" NavigateUrlField="Url" />
</DataBindings>
</asp:Menu>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
here a code behind a master page
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Localization.Classes;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
LinkButton senderLink = sender as LinkButton;
//store requested language as new culture in the session
if (senderLink.CommandArgument == "he-IL")
{
// some code
}
else
{
// some code
}
Session[Global.SESSION_KEY_CULTURE] = senderLink.CommandArgument;
//reload last requested page with new culture
Server.Transfer(Request.Path);
}
}
In your code behind:
this.html.Attributes.Add("dir", "ltr");
An alternative, if this is not working is to place the attribute directly on the tag and use a property to populate it:
<body dir="<%:Dir%>">
public string Dir { get; set; }
// Set Dir in the on click event
protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
LinkButton senderLink = sender as LinkButton;
//store requested language as new culture in the session
if (senderLink.CommandArgument == "he-IL")
{
Dir = "rtl";
}
else
{
Dir = "ltr";
}
Session[Global.SESSION_KEY_CULTURE] = senderLink.CommandArgument;
//reload last requested page with new culture
Server.Transfer(Request.Path);
}

Hiding the text box when clicking the button in the asp.net website

I have a button in my asp.net web application. When i clicking the button it will hide the text box in the same webs application .
If is it possible that anyone help me its very useful
Thank you
If the button is an html button then you can use javascript to do this:
onclick of button call following js:
document.getElementById(textBoxId).style.display = "none";
or
document.getElementById(textBoxId).style.visibility = "hidden";
from code behind TextBoxId.Visible = false;
from Javascript document.getElementById('<%=TextBoxId.ClientId%>').style.dispaly="none";
You can do it with JQuery:
<script>
$("#myButton").click(function () {
$("#myTextBox").hide("slow");
});
</script>
This is what you are looking for:
HideTextBox.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="HideTextBox.aspx.cs" Inherits="HideTextFields" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="BtnHide" runat="server" onclick="Button1_Click"
Text="Hide TextBox" />
</div>
</form>
</body>
</html>
And the code behind file
HideTextBox.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class HideTextFields : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control c in form1.Controls)
{
if (c is TextBox)
c.Visible = false;
}
}
}

How to trigger onclick event after received return value from popup which using add attribute a javascript?

How to trigger onclick event after received return value from popup aspx which using add attribute a javascript?
After adding
.Attributes.Add("onClick", "return popWin('" + NewBatchNo_TextBox.Text + "');");
Original onclick event do not fire?
if this method can not work, any other method to get value from pop up message box and return value and run click event
Thanks.
try this
Main.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type='text/javascript'>
function DoStuff() {
document.getElementById('Button1').click();
}
function popWin() {
var popy = window.open('popup.aspx', 'popup_form', 'menubar=no,status=no,top=100%,left=100;')
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPopupValue" runat="server" Width="327px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Show List" />
</div>
</form>
</body>
</html>
Main.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Button1.Attributes.Add("onclick", "return popWin()");
}
}
}
popup.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="popup.aspx.cs" Inherits="popup" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type='text/javascript'>
function validepopupform() {
window.opener.document.getElementById('txtPopupValue').value = document.getElementById('txtPop').value
self.close();
}
window.onbeforeunload = CloseEvent;
function CloseEvent() {
if (window.opener && !window.opener.closed) {
window.opener.DoStuff();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPop" runat="server"></asp:TextBox>
<input type='button' value='go' onclick='validepopupform()' />
</div>
</form>
</body>
</html>
The same problem I faced several times the cause is that java script cannot call code behind(c#/vb function/events).
An alternate way I used is to use hidden fields that is accessible by both java script and code behind. But then you want to call events not read values modified by java script.
For this we refreshed the page through java script when we want to trigger that code behind event and let the page load to monitor the scenario(value that java script set) and triggers the required event.
The code at the end would be real mess and unmanageable, would take lots of efforts to debug.

Categories