If you do not want to allow special characters then please refer this link
I have used the above code JavaScript in this below article.
Please refer this code
C#
private void CreateTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
txt.Attributes["onkeypress"] = "return IsAlphaNumeric(event);";
txt.Attributes["ondrop"] = "return false;";
txt.Attributes["onpaste"] = "return false;";
pnlTextBoxes.Controls.Add(txt);
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBoxes.Controls.Add(lt);
}
Add this JavaScript in the page
JavaScript
<script type="text/javascript">
var specialKeys = new Array();
specialKeys.push(8); //Backspace
specialKeys.push(9); //Tab
specialKeys.push(46); //Delete
specialKeys.push(36); //Home
specialKeys.push(35); //End
specialKeys.push(37); //Left
specialKeys.push(39); //Right
function IsAlphaNumeric(e) {
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
document.getElementById("error").style.display = ret ? "none" : "inline";
return ret;
}
</script>