Showing posts with label swapping. Show all posts
Showing posts with label swapping. Show all posts

Tuesday, March 13, 2012

Swapping Image Url

In an aspx page I have declared an Image control:
<asp:Image id="myImage" runat="server"></asp:Image
In the code-behind I populate it's ImageURL property, like this:
myImage.ImageUrl = "some.gif";

Then, in client-side JavaScript, the user can cause the image to be
replaced, like this:
document.all("myImage").src = "other.gif";

Here's the problem: on postback, the image control's .ImageUrl property
still contains "some.gif" and not "other.gif" as I am needing.

What do I need to do to receive - during postback - the value set in the
client by the JavaScript?

Thanks!After postback, the value of ImageUrl is coming from the control's ViewState.

One solution would be to set the value of a hidden field when you set the
image's src attribute. The value of the hidden field will get posted back
correctly.

"Guadala Harry" wrote:

> In an aspx page I have declared an Image control:
> <asp:Image id="myImage" runat="server"></asp:Image>
> In the code-behind I populate it's ImageURL property, like this:
> myImage.ImageUrl = "some.gif";
> Then, in client-side JavaScript, the user can cause the image to be
> replaced, like this:
> document.all("myImage").src = "other.gif";
> Here's the problem: on postback, the image control's .ImageUrl property
> still contains "some.gif" and not "other.gif" as I am needing.
> What do I need to do to receive - during postback - the value set in the
> client by the JavaScript?
> Thanks!
>
>
Thanks - I thought about going the hidden field route - but was hoping for
some solution that wouldn't require adding another field or control that
needs synchronizing with the original Image control.

Any other ideas?

Thanks again...

-G

"Brad Quinn" <BradQuinn@.discussions.microsoft.com> wrote in message
news:E07F8EE9-B35D-488C-A2A9-CB60DCA6AACD@.microsoft.com...
> After postback, the value of ImageUrl is coming from the control's
ViewState.
> One solution would be to set the value of a hidden field when you set the
> image's src attribute. The value of the hidden field will get posted back
> correctly.
> "Guadala Harry" wrote:
> > In an aspx page I have declared an Image control:
> > <asp:Image id="myImage" runat="server"></asp:Image>
> > In the code-behind I populate it's ImageURL property, like this:
> > myImage.ImageUrl = "some.gif";
> > Then, in client-side JavaScript, the user can cause the image to be
> > replaced, like this:
> > document.all("myImage").src = "other.gif";
> > Here's the problem: on postback, the image control's .ImageUrl property
> > still contains "some.gif" and not "other.gif" as I am needing.
> > What do I need to do to receive - during postback - the value set in the
> > client by the JavaScript?
> > Thanks!
Can you make the code in your codebehind only execute the first time it
executes? i.e.

if (!Page.IsPostback)
myImage.ImageUrl = "some.gif";

Toby Mathews

