Solution to ASP.NET, Javascript value modification problem
February 20th, 2006 // 4:14 pm @ Amar
Sorry for being late. Was tied up in work. So lets start where I left off before. The main problem which I found was that if the ASP.NET button was clicked, and I wanted to run some client side Javascript to update the values of hidden form elements and get the updated values on the postback event handler for that same button, it ignored all the changes javascript made and did not reflect to the code behind. My target was to somehow reflect the changes to the values to the back end.
The solution was not a very elegant one, and I am sure there has to be a different solution to this. But at least it works and gets the job done.
What I did was that I changed by ASP Button in page B to a LinkButton. I assigned blank text to it so effectively making a hidden link button.
<A href=”javascript:SendMail();”>Send Mail</A>
<asp:linkbutton id=SendMail runat=”server”></asp:linkbutton>
This enabled me to call a Javascript when the button was clicked, and also allowed me to have a code behind event handler for the link button click. As the link button is effectively hidden and hence cannot generate an event for the link button click on the back end, we have to raise this event manually in order for this code to work.
The next step would be to implement the SendMail() javascript function.
<script language=”javascript”>
function SendMail()
{
// save the values of the hidden variables from page A
// to the hidden variables in page B
// then call a postback with the id of our SendMail link button
__doPostBack(’SendMail’,’’);
}
This enables the javascript to store the value to the hidden variables first and then call a manual postback which would now take the new updated values and reflect it on the code behind. Effectively the user only has to click one button, which would first call a Javascript, change the values of the hidden form fields, and then do a postback with the new values reflected at the code behind.
Posted at DotNetJunkies on Wednesday, November 17, 2004 6:50 AM
Category : dotNet