Getting values from Dynamic elements.
        Posted  
        
            by nCdy
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by nCdy
        
        
        
        Published on 2010-04-14T12:14:35Z
        Indexed on 
            2010/04/14
            12:23 UTC
        
        
        Read the original article
        Hit count: 704
        
I'm adding some dynamic elements to my WebApp this way :
(Language used is Nemerele (It has a simple C#-like syntax))
  unless (GridView1.Rows.Count==0)
  {
    foreach(index with row = GridView1.Rows[index] in [0..GridView1.Rows.Count-1])
    {
      row.Cells[0].Controls.Add
                ({
                   def TB = TextBox(); TB.EnableViewState = false;
                   unless(row.Cells[0].Text == " ")
                   {
                     TB.Text = row.Cells[0].Text;
                     row.Cells[0].Text = "";
                   }
                   TB.ID=TB.ClientID;
                   TB.Width = 60;
                   TB
                });
      row.Cells[0].Controls.Add
                ({
                   def B = Button();   B.EnableViewState = false;
                   B.Width = 80;       B.Text = "?????????";
                   B.UseSubmitBehavior=false; // Makes no sense
                   //B.OnClientClick="select(5);"; // HERE I CAN KNOW ABOUT TB.ID
                   //B.Click+=EventHandler(fun(_,_) : void {  }); // POST BACK KILL THAT ALL
                   B
                });
    }
  }
This textboxes must make first field of GridView editable so ... but now I need to save a values. I can't do it on server side because any postback will Destroy all dynamic elements so I must do it without Post Back. So I try ...
  <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
  <script type="text/javascript">
        function CallPageMethod(methodName, onSuccess, onFail) {
            var args = '';
            var l = arguments.length;
            if (l > 3) {
                for (var i = 3; i < l - 1; i += 2) {
                    if (args.length != 0) args += ',';
                    args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
                }
            }
            var loc = window.location.href;
            loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "Report.aspx" : loc;
            $.ajax({
                type: "POST",
                url: loc + "/" + methodName,
                data: "{" + args + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: onSuccess,
                fail: onFail
            });
        }
        function select(index) {
            var id = $("#id" + index).html();
            CallPageMethod("SelectBook", success, fail, "id",id);
        }
  function success(response)
  {
    alert(response.d);
  }
  function fail(response)
  {
    alert("Ошибка.");
  }
  </script>
So... here is a trouble string : var id = $("#id" + index).html();
I know what is ID here : TB.ID=TB.ClientID; (when I add it) but I have no idea how to send it on Web Form. If I can add something like this div : 
<div id="Result" onclick="select(<%= " TB.ID " %>);"> Click here. </div>
from the code it will be really goal, but I can't add this element as from CodeBehind as a dynamic element.
So how can I transfer TB.ID or TB.ClientID to some static div
Or how can I add some clickable dynamic element without PostBack to not destroy all my dynamic elements.
Thank you.
© Stack Overflow or respective owner