0

我使用隐藏的输入字段将值从我的 javascript 传递到后面的代码。这很好用,但是当我尝试从后面的代码中清除字段时不起作用。

<input type="hidden" id="iRowNumberTblOne" name="iRowNumberTblOne" value="" runat="server"/>

我厌倦了通过几种方法来做到这一点,但它们都不起作用。

这是最简单的方法和大多数逻辑,但它不想清除值

iRowNumberTblOne.Value = "";

我什至做了一个 javascript,所以这些值会在客户端被清除。警报第一次“在启动时”出现,但在回发后,后面的代码似乎再也找不到 javascript。

<script type="text/javascript">
        function clearInputFileds() {
        alert('test');
            document.getElementById("ctl00_contentHolder_iSiteAlias").value = "";
            document.getElementById("ctl00_contentHolder_iServiceName").value = "";
            document.getElementById("ctl00_contentHolder_iRowNumberTblOne").value = "";
            document.getElementById("ctl00_contentHolder_iRowNumberTblTwo").value = "";

        }


    </script>

这是我在代码隐藏中使用的代码

Page.ClientScript.RegisterStartupScript(GetType(), "", "clearInputFileds();", true);

你知道为什么这些方法不起作用吗?也许知道清除这些字段的更好方法?

编辑:

输入字段由 javascript 函数填充。函数在单击时运行。

function setClickedValues(siteAlias, serviceName, rowNumberTblOne, rowNumberTblTwo) {
            document.getElementById("ctl00_contentHolder_iSiteAlias").value = siteAlias;
            document.getElementById("ctl00_contentHolder_iServiceName").value = serviceName;
            document.getElementById("ctl00_contentHolder_iRowNumberTblOne").value = rowNumberTblOne;
            document.getElementById("ctl00_contentHolder_iRowNumberTblTwo").value = rowNumberTblTwo;

        }
4

2 回答 2

2

This can be done in the code behind. But it is important to do it at the right time in the ASP.Net Application Livecycle. Take a closer look at:

Usually you (re)set the value in the click event handlers of your submit button or in the LoadComplete event. If you do it before, the value will be overwritten at the time the ViewState is restored.

The following image is extremly usefull when working with ASP.Net pages and ViewState:

ASP.NET Page Life Cycle
(Source: http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx)

BTW: It is a very bad idea to reset the value in client code with hard coded ID's as they are subject to changes.

于 2013-05-13T13:47:34.580 回答
2

Since all of those are server-side elements (you have them as runat="server") the easiest way to clear the values is simply to do:

elementID.Value="";

On all of them after postback. If you are saying that the values are not being cleared after doing the above, it's probably because you are not checking if(!IsPostback) before executing the function that populates the values in the first place.

Your client side code should also work but it's probably failing because your last parameter to the RegisterStartupScript is true, which indicates that the <script> tags ought to be added. This may cause a problem because your function is already enclosed in <script> tags so the last true parameter is not needed in this case.

于 2013-05-13T13:47:39.850 回答