Saturday, March 31, 2012
Submitting form with multiple onclick events
I'm building a custom control which basically takes data from the user
and then submits it. I would use the validator controls but if I
understand correctly (I've been working with .Net for 2 weeks) you
cant 'embed' them in the custom controls. Someone correct me if I'm
wrong (and please point to an example :) )
So, to get around this, I'm using Javascript. In my control, I'm doing
Writer.WriteAttribute("OnClick", "javascript:checkData();" &
Page.GetPostBackEventReference(Me, "Data"))
for an Input item so I get something like
<input type="button" value="Submit"
OnClick="javascript:checkData();__doPostBack('MyControl','D ata')" /
The checkData() function does this:
function checkLogin()
{
if (document.Data.item1.value == "" )
{
alert("Please enter item 1.");
return false;
}
if (document.Data.item2.value == "" )
{
alert("Please enter item 2.");
return false;
}
if (document.Data.item1.value != "" && document.Data.item2.value !=
"" )
{
return true;
}
}
My query is, how do I get the click event to fire correctly so that i
get alerts when there is a problem, and it passes through and submits
the form if there isnt a problem?
Thanks
SamHi,
Your client side script should use cancelBubble and returnValue to
disable event default process and raise alert if error found.
function CallServer()
{
if validate
{
__doPostBack('MyControl','Data');
}
else
{
alert("Error");
window.event.cancelBubble = true;
window.event.returnValue = false;
}
}
HTH
Natty Gur[MVP]
blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Natty Gur <natty@.dao2com.com> wrote in message news:<uSSwHLhAEHA.3348@.TK2MSFTNGP11.phx.gbl>...
> Hi,
> Your client side script should use cancelBubble and returnValue to
> disable event default process and raise alert if error found.
> function CallServer()
> {
> if validate
> {
> __doPostBack('MyControl','Data');
> }
> else
> {
> alert("Error");
> window.event.cancelBubble = true;
> window.event.returnValue = false;
> }
> }
>
> HTH
> Natty Gur[MVP]
> blog : http://weblogs.asp.net/ngur
> Mobile: +972-(0)58-888377
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!
Thanks Natty
This means I have to create inline javascripts which I'd rather not do
if I can help it. Just as luck would have it, after spending an 'hour
or two' trying to find a solution before posting, 5 mins after posting
I find a solution :( Always the way...
Anyhow, just for reference purposes, what I've done is to validate on
OnMouseDown and postback on OnClick. Works a treat
Hi Samuel,
Its unfortunate that you had to rework validation because of some incorrect
information about validators. The ASP.NET validators work fine within custom
controls. They have one key limitation related to their "location": the
ControlToValidate must be associated with the ID of a control in the same
"naming container". If your custom control impliments INamingContainer,
ControlToValidate can only point to other controls within your custom
control. If not, it can refer to other controls in the same naming container
that your custom control appears. Some types of naming containers: Page,
UserControl, and rows in DataGrid and DataList.
If you wanted to support validation without naming container limits, I wrote
a replacement to Microsoft's validators that overcomes that limitation among
numerous others: Professional Validation And More.
http://www.peterblum.com/vam/home.aspx.
-- Peter Blum
www.PeterBlum.com
Email: PLBlum@.PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
"Samuel Hon" <noreply@.samuelhon.co.uk> wrote in message
news:c8672b7d.0403040703.c458604@.posting.google.co m...
> Hi
> I'm building a custom control which basically takes data from the user
> and then submits it. I would use the validator controls but if I
> understand correctly (I've been working with .Net for 2 weeks) you
> cant 'embed' them in the custom controls. Someone correct me if I'm
> wrong (and please point to an example :) )
> So, to get around this, I'm using Javascript. In my control, I'm doing
> Writer.WriteAttribute("OnClick", "javascript:checkData();" &
> Page.GetPostBackEventReference(Me, "Data"))
> for an Input item so I get something like
> <input type="button" value="Submit"
> OnClick="javascript:checkData();__doPostBack('MyControl','D ata')" />
> The checkData() function does this:
> function checkLogin()
> {
> if (document.Data.item1.value == "" )
> {
> alert("Please enter item 1.");
> return false;
> }
> if (document.Data.item2.value == "" )
> {
> alert("Please enter item 2.");
> return false;
> }
> if (document.Data.item1.value != "" && document.Data.item2.value !=
> "" )
> {
> return true;
> }
> }
> My query is, how do I get the click event to fire correctly so that i
> get alerts when there is a problem, and it passes through and submits
> the form if there isnt a problem?
> Thanks
> Sam
"Peter Blum" <PLBlum@.Blum.info> wrote in message news:<OC#ZP$uAEHA.624@.TK2MSFTNGP10.phx.gbl>...
> Hi Samuel,
> Its unfortunate that you had to rework validation because of some incorrect
> information about validators. The ASP.NET validators work fine within custom
> controls. They have one key limitation related to their "location": the
> ControlToValidate must be associated with the ID of a control in the same
> "naming container". If your custom control impliments INamingContainer,
> ControlToValidate can only point to other controls within your custom
> control. If not, it can refer to other controls in the same naming container
> that your custom control appears. Some types of naming containers: Page,
> UserControl, and rows in DataGrid and DataList.
> If you wanted to support validation without naming container limits, I wrote
> a replacement to Microsoft's validators that overcomes that limitation among
> numerous others: Professional Validation And More.
> http://www.peterblum.com/vam/home.aspx.
> -- Peter Blum
> www.PeterBlum.com
> Email: PLBlum@.PeterBlum.com
> Creator of "Professional Validation And More" at
> http://www.peterblum.com/vam/home.aspx
<snip
Thanks Peter for clearing that up, I will re-investigate
Sam
Saturday, March 24, 2012
Suggestions: Filling calendar with db events
best way to fill a calendar with this data? As I switch months I want it to
repopulate with the "current" months data.
I'd like it to kind of work like a basic Outlook calendar.
Thanks!1. Create a calendar interface in ASP.Net/HTML.
2. Populate it with data.
3. What exactly do you need help with?
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
"VB Programmer" <Dont*NoSpam-Please*@.jEmail.com> wrote in message
news:eC0CzfT3EHA.1264@.TK2MSFTNGP12.phx.gbl...
> I have a bunch of events and appointments in a sql server db. What is the
> best way to fill a calendar with this data? As I switch months I want it
to
> repopulate with the "current" months data.
> I'd like it to kind of work like a basic Outlook calendar.
> Thanks!
>
I have a calendar on the aspx page. How can I bind it to data that comes
from a sql server db?
Ex:
Event 1 - 12/1/2004
Event 2 - 12/3/2004
Event 3 - 12/5/2004
So, if you were to go on the calendar to Dec 2004 Event 1 would show up in
the 12/1 box, Event 2 in the 12/3 box, etc... If I switched the calendar to
January it would populate the calendar with January events.
Thanks Kevin.
"Kevin Spencer" <kspencer@.takempis.com> wrote in message
news:ukQqNsT3EHA.2624@.TK2MSFTNGP11.phx.gbl...
> 1. Create a calendar interface in ASP.Net/HTML.
> 2. Populate it with data.
> 3. What exactly do you need help with?
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Neither a follower
> nor a lender be.
> "VB Programmer" <Dont*NoSpam-Please*@.jEmail.com> wrote in message
> news:eC0CzfT3EHA.1264@.TK2MSFTNGP12.phx.gbl...
> to
>
check out these code samples:
http://aspnet101.com/aspnet101/aspn...?code=caltitles
http://aspnet101.com/aspnet101/aspn...spx?code=CalMdb
David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com
Coming Soon - Data Management Toolkit
Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
http://augustwind.com/augustwind/dmt.aspx
"VB Programmer" <Dont*NoSpam-Please*@.jEmail.com> wrote in message
news:eC0CzfT3EHA.1264@.TK2MSFTNGP12.phx.gbl...
> I have a bunch of events and appointments in a sql server db. What is the
> best way to fill a calendar with this data? As I switch months I want it
to
> repopulate with the "current" months data.
> I'd like it to kind of work like a basic Outlook calendar.
> Thanks!
>
(Sorry - the other link was supposed to be:
http://aspnet101.com/aspnet101/aspn...x?code=calendar)
David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com
Coming Soon - Data Management Toolkit
Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
http://augustwind.com/augustwind/dmt.aspx
"VB Programmer" <Dont*NoSpam-Please*@.jEmail.com> wrote in message
news:eC0CzfT3EHA.1264@.TK2MSFTNGP12.phx.gbl...
> I have a bunch of events and appointments in a sql server db. What is the
> best way to fill a calendar with this data? As I switch months I want it
to
> repopulate with the "current" months data.
> I'd like it to kind of work like a basic Outlook calendar.
> Thanks!
>
Suggestions: Filling calendar with db events
best way to fill a calendar with this data? As I switch months I want it to
repopulate with the "current" months data.
I'd like it to kind of work like a basic Outlook calendar.
Thanks!1. Create a calendar interface in ASP.Net/HTML.
2. Populate it with data.
3. What exactly do you need help with?
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
"VB Programmer" <Dont*NoSpam-Please*@.jEmail.com> wrote in message
news:eC0CzfT3EHA.1264@.TK2MSFTNGP12.phx.gbl...
> I have a bunch of events and appointments in a sql server db. What is the
> best way to fill a calendar with this data? As I switch months I want it
to
> repopulate with the "current" months data.
> I'd like it to kind of work like a basic Outlook calendar.
> Thanks!
I have a calendar on the aspx page. How can I bind it to data that comes
from a sql server db?
Ex:
Event 1 - 12/1/2004
Event 2 - 12/3/2004
Event 3 - 12/5/2004
So, if you were to go on the calendar to Dec 2004 Event 1 would show up in
the 12/1 box, Event 2 in the 12/3 box, etc... If I switched the calendar to
January it would populate the calendar with January events.
Thanks Kevin.
"Kevin Spencer" <kspencer@.takempis.com> wrote in message
news:ukQqNsT3EHA.2624@.TK2MSFTNGP11.phx.gbl...
> 1. Create a calendar interface in ASP.Net/HTML.
> 2. Populate it with data.
> 3. What exactly do you need help with?
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Neither a follower
> nor a lender be.
> "VB Programmer" <Dont*NoSpam-Please*@.jEmail.com> wrote in message
> news:eC0CzfT3EHA.1264@.TK2MSFTNGP12.phx.gbl...
>> I have a bunch of events and appointments in a sql server db. What is
>> the
>> best way to fill a calendar with this data? As I switch months I want it
> to
>> repopulate with the "current" months data.
>>
>> I'd like it to kind of work like a basic Outlook calendar.
>>
>> Thanks!
>>
>>
check out these code samples:
http://aspnet101.com/aspnet101/aspn...?code=caltitles
http://aspnet101.com/aspnet101/aspn...spx?code=CalMdb
David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com
Coming Soon - Data Management Toolkit
Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
http://augustwind.com/augustwind/dmt.aspx
"VB Programmer" <Dont*NoSpam-Please*@.jEmail.com> wrote in message
news:eC0CzfT3EHA.1264@.TK2MSFTNGP12.phx.gbl...
> I have a bunch of events and appointments in a sql server db. What is the
> best way to fill a calendar with this data? As I switch months I want it
to
> repopulate with the "current" months data.
> I'd like it to kind of work like a basic Outlook calendar.
> Thanks!
(Sorry - the other link was supposed to be:
http://aspnet101.com/aspnet101/aspn...x?code=calendar)
David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com
Coming Soon - Data Management Toolkit
Manage MSDE/SQL Server/MySQL/MS Access Databases from one location.
http://augustwind.com/augustwind/dmt.aspx
"VB Programmer" <Dont*NoSpam-Please*@.jEmail.com> wrote in message
news:eC0CzfT3EHA.1264@.TK2MSFTNGP12.phx.gbl...
> I have a bunch of events and appointments in a sql server db. What is the
> best way to fill a calendar with this data? As I switch months I want it
to
> repopulate with the "current" months data.
> I'd like it to kind of work like a basic Outlook calendar.
> Thanks!
Tuesday, March 13, 2012
Swapping programmatically created user controls?
I have a form in which I'm trying to load a user control depending on
some user choice, and my control's events are not firing properly.
Here's a completely stripped down repro of the problem:
The form itself is empty:
<body>
<form id="form1" runat="server" />
</body>
and here's the code behind for the form:
protected override void OnInit( EventArgs e )
{
string currentControl = "UC1";
object objCurrentControl = Session["CurrentControl"];
if( objCurrentControl != null )
currentControl = (string)objCurrentControl;
form1.Controls.Add( LoadControl( currentControl + ".ascx" ) );
}
The first user control:
===============
<%@dotnet.itags.org. Control Language="C#" AutoEventWireup="true" CodeFile="UC1.ascx.cs"
Inherits="UC1" %>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Create UC2" />
and the code behind:
protected void Button1_Click( object sender, EventArgs e )
{
Control ctl = LoadControl( "UC2.ascx" );
ctl.ID = "UC2";
Parent.Controls.Add( ctl );
HttpContext.Current.Session["CurrentControl"] = "UC2";
Parent.Controls.Remove( this );
}
The second user control:
==================
<%@dotnet.itags.org. Control Language="C#" AutoEventWireup="true" CodeFile="UC2.ascx.cs"
Inherits="UC2" %>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Create UC1" />
and the code behind:
protected void Button1_Click( object sender, EventArgs e )
{
Control ctl = LoadControl( "UC1.ascx" );
ctl.ID = "UC1";
Parent.Controls.Add( ctl );
Parent.Controls.Remove( this );
HttpContext.Current.Session["CurrentControl"] = "UC1";
}
and here's the problem:
When I run this, the page loads fine, with UC1. I click on the button
in UC1, the form gets posted, and comes back with UC2. Now if I click
on the button in UC2, the form gets posted, but comes back with UC2
itself instead of UC1. I click on the button in UC2 again, and now the
form comes back with UC1! It seems that the control loads fine, but the
button click event defined inside the user control will not fire the
first time, but only the second time.
I'm from the WinForms world and I'm new to the WebForms, so I might be
missing something obvious. Could anyone kindly point out what I'm doing
wrong? I've been scratching my head for the past two days without
making any progress. Thanks a lot in advance for your time.
- Ramesh.Hi,
I solved the problem myself. All I had to do was change the OnInit of
the form to set the ID of the control as well, like this:
protected override void OnInit( EventArgs e )
{
string currentControl = "UC1";
object objCurrentControl = Session["CurrentControl"];
if( objCurrentControl != null )
currentControl = (string)objCurrentControl;
Control ctl = LoadControl( currentControl + ".ascx" );
ctl.ID = currentControl;
form1.Controls.Add( ctl );
}
and it works fine now. I knew I was missing something obvious... Sorry
if I had wasted your time.
This approach, with an Atlas UpdatePanel thrown in to enclose the user
controls, results in a very WinForms like feel. Wow!
- Ramesh
Ramesh wrote:
Quote:
Originally Posted by
Hello all,
>
I have a form in which I'm trying to load a user control depending on
some user choice, and my control's events are not firing properly.
Here's a completely stripped down repro of the problem:
>
The form itself is empty:
<body>
<form id="form1" runat="server" />
</body>
>
and here's the code behind for the form:
protected override void OnInit( EventArgs e )
{
string currentControl = "UC1";
object objCurrentControl = Session["CurrentControl"];
if( objCurrentControl != null )
currentControl = (string)objCurrentControl;
>
form1.Controls.Add( LoadControl( currentControl + ".ascx" ) );
}
>
>
The first user control:
===============
<%@. Control Language="C#" AutoEventWireup="true" CodeFile="UC1.ascx.cs"
Inherits="UC1" %>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Create UC2" />
>
and the code behind:
protected void Button1_Click( object sender, EventArgs e )
{
Control ctl = LoadControl( "UC2.ascx" );
ctl.ID = "UC2";
Parent.Controls.Add( ctl );
HttpContext.Current.Session["CurrentControl"] = "UC2";
Parent.Controls.Remove( this );
}
>
The second user control:
==================
<%@. Control Language="C#" AutoEventWireup="true" CodeFile="UC2.ascx.cs"
Inherits="UC2" %>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Create UC1" />
>
and the code behind:
protected void Button1_Click( object sender, EventArgs e )
{
Control ctl = LoadControl( "UC1.ascx" );
ctl.ID = "UC1";
Parent.Controls.Add( ctl );
Parent.Controls.Remove( this );
HttpContext.Current.Session["CurrentControl"] = "UC1";
}
>
and here's the problem:
When I run this, the page loads fine, with UC1. I click on the button
in UC1, the form gets posted, and comes back with UC2. Now if I click
on the button in UC2, the form gets posted, but comes back with UC2
itself instead of UC1. I click on the button in UC2 again, and now the
form comes back with UC1! It seems that the control loads fine, but the
button click event defined inside the user control will not fire the
first time, but only the second time.
>
I'm from the WinForms world and I'm new to the WebForms, so I might be
missing something obvious. Could anyone kindly point out what I'm doing
wrong? I've been scratching my head for the past two days without
making any progress. Thanks a lot in advance for your time.
>
- Ramesh.