我知道我的问题与here重复。但仍然面临问题
在我的javascript代码中,我尝试了将文本分配给标签的不同方式
function fileuploadvalidation() {
document.getElementById("<%=lableid.ClientID%>").Text = "hello";
document.getElementById("<%=lableid.ClientID%>").innerHTML = "hi";
}
它的用途也可以在我的 .aspx 页面上看到文本,但当我过去在服务器端获取该文本时,它会变成空白。
.html页面代码
<asp:UpdatePanel ID="UpdatePanel16" runat="server"><ContentTemplate>
<asp:Button ID="btn_browse" runat="server" Text="Upload" OnClick="btn_browse_Click" OnClientClick="return fileuploadvalidation();" />
<asp:Label ID="lableid" runat="server" Text="" Style="font-size: small; font-weight: 400;font-family: Arial, Helvetica, sans-serif"></asp:Label>
</ContentTemplate></asp:UpdatePanel>
.cs代码
protected void btn_browse_Click(object sender, EventArgs e)
{
string abc = lableid.Text;// This is coming null
}
最佳答案
使用hidden-field
存储您设置的值,然后在后面的代码中访问它
添加隐藏字段
<input type="hidden" id ="hiddenfieldid" runat="server" />
.
function fileuploadvalidation() {
document.getElementById("lableid").value = "hello";
document.getElementById('hiddenfieldid').value= "hello";
}
然后在代码隐藏
中检索它。
string abc = hiddenfieldid.value;
关于c# - javascript中设置的标签文本未在服务器端显示标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21307427/