我有一个Javascript函数,它在form1上的localstorage中存储一个Idnumber,并在form2上加载时将localstorage数据检索到一个文本框中。
一切都能正常工作,但是在form2上,我也有一个代码,它必须使用文本框OnPageload事件中的IDnumber从SQL数据库中选择一个数据,但它不工作。
所以我意识到,当from2被加载时,codebehind (onpageload)事件首先被触发,然后才是Javascript,而Javascript应该是把IDnumber放到文本框中的,请问我被卡住了,我不知道该怎么做,任何帮助都将是非常感激的。
问题是,我需要java脚本在sever端代码被触发之前,先将Idnumber加载到文本框中。
这是我在form1上的javascript
$(document).on("click", ".Laundry-Cont", function () {
var classval = $(this).children("input").val();
// var getInput = prompt("Hey type something here: ");
sessionStorage.setItem("storageName", classval);
$(document.getElementById('<%=mnn.ClientID%>')).val(sessionStorage.getItem("storageName"));
// window.onload = alert(localStorage.getItem("storageName"));
//$('#mn').val(localStorage.getItem("storageName"));
window.location.href = '/proudctselection.aspx'
});
//this is my script on form2
$(document.getElementById('<%=txtpicproductid.ClientID%>')).val(sessionStorage.getItem("storageName"));
});
//this is my SQL query on form2 in codebehind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If cn.State = ConnectionState.Open Then
cn.Close()
End If
Dim selproduct As New SqlCommand("SELECT *FROM allproducts WHERE ItID = @productid")
selproduct.Connection = cn
selproduct.Parameters.AddWithValue("@productid", SqlDbType.BigInt).Value = iidd
Dim selproductda As New SqlDataAdapter(selproduct)
Dim selproductdt As New DataTable
selproductda.Fill(selproductdt)
For Each rw As DataRow In selproductdt.Rows
Me.productmainimage.ImageUrl = rw.Item(6).ToString
Next
cn.Close()
End Sub
这是不可能的,因为页面必须在JavaScript显示之前完全加载,所以你不能让JavaScript在Page On Load事件之前运行。
你可以用几种不同的方式将ID传递给表单2。
还可以回顾一下,了解更多的技巧和想法。 从另一个页面获取价值