[英]How to Change ASP.NET Label Text from C#?
This seems to be a simple question, but I just can't figure it out.这似乎是一个简单的问题,但我就是想不通。
I have a DropDownList given value 1 to 4 to each of its options, I've also set OnSelectedIndexChanged to the DropDownList in order to trigger the text in a Label to change.我有一个 DropDownList 的每个选项的值都为 1 到 4,我还将 OnSelectedIndexChanged 设置为 DropDownList 以触发标签中的文本更改。
Now the issue is when the DropDownList option is changed, the Label text doesn't change.现在的问题是当 DropDownList 选项更改时,标签文本不会更改。 I have no idea which part of my code causes this issue, especially when the browser console returns no error.
我不知道我的代码的哪一部分导致了这个问题,尤其是当浏览器控制台没有返回错误时。
Thanks in advance.提前致谢。
Here's my code: The DropDownList这是我的代码:DropDownList
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="form-inline">
<asp:DropDownList ID="ddl_basic_layer_select" runat="server" CssClass="form-control" Style="width: auto;" OnSelectedIndexChanged="ddl_basic_layer_select_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="1">ArcGIS</asp:ListItem>
<asp:ListItem Value="2">Web Tile</asp:ListItem>
<asp:ListItem Value="3">WMS</asp:ListItem>
<asp:ListItem Value="4">WMTS</asp:ListItem>
</asp:DropDownList>
</div>
</ContentTemplate>
</asp:UpdatePanel>
The Label标签
<div class="col-lg-9">
<asp:Label ID="url_tips" class="color_red" runat="server" Text="initial text" />
</div>
C# C#
protected void ddl_basic_layer_select_SelectedIndexChanged(object sender, EventArgs e)
{
string type = ddl_basic_layer_select.SelectedItem.Value;
if (type == "1")
{
url_tips.Text = "1111111111111";
}
if (type == "2")
{
url_tips.Text = "2222222222222";
}
}
You need to put label inside another UpdatePanel and set the UpdateMode to conditional.您需要将标签放入另一个 UpdatePanel 并将 UpdateMode 设置为条件。 UpdatePanel causes selected parts of a page to "refresh".
UpdatePanel 使页面的选定部分“刷新”。 In order to trigger a change outside "selected" parts which is label in your case, need to create another UpdatePanel.
为了在您的案例中的标签“选定”部分之外触发更改,需要创建另一个 UpdatePanel。
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode=Conditional>
<ContentTemplate>
<asp:Label ID="url_tips" class="color_red" runat="server" Text="initial text" />
</ContentTemplate>
</asp:UpdatePanel>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.