HiddenField value not being set

I am trying to retrieve 4 hiddenfield values from a page that are being set using JCrop. Standard HTML input 'text' fields are being set with the correct values but not ASP:HiddenField items.

Either the JS setting the values on the HiddenFields is not correct or it is my call to retrieve the value that is incorrect...

Dim xpos As Integer = CInt(X.Value)

errors telling me;

System.FormatException: Input string was not in a correct format. This is I THINK because it is blank!

Thaks in advance
<script language="Javascript" type="text/javascript">
	    jQuery(document).ready(function () {
	        jQuery('#cropbox').Jcrop({
	            aspectRatio: 1,
	            onSelect: updateCoords,
                onChange: showCoords,
                onSelect: showCoords,
                onChange: updateCoords
		    });
	    });

	    function updateCoords(c) {
	        jQuery('#X').val(c.x);
	        jQuery('#Y').val(c.y);
	        jQuery('#W').val(c.w);
            jQuery('#H').val(c.h);
		};

		function showCoords(c) {
		    jQuery('#x1').val(c.x);
		    jQuery('#y1').val(c.y);
		    jQuery('#x2').val(c.x2);
		    jQuery('#y2').val(c.y2);
		    jQuery('#w2').val(c.w);
		    jQuery('#h2').val(c.h);
		};
    </script>

<img src="/uploads/users/temp/<%=Request.Querystring("FileName") %>" id="cropbox" alt="Crop image"/><br />
                <label>X1 <input type="text" size="4" id="x1" name="x1" /></label>
			    <label>Y1 <input type="text" size="4" id="y1" name="y1" /></label>
			    <label>X2 <input type="text" size="4" id="x2" name="x2" /></label>
                <label>Y2 <input type="text" size="4" id="y2" name="y2" /></label>
                <label>W <input type="text" size="4" id="w2" name="w" /></label>
                <label>H <input type="text" size="4" id="h2" name="h" /></label>
                <asp:HiddenField ID="X" runat="server" />
                <asp:HiddenField ID="Y" runat="server" />
                <asp:HiddenField ID="W" runat="server" />
                <asp:HiddenField ID="H" runat="server" />


 Protected Sub btnCrop_Click(ByVal sender As Object, ByVal e As EventArgs)
        If IsPostBack Then
            Dim xpos As Integer = CInt(X.Value)
            Dim ypos As Integer = CInt(Y.Value)
            Dim width As Integer = CInt(W.Value)
            Dim height As Integer = CInt(H.Value)

            Dim NewImage As Image = Bitmap.FromFile(Server.MapPath("/uploads/users/temp/") & "\" & Request.QueryString("FileName"))
            Dim BMP As Bitmap = New Bitmap(width, height, NewImage.PixelFormat)
            Dim g As Graphics = Graphics.FromImage(BMP)
            g.DrawImage(NewImage, New Rectangle(xpos, ypos, width, height), New Rectangle(xpos, ypos, width, height), GraphicsUnit.Pixel)
            BMP.Save(Server.MapPath("/uploads/users/") & "\" & Request.QueryString("UserID") & ".jpg", NewImage.RawFormat)
            CroppedImage.Visible = True
            CroppedImage.ImageUrl = "~/uploads/users/" & Request.QueryString("UserID") & ".jpg"
        End If
    End Sub

Open in new window

swgdesignAsked:
Who is Participating?
 
Kiran SonawaneProject LeadCommented:
You need access asp.net control like below

 function updateCoords(c) {
          jQuery('#<%=X.ClientID%>').val(c.x);
          jQuery('#<%=Y.ClientID%>').val(c.y);
          jQuery('#<%=W.ClientID%>').val(c.w);
            jQuery('#<%=H.ClientID%>').val(c.h);
    };

    function showCoords(c) {
        jQuery('#<%=x1.ClientID%>').val(c.x);
        jQuery('#<%=y1.ClientID%>').val(c.y);
        jQuery('#<%=x2.ClientID%>').val(c.x2);
        jQuery('#<%=y2.ClientID%>').val(c.y2);
        jQuery('#<%=w2.ClientID%>').val(c.w);
        jQuery('#<%=h2.ClientID%>').val(c.h);
    };
0
 
Kiran SonawaneProject LeadCommented:
One mistake in above. showCoords should be your original one since it is simple html control i.e.

function showCoords(c) {
        jQuery('#x1').val(c.x);
        jQuery('#y1').val(c.y);
        jQuery('#x2').val(c.x2);
        jQuery('#y2').val(c.y2);
        jQuery('#w2').val(c.w);
        jQuery('#h2').val(c.h);
    };
0
 
Easwaran ParamasivamCommented:
Debug the JS function updateCoords before postback. If it not set then the hidden fields value will be blank. This can't be converted into int. However use TryParse method of type for conversion.

http://www.dotnetperls.com/integer-tryparse-vbnet

If it is blank then set default values as per your need.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.