开发者

embed javascript variable in asp.net control id

I'm working with a table of asp.net controls on client side. They are all named in the following fashion when they are created on server side: "txt_name_" + rowNum. I know I can access them by their id by using "<%=Control.ClientID%>". On client side I have access to current row num via a jav开发者_如何学编程ascript variable. I'm wondering how I can access one of the controls on client side by using something like the following(which doesn't seem to work): "<%=txt_name_" + jsRowNumVar + ".ClientID%>". so I'm essentially trying to substitute a javascript variable as part of an asp.net control name and it doesn't seem to be working. Any ideas on how I would accomplish this?


It does not work like this the <% ... %> stuff is evaluated on the server and comes to the client as html. Placing the name of the javascript variable there will do no good for you


As an alternative, you can render your rows' client ids with an javascript array, and you can use it with the row index.

string jsToRender = "<script>var myCtrlIds = new Array('" + 
         text0.ClientId + "', '" + 
         text1.ClientId + "', '" + 
         text2.ClientId + "');</script>";
Page.ClientScript.RegisterStartupScript(typeof(MyPage), "jsKey", jsToRender);

Then use the control id array in your client scripts :

<script>
      var text0Value = document.getElementById(myCtrlIds[0]).value;
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