将代码后面的变量值传递给javascript

时间:2011-05-31 21:25:16

标签: javascript asp.net .net-3.5

我的.aspx页面中有一个隐藏变量。

  input type="hidden" runat="server" id="isdup" 

现在在代码后面我检查某些条件并相应地分配isdup值。但是,这可能对你没什么帮助,但这就是我在代码背后所做的事情。

            bool exist = (from n in mCDC.NCDCPoints
                          where n.EVENT_TYPE_ID == eventID
                          where n.BeginDate == begin
                          where n.EndDate == end
                          select n).Count() > 0;

    try
    {
        if (!exist)
        {
            //do this before insert so the insert will have correct values
            isdup.Value = "false";
            SaveAllColumnFields(ref ncdc, e);
            mCDC.NCDCPoints.InsertOnSubmit(ncdc);
            mCDC.SubmitChanges();
            //do this after insert because it wont work until the ncdc object
            //has been assigned an ID
            SaveAllDynamicFields(mCDC, ref ncdc, e);
            mCDC.SubmitChanges();
            Grid1.CurrentPageIndex = 0;
        }
        else
        {
            isdup.Value = "true";
            System.Windows.Forms.MessageBox.Show(isdup.Value);
        }

现在我需要访问javascript中的isdup。但问题是这些值没有传递,isdup为空。

    var showus= document.getElementById("<%=isdup.ClientID %>").value;
    alert(showus);
    if(showus == "true")
    {
      Showduplicate();
    }

那么,请让我知道我一直在做的错误?

2 个答案:

答案 0 :(得分:1)

你曾尝试过:

  var showus= document.getElementById('<%=isdup.ClientID %>').value;

<强>更新

是页面末尾的javascript吗?

<强>更新

尝试将此代码放在页面中:

 <asp:HiddenField ID="isdup" runat="server"  Value="eee"/>
    <script>
        var showus = document.getElementById("<%=isdup.ClientID %>").value;
        alert(showus);
</script>

这适合我!

<强>更新

在page_load中

...

   protected void Page_Load(object sender, EventArgs e)
    {

if (!ClientScript.IsStartupScriptRegistered("clientscript"))
{
   string script1 = "<script language=JavaScript>";
   script1 += "var showus= document.getElementById('" + isdup.ClientID + "').value;";
   script1 += "alert(showus);";
   script1 += "</script>";

   ClientScript.RegisterStartupScript(typeof(Page), "clientscript", script1);
}

我的例子:

 protected void pagesTree_NodeClick(object sender, RadTreeNodeEventArgs e)
   {
        PageStructure page = pageService.GetPage(Guid.Parse(e.Node.Value));

        this.LoadPageData(page);

        isdup.Value = "xxx";
    }

<强>更新

         bool exist = (from n in mCDC.NCDCPoints
                      where n.EVENT_TYPE_ID == eventID
                      where n.BeginDate == begin
                      where n.EndDate == end
                      select n).Count() > 0;

 if (!ClientScript.IsStartupScriptRegistered("clientscript"))
{
   string script1 = "<script language=JavaScript>";
   script1 += "var showus= document.getElementById('" + isdup.ClientID + "').value;";
   script1 += "alert(showus);";
   script1 += "</script>";

   ClientScript.RegisterStartupScript(typeof(Page), "clientscript", script1);
}


try
{
    if (!exist)
    {
        //do this before insert so the insert will have correct values
        isdup.Value = "false";
        SaveAllColumnFields(ref ncdc, e);
        mCDC.NCDCPoints.InsertOnSubmit(ncdc);
        mCDC.SubmitChanges();
        //do this after insert because it wont work until the ncdc object
        //has been assigned an ID
        SaveAllDynamicFields(mCDC, ref ncdc, e);
        mCDC.SubmitChanges();
        Grid1.CurrentPageIndex = 0;
    }
    else
    {
        isdup.Value = "true";
        System.Windows.Forms.MessageBox.Show(isdup.Value);
    }

答案 1 :(得分:0)

试试这个JQuery代码。

var showus= $("#<%=isdup.ClientID %>").val();

替换您的input字段并使用jquery代码尝试此操作
更新

<asp:HiddenField ID="isdup" runat="server" EnableViewState="true" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"/>
<script type="text/javascript" language="javascript">
    $(document).ready(function() {
    var showus = $("#<%=isdup.ClientID %>").val();
    alert(showus);
    if (showus == "true") {
        Showduplicate();
     }
    });
</script>