2013-04-26 63 views
0

我在这里有这个代码。选择的索引是我的C#程序中的文本框。我的问题是在将SelectedIndex的文本传送给函数之前,需要点击两次Show Popup按钮。我真的不知道它是否因为getElementByID()。innerHTML()。 Javascript getElementByID()。innerHTML()在第二次点击上工作

<asp:Button ID="Button1" runat="server" Text="Show Popup" 

    OnClientClick="ShowPopUp('#SelectedIndex');" onclick="Button1_Click1" /> 



<script type="text/javascript"> 

    ShowPopUp = function() { 

     var x = document.getElementById('<%=SelectedIndex.ClientID %>').innerText; 

     window.showModalDialog('CopyFiles/'+x, window, 'dialogWidth:800px;dialogHeight:800px;center:yes;resizable:0;status:0;scrollbars:no;menubar:0;titlebar:no;toolbar:0;'); 

    } 

的人?谁能帮我? :X预先感谢。

+0

糟糕的设计 - 在您按下按钮,在打开的窗口,充分回发指数变化A.您再按一次,现在你得到了A,现在的窗口中正确打开,但你有一个更多回帖。重新设计你的步骤。 – Aristos 2013-04-26 01:59:10

回答

1

看起来好像是因为选定的索引直到单击按钮并发生回发之后才被设置。而不是使用服务器端变量设置getElementById的值......而是使用javascript获取所选下拉列表的值。

例如。一些代码在javascript中获取所选下拉值。

<select id="ddl"> 
    <option value="1">one</option> 
    <option value="2">two</option> 
</select> 

var myDDL = document.getElementById("ddl"); 
var val = myDDL.options[myDDL.selectedIndex].value; 

一些小的调整,你的代码(未测试)。

<asp:Button ID="Button1" runat="server" Text="Show Popup" 
    OnClientClick="ShowPopUp();" onclick="Button1_Click1" /> 

<script type="text/javascript"> 

    function ShowPopUp() 
    { 
     var myDDL = document.getElementById("ddl"); 
     var val = myDDL.options[myDDL.selectedIndex].value;  

     //var x = document.getElementById('ddl').innerText; 

     window.showModalDialog('CopyFiles/' + val, window, 'dialogWidth:800px;dialogHeight:800px;center:yes;resizable:0;status:0;scrollbars:no;menubar:0;titlebar:no;toolbar:0;'); 

    } 
+1

非常感谢!有用。 :) – 2013-04-26 02:54:11

+0

我的荣幸。 :) – 2013-04-26 02:58:48

+1

不要忘记标记正确的答案。它也可能会阻止其他人。 – 2013-04-26 06:35:10