我将在检查CheckBox后在TextBox中显示一些文本,该文本框位于更新面板之外,但我无法使其工作。请帮帮我吗?
这是我的代码:
<asp:UpdatePanel runat="server" ID="uplMaster">
<ContentTemplate>
<asp:CheckBox ID="cbShowText" runat="server" Text="Show Some Text" AutoPostBack="true"
OnCheckedChanged="cbShowText_CheckedChanged" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="txtBox" Text="Empty" runat="server" />
代码背后:
protected void cbShowText_CheckedChanged(object sender, EventArgs e)
{
txtBox.Text = "Some Text";
}
提前致谢:D
P.S。您可能已经猜到了,我已经类似于我的问题,这就是为什么我不想将TextBox放在UpdatePanel中
答案 0 :(得分:20)
我将TextBox放在另一个UpdatePanel中,然后调用Update方法:
这是我的新代码:
<asp:UpdatePanel runat="server" ID="uplMaster" UpdateMode="Always">
<ContentTemplate>
<asp:CheckBox ID="cbShowText" runat="server" Text="Show Some Text" AutoPostBack="true"
OnCheckedChanged="cbShowText_CheckedChanged" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="uplDetail" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="txtBox" Text="Empty" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
代码背后:
protected void cbShowText_CheckedChanged(object sender, EventArgs e)
{
txtBox.Text = "Some Text";
uplDetail.Update();
}
希望这有帮助
答案 1 :(得分:2)
文本框也必须在更新面板中。
*编辑:
对不起,我没有正确地阅读你的问题。也许写一个javascript函数,并从codebehind调用函数?
答案 2 :(得分:1)
我知道这已经有一段时间了,但这就是我所做的。就像@bla说的那样写一个javascript函数并从后面的代码中调用它。
因此,在您选中的已更改的电话中。 changeText是页面或脚本文件中页面上的javascript函数。
protected void cbShowText_CheckedChanged(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "Show Different Text", "changeText();", true);
}
示例Javascript。只有当被检查的更改事件从后面的代码触发时才会被调用。
<script type="text/javascript">
function changeText() {
var txt= document.getElementById('<%= txtBox.ClientID %>');
var chk = document.getElementById('<%= cbShowText.ClientID %>');
if (chk.checked === true) {
txt.Text = "Something";
} else {
txt.Text = "Somethingelse";
}
}
</script>
希望这有助于某人。