Search code examples
c#javascriptwebformscode-behind

accessing javascript dynamic attributes from codebehind


I'm new in ASP and I am stucked with my ascx control. I have a control Upload.ascx with the following init code

 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Uploader.ascx.cs" Inherits="Uploader" %>
 <script type="text/javascript" > 
 $(function ()
 {
    var guid = GUID();//GUID() - js function which generates GUID
    $('#' + '<%= OuterId %>').attr('guid', guid);
 }
 </script>
 <div class="buttonNoHover" runat="server" id="current">Upload</div>  

and cs init code (part of it)

 protected void Page_Load(object sender, EventArgs e)
 {
    current.ID = this.ID;
 }

My control declaration looks like the following

 <my:Uploader ID="upload" OuterId="upload" ClientIDMode="Static" runat="server" /> 

In my JS I can access the attribute $('#upload').attr('guid') and it gives correct value. But when I submit the form, my upload.Attributes["guid"] becomes null in codebehind. How can I access it and what am I doing wrong?


Solution

  • You cannot do it like this:

    There are two ways:

    Pass the GUID from the server and store in some literal and access the literal using JQuery etc

    OR

    Create an server side hidden field and assign the value.

    <asp:HiddenField runat="server" ID="hidden" />
    
    $('[id$=hidden]').val(guid);
    

    On submit access the hidden field