Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I want to move some parts of the html page containing this function to a new web user control. now "form1" is not defined and it is not working correctly without any error message!
I appreciate any help for my problem...

admin.aspx:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="admin.aspx.cs%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<script language="javascript" src="encryption.js"></script>
<script language="javascript">
    function encPass()
    {
    if(form1.txtPassword.value=="")
        {
        alert("Please Enter your Password!");
        return false;
        }
    form1.txtPasswordHash.value=MD5(form1.txtPassword.value);
    form1.txtPassword.value="";
    return true;
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
        <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
        <tr><td id="tdRootContainer" runat="server" height="590" align="right" valign="top"></td></tr>
        </table>
    </form>
</body>
</html>


admin.aspx.cs:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            string frame = helper.request(Request, "f");
            if (frame == "")
                frame = "adminUsers";

            Control ucRoot = LoadControl("userControls/" + frame + ".ascx");
            tdRootContainer.Controls.Add(ucRoot);
        }


adminUsers.ascx:
XML
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="adminUsers.ascx.cs"%> <asp:TextBox ID="txtPasswordHash" runat="server" Width="500"></asp:TextBox>
<TABLE cellSpacing="0" cellPadding="0" width="800" border="0">
<TR height="30" valign="top">
        <TD><asp:TextBox id="txtUsername" Runat="server" Width="200"></asp:TextBox></TD>
    </TR>
<TR height="30" valign="top">
        <TD><asp:TextBox id="txtPassword" Runat="server" Width="200" TextMode="Password"></asp:TextBox></TD>
    </TR>
    <TR>
        <TD colSpan="2"><asp:Button ID="btnSave" runat="server" Text="ذخیره" onclick="btnSave_Click" /></TD>
    </TR>
</TABLE>


adminUsers.ascx.cs
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                changeMode("list");
                btnSave.Attributes["onclick"] = "return encPass();";
                buildList();
            }
        }


Regards,
Sheryi26
Posted
Updated 30-Dec-10 21:40pm
v14
Comments
Manfred Rudolf Bihy 28-Dec-10 13:26pm    
Updated my answer with a code example to use in "code behind".
Ankur\m/ 30-Dec-10 8:27am    
Please use <PRE> tags ('code block' option above editor window) to wrap the code snippet.
You may also select 'Auto Detect' / 'Wrap in PRE Block' checkbox below the editor to automatically detect the code block when you paste some code.
sheryi26 30-Dec-10 9:03am    
Dear Ankur, thanks for your comment... I did. Regards
Manfred Rudolf Bihy 30-Dec-10 13:06pm    
Thanks Sheryi for posting your code. I already had a first glimpse, but must now return to other work. I'll try to get back to you tomorrow so hang tight, all is not lost :).
sheryi26 30-Dec-10 13:32pm    
Dear Manfred, Thanks :)

Javascript is client side code and can thus not be executed in "code behind", unless of course you're ready to fire up a javascript interpreter in your "code behind". That should be avoided for all kinds of reasons, one of them being a performance issue.

What you can do though is to have the MD5 calculated when the document is loaded in the browser and then populate the field txtPasswordHash, which would of course defeat what you're trying to do, namely not sending the password in clear text.

Please use this in your code behind:

C#
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();

byte[] data = System.Text.Encoding.ASCII.GetBytes(Value);

data = x.ComputeHash(data);

return System.Text.Encoding.ASCII.GetString(data);



Cheers,

Manfred
 
Share this answer
 
v2
Comments
AspDotNetDev 28-Dec-10 13:31pm    
My vote of 5: well thought out answer specific to the OP's needs. More impressive and useful than a general "you can't do that" type answer.
Manfred Rudolf Bihy 28-Dec-10 13:52pm    
Thanks for the compliment!
sheryi26 28-Dec-10 15:08pm    
Dear Manfred,
thanks... I read many subjects for the problem, then as you told, it is not advised to fire up a javascript interpreter in our "code behind"...
I had another problem which to solve it, I had reached to this topic.
now I describ it, maybe you can help me.

I had this script in user interface, "encPass" function was called in behind code and it had worked properly.

<script language="javascript" src="encryption.js"></script>
<script language="javascript">
function encPass()
{
if(form1.txtPassword.value=="")
{
alert("Please Enter your Password!");
return false;
}
form1.txtPasswordHash.value=MD5(form1.txtPassword.value);
form1.txtPassword.value="";
return true;
}
</script>

then I wanted to move some parts of the html page containing this function in behind code to a new user control.
now "form1" is not defined and it is not working correctly!
can help me about this problem?
As I understand it you have created an ASP.NET user control that renders HTML output. So when the corresponding javascript is generated during the HTML rendering process you have to make sure that the correct form is referenced from your (generated) javascript.
Your custom control contains a form (or may be multiple forms? ) and this form has an id (see ClientID on MSDN). If the javascript code running on the client side must access the form it has to know how to find it on the page.

You might also look to use jQuery as you can wonderfully select HTML elements with it.
Example:
JavaScript
var myFormElement = $("form#yourFormsClientID");


Hope this helps some!
Leave a comment if you still have some doubts.

Cheers,

Manfred
 
Share this answer
 
Comments
sheryi26 30-Dec-10 9:48am    
I had changed my script to this, I don't know taht I understand your help correctly or not! but it did not answered... I am working on it...

<script language="javascript">
function encPass()
{
var tb1 = document.getElementById("txtPassword");
var tb2 = document.getElementById("txtPasswordHash");

if(tb1.value =="")
{
alert("Please Enter your Password!");
return false;
}
tb2.value=MD5(tb1.value);
tb1.value="";
return true;
}
</script>
Manfred Rudolf Bihy 30-Dec-10 9:59am    
Are "txtPassword" and "txtPasswordHash" the IDs of your input boxes? If not the code will not work.
document.getElementById fetches the HTML element if you give it the correct id of the element as given below:
<input name="test" id="txtPassword" value="" type="text"/>
When the HTML in your control if rendered you'll have to make sure the javascript part is generated with the appropriate ClientID value of the corresponding input controls.
sheryi26 30-Dec-10 10:47am    
yes,"txtPassword" and "txtPasswordHash" are the IDs of my input boxes, I edited my question again and copy all of my 2 pages' code...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900