I have a checkbox that is set to "false" as default in the aspx. Then there is a function that determines whether it is true or false, the prob开发者_高级运维lem is that when on the page there is a textbox and this textbox has a TextChange Event on it, when i type something in the textbox and then go to the checkbox which at this time is visible the first time i click the whole page postbacks and resets the checkbox to unchecked.. then i need to click it again and then it sticks.. what can I do to make it stick at the first click?
Can I use some javascript for this or what do you think my options are?
protected void myTextbox_TextChanged(object sender, EventArgs e)
{
ShowCheckBox(true);
}
private void ShowCheckBox(bool ckVal)
{
myCheckBox.Visible = ckVal;
}
why not add the textbox inside an update panel something like this:
<asp:UpdatePanel runat="server" ID="test">
<ContentTemplate>
<asp:TextBox ID="TEXTBOX" runat="server" Visible="true" AutoPostBack="true"
OnTextChanged="checkUser" >Page Name</asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TEXTBOX" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
My best guess is that your check box initialization (what sets Checked to false) runs on every postback. E.g. change this
protected void Page_Load(object sender, EventArgs e)
{
myCheckBox.Checked = false;
}
to this
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
myCheckBox.Checked = false;
}
However, we'll need to see some more code before we can effectively assist you. Part of the issue might be that ViewState is not persisted when the control is not Visible. If all that you want to do is show/hide it, use javascript as others have suggested.
Here's an example with jQuery:
$(document).ready(function (){
$("#myTextBox").change(function() {
$("#myCheckBox").show();
});
});
If i was you,i would write some clode in client side to show the check box or hide it, writting suck code in server side is not good and needs extra roundtrip also you can use an updatepanel to do that
Checkbox, if .Visible = false
is not rendered to the client. So when postback happens the default value of "false" is what it's set to.
You can render control all the time and toggle visibility via CSS so the checkbox is always part of the control tree and its ViewState is persisted.
style="display:none"
and style="display:inline"
You did not post the whole code so I am just going to guess. Try setting the AutoPostBack property for the textbox to false and see if that fixes it.
If that does not do it please post your code or a full sample code that reproduces the problem.
精彩评论