2013-04-11 60 views
0

我分配一個值到ASP文本框在JavaScript如下:asp.net和javascript

var tempStorytxt = document.getElementById("<%=txtStory.ClientID%>"); 
tempStorytxt.Value = TempValue; 
alert(tempStorytxt.Value); 

,但是當我在代碼隱藏訪問如下沒有什麼txtStory

int StoryId = int.Parse(txtStory.Text); 
+0

你什麼時候運行你的javascript代碼? – 2013-04-11 14:32:04

+0

單選按鈕 – user1871770 2013-04-11 14:34:05

+1

onchange是否解析第一次加載時的值?如果是這樣,您將無法獲得該值,因爲後面的服務器代碼首先運行。 JavaScript只會在頁面加載後纔會分配值(取決於實際運行腳本的時間)。 – 2013-04-11 14:36:35

回答

0

使用小V作爲txtStorytxt.value沒有txtStorytxt.Value

+0

但是當我使用alert(tempStorytxt.Value)時;它給了我警戒框中的正確值 – user1871770 2013-04-11 14:37:05

0

試試這個:

document.getElementById("<%= txtStory.ClientID%>").value = TempValue; 
0

試試這個;

<asp:Button ID="btnTest" runat="server" OnClientClick="setVal()" OnClick="btnTest_Click" Text="Test" /> 

你的js函數;

function setVal() 
{ 
    var tempStorytxt = document.getElementById("<%=txtStory.ClientID%>"); 
    tempStorytxt.setAttribute("value", tempValue); 
    alert(tempStorytxt.getAttribute("value")); 
} 

並在按鈕點擊事件;

int StoryId = int.Parse(txtStory.Text);