Question

I know my question is duplicate of here but still facing problem

In my javascript code I have tried different way of assigning text to label are

 function fileuploadvalidation() {
document.getElementById("<%=lableid.ClientID%>").Text = "hello"; 
document.getElementById("<%=lableid.ClientID%>").innerHTML = "hi";
}

and its use to be seen text on my .aspx page also but when I used to fetch that text on my server side it is coming blank.

.html page code

<asp:UpdatePanel ID="UpdatePanel16" runat="server"><ContentTemplate>
<asp:Button ID="btn_browse" runat="server" Text="Upload" OnClick="btn_browse_Click" OnClientClick="return fileuploadvalidation();" />&nbsp;&nbsp;
<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 code

protected void btn_browse_Click(object sender, EventArgs e)
{
    string abc = lableid.Text;// This is coming null
}
Was it helpful?

Solution

Use hidden-field to store the value that you set,then access it in code behind

add a hidden field

 <input type="hidden" id ="hiddenfieldid" runat="server" />

.

function fileuploadvalidation() {
    document.getElementById("lableid").value = "hello"; 
    document.getElementById('hiddenfieldid').value= "hello"; 
}

then retrieve it in code behind.

string abc = hiddenfieldid.value;

OTHER TIPS

I think you want to add set the value property of the label

 var lbl = document.getElementById("<%=lableid.ClientID%>")
 lbl.value = "hello"; 

try to use this

 $('#lableid').val($('#lableid').val().replace($('#lableid').val(), "hello"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top