Set Conditional AutoPostBack In User Control

R

rn5a

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 <form> tags. 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.
 
C

Cowboy \(Gregory A. Beamer\)

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!
*************************************************
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top