0

I have a hidden variable in my .aspx page.

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

Now in code behind i check for certain conditions and assign isdup a value accordingly. However, this may not help you much but this is what i do in code behind.

            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);
        }

Now I need to access the isdup inside javascript. However the problem has been that those values are not passed and isdup is null.

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

So, kindly let me know the mistake i have been doing?

7
  • What version of .net? 4.0 resolved these types of issues Commented May 31, 2011 at 21:26
  • 3
    Why are you using System.Windows.Forms.MessageBox is an ASP.net application? Commented May 31, 2011 at 21:27
  • how do you pass isdup to the view? Commented May 31, 2011 at 21:27
  • @Cybernate: I just used it to check the values. Commented May 31, 2011 at 21:29
  • @Chris Lively: I'm using .net 3.5 Commented May 31, 2011 at 21:32

2 Answers 2

2

Hve you tried with:

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

update

is javascript at the end of the page?

update

try to put this code in the page:

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

this works for me!

update

in 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);
}

my example:

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

        this.LoadPageData(page);

        isdup.Value = "xxx";
    }

update

         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);
    }
Sign up to request clarification or add additional context in comments.

12 Comments

No, it is not the end of page
Try move your javascript after the hidden field... like at the end of the page!
I just tried this thing.. It didn't.... It shows 'eee' but not the value from code behind.
The thing has been that i have been updating isdup value outside the main.... So, do you think thats a mistake?
I've update the post and now it read the value from server code!
|
0

Try this JQuery code.

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

Replace your input field and try this with jquery code
UPDATED

<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>

4 Comments

The thing has been that i have been updating isdup value outside the main.... So, do you think that's a mistake?
@Nishanth: I have updated my answer, just copy-past that code and try it.
@Nishanth: this is because you are not able to get jquery file reference.
@Nishanth: goto code.jquery.com/jquery-1.4.1.min.js and save file as jquery-1.4.1.min.js and then add this file into your solution explorer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.