Skip to content

Instantly share code, notes, and snippets.

@evagoras
Last active September 28, 2023 09:40
Show Gist options
  • Save evagoras/f643d1d62c0cc3c75cd0fafdc4e20c5f to your computer and use it in GitHub Desktop.
Save evagoras/f643d1d62c0cc3c75cd0fafdc4e20c5f to your computer and use it in GitHub Desktop.
How postback works in ASP.NET
<script language="VB" runat="server">
Sub CreateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim strFileName As String = funcParam.Value
'custom code to create a text file
End Sub
</script>
<html>
<head>
<script language="JavaScript">
//ask for user input and then create file
function CreateFile() {
//get filename from the user
var fileName = prompt('Type the name of the file you want to create:','');
//if the user clicks on OK and if they have entered something
if ((fileName) && (fileName!="")) {
//save the filename to the hidden form field 'funcParam'
document.forms['myForm'].elements['funcParam'].value = fileName;
//call the postback function with the right ID
__doPostBack('CreateFile','');
}
}
</script>
</head>
<body>
<form runat="server" id="myForm">
<a href="javascript:CreateFile();">Create Text file</a>
<asp:linkbutton id="CreateFile" runat="server" onclick="CreateFile_Click" />
<input type="hidden" runat="server" id="funcParam">
</form>
</body>
</html>
...
<asp:linkbutton id="Test" runat="server" text="Create Text file" onclick="Test_Click" visible="false" />
...
<html>
<body>
<form name="myForm" method="post" action="test.aspx" id="myForm">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE"
value="dDwtMTAwOTA0ODU1NTs7PsllCK1DzvZsK0J6OQ2dxKqmEwVs" />
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform = document.myForm;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
<a id="Test" href="javascript:__doPostBack('Test','')">Create Text file</a>
</form>
</body>
</html>
<script language="VB" runat="server">
Sub Test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'enter your code to perform
End Sub
</script>
<html>
<body>
<form runat="server" id="myForm">
<asp:linkbutton id="Test" runat="server" text="Create Text file" onclick="Test_Click" />
</form>
</body>
</html>
@evagoras
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment