469,609 Members | 1,179 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

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="MyPage" 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" OnCheckedChanged="chk_Check"
AutoPostBack="true" 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....something
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.AutoPostBack = True
Else
txtName.AutoPostBack = 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 4971
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**@rediffmail.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.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="MyPage" 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" OnCheckedChanged="chk_Check"
AutoPostBack="true" 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....something
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.AutoPostBack = True
Else
txtName.AutoPostBack = 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.UserControl

Public Property TextBoxAutoPostBack() As Boolean
Get
Return txtName.AutoPostBack
End Get
Set(ByVal value As Boolean)
txtName.AutoPostBack = value
End Set
End Property

End Class

Now use it in aspx page like this

<%@ Register TagPrefix="MyPage" TagName="Ctrl" Src="Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
mpc.TextBoxAutoPostBack = CType(obj, CheckBox).Checked
End Sub
</script>

<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" 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**@rediffmail.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="MyPage" 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" OnCheckedChanged="chk_Check"
AutoPostBack="true" 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....something
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.AutoPostBack = True
Else
txtName.AutoPostBack = 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by SLE | last post: by
4 posts views Thread by Ethan Adams | last post: by
1 post views Thread by John Oakes | last post: by
4 posts views Thread by JJ | last post: by
reply views Thread by riley.derrick | last post: by
reply views Thread by andy | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.