473,666 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set Conditional AutoPostBack In User Control

Assume that a user control has a TextBox (Page1.ascx)

<asp:TextBox ID="txtName" runat="server"/>

I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <formtags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:

<%@ Register TagPrefix="MyPa ge" TagName="Ctrl" Src="Page1.ascx " %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
..........
..........
End Sub
</script>

<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChange d="chk_Check"
AutoPostBack="t rue" Text="Check Me" runat="server"/>
</form>

When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.

One way of doing this is to make use of a Session variable....som ething
like this (in the ASPX page):

Sub Page_Load(..... .)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

Sub chk_Check(..... .)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

& then in the ASCX page

<script runat="server">
Sub Page_Load(..... .)
If (Session("chk") = 1) Then
txtName.AutoPos tBack = True
Else
txtName.AutoPos tBack = False
End If
End Sub
</script>

Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?

Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.

Oct 8 '06 #1
2 5284
If you want to do this as simply as possible, you will have the checkbox
auto postback to set the value. If you want this fully dynamic, you will
have to write JavaScript that creates a postback event on the textbox and
removes it via checking/unchecking the box.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/

*************** *************** *************** ****
Think Outside the Box!
*************** *************** *************** ****
<rn**@rediffmai l.comwrote in message
news:11******** *************@c 28g2000cwb.goog legroups.com...
Assume that a user control has a TextBox (Page1.ascx)

<asp:TextBox ID="txtName" runat="server"/>

I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <formtags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:

<%@ Register TagPrefix="MyPa ge" TagName="Ctrl" Src="Page1.ascx " %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
..........
..........
End Sub
</script>

<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChange d="chk_Check"
AutoPostBack="t rue" Text="Check Me" runat="server"/>
</form>

When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.

One way of doing this is to make use of a Session variable....som ething
like this (in the ASPX page):

Sub Page_Load(..... .)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

Sub chk_Check(..... .)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

& then in the ASCX page

<script runat="server">
Sub Page_Load(..... .)
If (Session("chk") = 1) Then
txtName.AutoPos tBack = True
Else
txtName.AutoPos tBack = False
End If
End Sub
</script>

Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?

Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.

Oct 8 '06 #2
It's simple (i am assuming you want to use postbacks instead of dynamic
javascript). Just create a property for user control that will expose
autopostback for the text box as shown below (i defined it in code behind of
the user control)

Public Class Page1
Inherits System.Web.UI.U serControl

Public Property TextBoxAutoPost Back() As Boolean
Get
Return txtName.AutoPos tBack
End Get
Set(ByVal value As Boolean)
txtName.AutoPos tBack = value
End Set
End Property

End Class

Now use it in aspx page like this

<%@ Register TagPrefix="MyPa ge" TagName="Ctrl" Src="Page1.ascx " %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
mpc.TextBoxAuto PostBack = CType(obj, CheckBox).Check ed
End Sub
</script>

<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChange d="chk_Check"
AutoPostBack="t rue" Text="Check Me" runat="server"/>
</form>

Autopostback is serialized to viewstate by default so you page contains it
between postbacks. That's all.

Hope this helps
--

Milosz Skalecki
MCP, MCAD
"rn**@rediffmai l.com" wrote:
Assume that a user control has a TextBox (Page1.ascx)

<asp:TextBox ID="txtName" runat="server"/>

I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <formtags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:

<%@ Register TagPrefix="MyPa ge" TagName="Ctrl" Src="Page1.ascx " %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
..........
..........
End Sub
</script>

<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChange d="chk_Check"
AutoPostBack="t rue" Text="Check Me" runat="server"/>
</form>

When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.

One way of doing this is to make use of a Session variable....som ething
like this (in the ASPX page):

Sub Page_Load(..... .)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

Sub chk_Check(..... .)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

& then in the ASCX page

<script runat="server">
Sub Page_Load(..... .)
If (Session("chk") = 1) Then
txtName.AutoPos tBack = True
Else
txtName.AutoPos tBack = False
End If
End Sub
</script>

Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?

Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.

Oct 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
392
by: LRD | last post by:
Form not refreshing after autopostback unless keyboard or mouse move Hi, We created several new ASP.NET C# forms for our intranet. In each form we use panels for different sections of the form that are visible or not depending on what the user is doing. Each particular form.aspx page has a user control on it. This user control contains the form HTML and the C# code.
3
5928
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be terminated by pressing ++ and then terminate the process. I searched the entire internet and found out that there could be two things wrong (both of them are mentioned in the bug list on the access
8
9358
by: Matthew Louden | last post by:
why need to set autopostback property to be true?? I know autopostback event means to send the form to the server automatically. I tried checkbox, checkbox list, radio button, and radio button list, and they all need to set autopostback property to be true, in order to make the event of the control fires. I know this is the must, but I dont know why. Please advise! Thanks!
2
1784
by: SLE | last post by:
A web user control exposes a RadioButtonList (with AutoPostBack = true) called "Question". This control also has an embedded RequiredFieldValidator control. 1. Main ASPX page is populated with several "Questions". 2. Within the code of the main page, there is a SelectedIndexChanged handler for the RadioButtonList exposed by Question. The function loops through a number of questions on the pages and hides/shows questions (control.visible...
4
1179
by: Ethan Adams | last post by:
I have a user control that is a header that I intend to use in a number of pages. This user control contains a number of ASP list boxes and an ASP check box. I have set the Autopostback to true for these ASP controls. I then put the tag for the user control in a page and ASP.NET writes a little javascript that makes the autopostback feature work. This works fine on my development machine which has XP Pro on it. However, when I put it...
1
1773
by: John Oakes | last post by:
I have a TextBox with AutoPostBack = True. Often a user needs to click directly from this control to a RadioButtonList. Because the TextBox immediately posts back, the RadioButtonList selection doesn't get changed and the user has to click it a second time. I would have the RadioButtonList auto post back rather than the text box; however, often the user doesn't need to select the RadioButtonList, but the form still needs to post back...
4
10437
by: JJ | last post by:
Hi, I need to have a button cause an autopostback. How can I do this? I have textboxs on webform that supply values for a calc but not all will get filled at any particular time. So I need a button that signals once user is done to do an autopostback. How can I do this? Thanks, JJ
0
1454
by: riley.derrick | last post by:
To all the ASP.NET 2.0 Gurus, I am having a bit of a problem. Here is the situation. I have a created a web form that does the following. Has 2 dropdown lists that are databound to a SQL Datasource. Has 2 calendar controls with 2 dropdowns (month/year) per Calendar. The calendars are edited/modified so that the user can choose a Month and a Year for a particular calendar and the calendar control updates and displays that particular month...
0
3728
by: andy | last post by:
Hi, I have a form uses several dropdownlists to narrow a set of criteria. ( This is in turn used to control what is shown on a gridview. ) With each, the user selects an entry and then the next dropdownlist uses that control's selected value to drive what it shows. They're all set to autopostback. Everything works fine except where one of the levels only has one entry. This seems to mess up the next level down and you see the wrong
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8780
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7378
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.