"Guadala Harry" <GMan@.NoSpam.com> wrote in message
news:%23wqVQXQgEHA.2908@.TK2MSFTNGP10.phx.gbl...
> Thanks - I thought about going the hidden field route - but was hoping for
> some solution that wouldn't require adding another field or control that
> needs synchronizing with the original Image control.
> Any other ideas?
> Thanks again...
> -G
>
> "Brad Quinn" <BradQuinn@.discussions.microsoft.com> wrote in message
> news:E07F8EE9-B35D-488C-A2A9-CB60DCA6AACD@.microsoft.com...
> > After postback, the value of ImageUrl is coming from the control's
> ViewState.
> > One solution would be to set the value of a hidden field when you set
the
> > image's src attribute. The value of the hidden field will get posted
back
> > correctly.
> > "Guadala Harry" wrote:
> > > In an aspx page I have declared an Image control:
> > > <asp:Image id="myImage" runat="server"></asp:Image>
> > > > In the code-behind I populate it's ImageURL property, like this:
> > > myImage.ImageUrl = "some.gif";
> > > > Then, in client-side JavaScript, the user can cause the image to be
> > > replaced, like this:
> > > document.all("myImage").src = "other.gif";
> > > > Here's the problem: on postback, the image control's .ImageUrl
property
> > > still contains "some.gif" and not "other.gif" as I am needing.
> > > > What do I need to do to receive - during postback - the value set in
the
> > > client by the JavaScript?
> > > > Thanks!
> > > >
That's not really relevant to the problem - it would be IF there was an
issue with postback processing walking on the value set in the client - but
issue is that the value set in the client code is never making it to the
server (unless I explicitly stuff the value into a hidden field - which I
don't want to do unless I really must).

Thanks anyway.

"Toby Mathews" <tobymathewsNOSPAM@.yahoo.spamfree.co.uk> wrote in message
news:cfhs1q$87n$1@.thorium.cix.co.uk...
> Can you make the code in your codebehind only execute the first time it
> executes? i.e.
> if (!Page.IsPostback)
> myImage.ImageUrl = "some.gif";
> Toby Mathews
> "Guadala Harry" <GMan@.NoSpam.com> wrote in message
> news:%23wqVQXQgEHA.2908@.TK2MSFTNGP10.phx.gbl...
> > Thanks - I thought about going the hidden field route - but was hoping
for
> > some solution that wouldn't require adding another field or control that
> > needs synchronizing with the original Image control.
> > Any other ideas?
> > Thanks again...
> > -G
> > "Brad Quinn" <BradQuinn@.discussions.microsoft.com> wrote in message
> > news:E07F8EE9-B35D-488C-A2A9-CB60DCA6AACD@.microsoft.com...
> > > After postback, the value of ImageUrl is coming from the control's
> > ViewState.
> > > > One solution would be to set the value of a hidden field when you set
> the
> > > image's src attribute. The value of the hidden field will get posted
> back
> > > correctly.
> > > > "Guadala Harry" wrote:
> > > > > In an aspx page I have declared an Image control:
> > > > <asp:Image id="myImage" runat="server"></asp:Image>
> > > > > > In the code-behind I populate it's ImageURL property, like this:
> > > > myImage.ImageUrl = "some.gif";
> > > > > > Then, in client-side JavaScript, the user can cause the image to be
> > > > replaced, like this:
> > > > document.all("myImage").src = "other.gif";
> > > > > > Here's the problem: on postback, the image control's .ImageUrl
> property
> > > > still contains "some.gif" and not "other.gif" as I am needing.
> > > > > > What do I need to do to receive - during postback - the value set in
> the
> > > > client by the JavaScript?
> > > > > > Thanks!
> > > > > > > >
Harry,

Sorry, I didn't read your message properly first time round. I'm not sure
how else you could achieve what you want to do other than how you decribe.
Good luck,

Toby

"Guadala Harry" <GMan@.NoSpam.com> wrote in message
news:%23Tkt9DRgEHA.1048@.tk2msftngp13.phx.gbl...
> That's not really relevant to the problem - it would be IF there was an
> issue with postback processing walking on the value set in the client -
but
> issue is that the value set in the client code is never making it to the
> server (unless I explicitly stuff the value into a hidden field - which I
> don't want to do unless I really must).
> Thanks anyway.
>
> "Toby Mathews" <tobymathewsNOSPAM@.yahoo.spamfree.co.uk> wrote in message
> news:cfhs1q$87n$1@.thorium.cix.co.uk...
> > Can you make the code in your codebehind only execute the first time it
> > executes? i.e.
> > if (!Page.IsPostback)
> > myImage.ImageUrl = "some.gif";
> > Toby Mathews
> > "Guadala Harry" <GMan@.NoSpam.com> wrote in message
> > news:%23wqVQXQgEHA.2908@.TK2MSFTNGP10.phx.gbl...
> > > Thanks - I thought about going the hidden field route - but was hoping
> for
> > > some solution that wouldn't require adding another field or control
that
> > > needs synchronizing with the original Image control.
> > > > Any other ideas?
> > > > Thanks again...
> > > > -G
> > > > > > "Brad Quinn" <BradQuinn@.discussions.microsoft.com> wrote in message
> > > news:E07F8EE9-B35D-488C-A2A9-CB60DCA6AACD@.microsoft.com...
> > > > After postback, the value of ImageUrl is coming from the control's
> > > ViewState.
> > > > > > One solution would be to set the value of a hidden field when you
set
> > the
> > > > image's src attribute. The value of the hidden field will get
posted
> > back
> > > > correctly.
> > > > > > "Guadala Harry" wrote:
> > > > > > > In an aspx page I have declared an Image control:
> > > > > <asp:Image id="myImage" runat="server"></asp:Image>
> > > > > > > > In the code-behind I populate it's ImageURL property, like this:
> > > > > myImage.ImageUrl = "some.gif";
> > > > > > > > Then, in client-side JavaScript, the user can cause the image to
be
> > > > > replaced, like this:
> > > > > document.all("myImage").src = "other.gif";
> > > > > > > > Here's the problem: on postback, the image control's .ImageUrl
> > property
> > > > > still contains "some.gif" and not "other.gif" as I am needing.
> > > > > > > > What do I need to do to receive - during postback - the value set
in
> > the
> > > > > client by the JavaScript?
> > > > > > > > Thanks!
> > > > > > > > > > > > > >

Swapping Image Url

In an aspx page I have declared an Image control:
<asp:Image id="myImage" runat="server"></asp:Image>
In the code-behind I populate it's ImageURL property, like this:
myImage.ImageUrl = "some.gif";
Then, in client-side JavaScript, the user can cause the image to be
replaced, like this:
document.all("myImage").src = "other.gif";
Here's the problem: on postback, the image control's .ImageUrl property
still contains "some.gif" and not "other.gif" as I am needing.
What do I need to do to receive - during postback - the value set in the
client by the JavaScript?
Thanks!After postback, the value of ImageUrl is coming from the control's ViewState
.
One solution would be to set the value of a hidden field when you set the
image's src attribute. The value of the hidden field will get posted back
correctly.
"Guadala Harry" wrote:

> In an aspx page I have declared an Image control:
> <asp:Image id="myImage" runat="server"></asp:Image>
> In the code-behind I populate it's ImageURL property, like this:
> myImage.ImageUrl = "some.gif";
> Then, in client-side JavaScript, the user can cause the image to be
> replaced, like this:
> document.all("myImage").src = "other.gif";
> Here's the problem: on postback, the image control's .ImageUrl property
> still contains "some.gif" and not "other.gif" as I am needing.
> What do I need to do to receive - during postback - the value set in the
> client by the JavaScript?
> Thanks!
>
>
Thanks - I thought about going the hidden field route - but was hoping for
some solution that wouldn't require adding another field or control that
needs synchronizing with the original Image control.
Any other ideas?
Thanks again...
-G
"Brad Quinn" <BradQuinn@.discussions.microsoft.com> wrote in message
news:E07F8EE9-B35D-488C-A2A9-CB60DCA6AACD@.microsoft.com...
> After postback, the value of ImageUrl is coming from the control's
ViewState.
> One solution would be to set the value of a hidden field when you set the
> image's src attribute. The value of the hidden field will get posted back
> correctly.
> "Guadala Harry" wrote:
>
Can you make the code in your codebehind only execute the first time it
executes? i.e.
if (!Page.IsPostback)
myImage.ImageUrl = "some.gif";
Toby Mathews
"Guadala Harry" <GMan@.NoSpam.com> wrote in message
news:%23wqVQXQgEHA.2908@.TK2MSFTNGP10.phx.gbl...
> Thanks - I thought about going the hidden field route - but was hoping for
> some solution that wouldn't require adding another field or control that
> needs synchronizing with the original Image control.
> Any other ideas?
> Thanks again...
> -G
>
> "Brad Quinn" <BradQuinn@.discussions.microsoft.com> wrote in message
> news:E07F8EE9-B35D-488C-A2A9-CB60DCA6AACD@.microsoft.com...
> ViewState.
the
back
property
the
>
That's not really relevant to the problem - it would be IF there was an
issue with postback processing walking on the value set in the client - but
issue is that the value set in the client code is never making it to the
server (unless I explicitly stuff the value into a hidden field - which I
don't want to do unless I really must).
Thanks anyway.
"Toby Mathews" <tobymathewsNOSPAM@.yahoo.spamfree.co.uk> wrote in message
news:cfhs1q$87n$1@.thorium.cix.co.uk...
> Can you make the code in your codebehind only execute the first time it
> executes? i.e.
> if (!Page.IsPostback)
> myImage.ImageUrl = "some.gif";
> Toby Mathews
> "Guadala Harry" <GMan@.NoSpam.com> wrote in message
> news:%23wqVQXQgEHA.2908@.TK2MSFTNGP10.phx.gbl...
for
> the
> back
> property
> the
>
Harry,
Sorry, I didn't read your message properly first time round. I'm not sure
how else you could achieve what you want to do other than how you decribe.
Good luck,
Toby
"Guadala Harry" <GMan@.NoSpam.com> wrote in message
news:%23Tkt9DRgEHA.1048@.tk2msftngp13.phx.gbl...
> That's not really relevant to the problem - it would be IF there was an
> issue with postback processing walking on the value set in the client -
but
> issue is that the value set in the client code is never making it to the
> server (unless I explicitly stuff the value into a hidden field - which I
> don't want to do unless I really must).
> Thanks anyway.
>
> "Toby Mathews" <tobymathewsNOSPAM@.yahoo.spamfree.co.uk> wrote in message
> news:cfhs1q$87n$1@.thorium.cix.co.uk...
> for
that
set
posted
be
in
>

Swapping programmatically created user controls?

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:
===============
<%@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.