简体   繁体   English

JavaScript(asp.net)的神秘回发问题

[英]Mysterious postback issues with javascript (asp.net)

I show a modalpopupextender using javascript, and when I click its save button, it saves the things I have input in, and (as its a button) postsback automatically so the page refreshes. 我显示了一个使用javascript的modalpopupextender,当我单击其“保存”按钮时,它将保存我输入的内容,并(作为其按钮)自动回发,以便页面刷新。

However when it refreshes, I get an error because it says the checkbox control I define on my aspx page is null. 但是,当刷新时,出现错误,因为它说我在aspx页面上定义的复选框控件为空。

One of the first things I do when the page loads is create some javascript and feed in various clientIDs for my components, and the last one to be added is chkFullyBooked.ClientID. 页面加载时,我要做的第一件事是创建一些JavaScript,并为我的组件输入各种clientID,最后要添加的是chkFullyBooked.ClientID。

When the page loads the first time, it is ok, but after posting back it says my checkbox is null. 页面首次加载时可以,但是在发回页面后,我的复选框为空。

Another issues is that in my save button function, while it can retreive the value of textboxes and the checkbox in code, it cannot get the text from labels that have changed... I got around this by converting my labels to textboxes though. 另一个问题是,在我的“保存”按钮功能中,虽然它可以获取代码中文本框和复选框的值,但它无法从已更改的标签中获取文本……我通过将标签转换为文本框来解决了这个问题。

I just cannot figure out the reason why the checkbox control (hard-coded into my page with other textboxes) would come back as null on a page refresh. 我只是无法弄清楚复选框控件(与其他文本框一起硬编码到我的页面中)在页面刷新时返回为null的原因。

Code at the top of my aspx page: 我的aspx页面顶部的代码:

<% Response.Write("<script language='javascript' type='text/javascript'>" +

    "function DisplayModalPopup(dateCreated, note, editedBy, fullybooked) {" +

        "var modalDialog = $find('AddressErrorPopup');" +

        "document.getElementById('" + txtNote.ClientID + "').value = note;" +

        "if (modalDialog != null) { modalDialog.show(); }" +

        "var lblLastEditBy = document.getElementById('" + txtLastEditBy.ClientID + "');" +
        "lblLastEditBy.value = editedBy;" +

        "var lblDateCreated = document.getElementById('" + txtTallyBoardEntryDate.ClientID + "');" +
        "lblDateCreated.value = dateCreated;" +

        "var fullyBooked = document.getElementById('" + chkFullyBooked.ClientID + "');" +
        "fullyBooked.value = fullybooked;" +


    "} " + 
"</script>"); %>

Later on in my code, an excerpt: 稍后在我的代码中,摘录:

.
.
.
    <tr>
    <td colspan="2" style="text-align:left">Last Edit by - <asp:TextBox ID="txtLastEditBy" ReadOnly="true" runat="server"></asp:TextBox></td>
    </tr>
    <tr><td colspan="2" style="text-align:left"><asp:CheckBox runat="server" ID="chkFullyBooked" Text="Fully Booked?" Checked="false"/></td></tr>
.
.
.

Edit: This of course means I get a null reference exception on the code above as it cannot access ClientID. 编辑:这当然意味着我在上面的代码中获得了空引用异常,因为它无法访问ClientID。

Edit: As a crappy fix I did response.redirect(request.url.tostring) instead of letting the button postback itself, and this for some godforsaken reason works fine.. 编辑:作为cr脚的修复程序,我做了response.redirect(request.url.tostring)而不是让按钮回发本身,并且出于某些不可思议的原因,此方法工作正常。

Since you're "hard coding" the values for the text/checkboxes through Javascript at runtime, the ViewState has no knowledge of these values being set and so they will remain blank after a refresh or postback. 由于您在运行时通过Javascript对文本/复选框的值进行了“硬编码”,因此ViewState不了解这些值的设置,因此刷新或回发后它们将保持空白。

You'll notice that the Response.Redirect does retain your values because when that server based method is called, it goes through all the form fields and gathers their values to store in the ViewState and repopulates them when it come back from the server request. 您会注意到Response.Redirect确实保留了您的值,因为在调用基于服务器的方法时,它会遍历所有表单字段并收集其值以存储在ViewState中,并在从服务器请求返回时重新填充它们。

Have you tried loading the initial values through the Page_Load function the first time the user arrives at the page? 您是否曾尝试在用户首次访问页面时通过Page_Load函数加载初始值? This will make sure the values are stored in the ViewState when perform your actions based on the modal popup actions. 这将确保在基于模式弹出操作执行操作时,将值存储在ViewState中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM