Sunday, March 25, 2012

A couple of, I hope Basic Questions RE SqlDataSources and scripting

Greetings!

I am writing a proof of concept ASP.net application for my employer and I have run into a couple perplexing issues. First off, In my Page_Load event I have the following code.

txOTHrs.Attributes[

"onBlur"] ="return calculateTotal(this)";

The onblur event handler is coded as follows

function calculateTotal(tb)

{

var regHrs = document.getElementById ("txRegHrs").value; var otHrs = document.getElementById("txOtHrs").value; var rate = document.getElementById("txRate").value; var regAmt = rate * regHrs; var otAmt= rate * ( otHrs * 1.5 ); var total = otAmt + regAmt;

document.getElementById(

"txTotal").value = total;

}

Everything seems to work fine. The onBlur event is handled and, as I expect the value in the txTotal text box is updated correctly. The problem occurs when I try to access that value in the codebehind page. When ever I look at txTotal.Text in the debugger it is always blank. This has left me scratching my head as I can clearly see a value on the web page. Have I missed some step where I have to notify the server that a value in the control has changed ?

My second question is, I hope a very easy one to answer. In WinForms when I have a dataset I can access the individual rows ( in an untyped dataset ) by speficying dataset.tables["tableName"].Rows[index]. Is there any similar mechanism for accessing the individual rows, and fields in the SqlDataSource object in ASP.net ?

Any help anyone can provide is greatly appreciated!!

If you are comming from a "Windows Forms" world, then there are a few key concepts that you are going to need to understand... most especially is the "Page Lifecycle" which I will mention further down... but to answer your questions quickly:

sabrecat:

Have I missed some step where I have to notify the server that a value in the control has changed ?

- Yes... you need to "PostBack" to the server at any time that you want to reflect changes.

sabrecat:

In WinForms when I have a dataset I can access the individual rows ( in an untyped dataset ) by speficying dataset.tables["tableName"].Rows[index]. Is there any similar mechanism for accessing the individual rows, and fields in the SqlDataSource object in ASP.net ?

- Again, Yes... here is a small example:

DataView myDataView = (DataView)this.MarketDataSource.Select(DataSourceSelectArguments.Empty);

foreach (DataRow rowin myDataView.Table.Rows)
{
// do something here :)
}

// Or you can do...

string firstPersonsLastName = (string)myDataView.Table.Rows[0]["LastName"];

Here is the "Page Lifecycle" that I mentioned earlier:
http://msdn2.microsoft.com/en-us/library/ms178472.aspx
http://www.eggheadcafe.com/articles/o_aspNet_Page_LifeCycle.jpg

Read the MSDN article, save that picture to your desktop and look at it often :)

I hope this helps.

Peace,

|||

Thank you very much for the reply! You have been tremendously helpful!

I thought that the data that I was putting into the control wasnt getting back to the serve. I wasnt sure what mechanism to use however to get it into the server. To actually perform thepostback I would use the __doPostBack function that ASP.Net puts into the page right ? I mean in my script I'd do somthing like

function calculateTotal(e, tb)
{
var regHrs = document.getElementById ("txRegHrs").value;
var otHrs = document.getElementById("txOtHrs").value;
var rate = document.getElementById("txRate").value;
var regAmt = rate * regHrs;
var otAmt= rate * ( otHrs * 1.5 );
var total = otAmt + regAmt;
document.getElementById("txTotal").value = total;
__doPostBack(tb,e);
}

Since my calulateTotal function right now only has the one argument (tb) I assume then that I would have to change my codebehind page to look somthing like

txOTHrs.Attributes[

"onBlur"] ="return calculateTotal(event, this)";

instead of

txOTHrs.Attributes["onBlur"] ="return calculateTotal(this)";

Regarding the SqlDataSource. Does the argument DataSourceSelectArguments.Empty make the invocation of the Select method NOT go out to the database again ? If this is not the case and the database is hit again then I would want to get the data into a DataView at the time that I do my initial select on the database and then put that object into the session to make it persistant between page reloads ?

Again thank you very much for all the insights and the links to some really good data.

No comments:

Post a Comment