0

I have this ASP.net component:

<asp:Button ID="Button4" runat="server" Text="Submit Entry" CssClass="eventSubmitButton" OnClick="SubmitData"/>

and I'd like to send some additional information to this SubmitData method, implemented in C#. For example, I want to send an int set in the javascript. I've read that the way to do this is extending the EventArgs class.

However, I don't really understand. Sure I can implement a class that extends EventArgs in C#, but how does that help me pass information from my Javascript?

Is there another way to pass arbitrary information during postback?

Jeremy
  • 5,365
  • 14
  • 51
  • 80

2 Answers2

1

Simply assign it to a hidden field in javascript and read it using Request.Form["FieldName"]

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • Sorry if this is a stupid question, but does it take extra time for the server to get that info from the client? Or does it already "have" that info? – Jeremy Jun 09 '11 at 16:46
  • Ha, beat me to it. Posting an extra hidden field will not be an issue. – John Kalberer Jun 09 '11 at 16:47
0

CommandArgument property for button is used to pass EventArgs, though its a ServerSide property and cannot be accessed through Javascript.

I believe, best way would be to use a hiddenfield and set value through JavaScript. Here's the sample code...

<script type="text/javascript">

function SetHiddenField()
{
document.getElementById('hfSample').value = "value to assign";
document.getElementById('form1').submit();

}

</script>


<body onload="SetHiddenField();">
<form id="form1" runat="server">
<div>
<input id="hfSample" type="hidden" value="" />
<input id="btnSubmit" type="button" onclick="SetHiddenField();" />
</div>
</form>
</body>
Sumit B
  • 300
  • 4
  • 11