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.
<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.