ASP.net C# javascript

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hi

In VBScript, I can use a array with dynamic indexing like the following
-------------------------
Dim SampleArray, DynamicStringIndex, DataValue
...
...
DynamicStringIndex = "hello"
DataValue = "contents"

SampleArray(DynamicStringIndex) = DataValue
...
...
SampleArray.Remove(DynamicStringIndex)

-------------------------

Can someone show me how to accomplish the same in C# javascript in an ASP.NET web page (.aspx)?

I would also like to how how can I loop through a binded Datagrid Datasource to perform some java scripts ?

Thanks
Peter
 
Hi

In VBScript, I can use a array with dynamic indexing like the following
-------------------------
Dim SampleArray, DynamicStringIndex, DataValue
..
..
DynamicStringIndex = "hello"
DataValue = "contents"

SampleArray(DynamicStringIndex) = DataValue
..
..
SampleArray.Remove(DynamicStringIndex)

-------------------------

Can someone show me how to accomplish the same in C# javascript in an ASP..NET web page (.aspx)?

I would also like to how how can I loop through a binded Datagrid Datasource to perform some java scripts ?

Thanks
Peter

http://msdn.microsoft.com/en-us/library/bb397721.aspx

or

http://ejohn.org/blog/javascript-array-remove/

Hope, this helps
 
Hi

In VBScript, I can use a array with dynamic indexing like the following
-------------------------
Dim SampleArray, DynamicStringIndex, DataValue
..
..
DynamicStringIndex = "hello"
DataValue = "contents"

SampleArray(DynamicStringIndex) = DataValue
..
..
SampleArray.Remove(DynamicStringIndex)

-------------------------

Can someone show me how to accomplish the same in C# javascript in an ASP..NET web page (.aspx)?

I would also like to how how can I loop through a binded Datagrid Datasource to perform some java scripts ?

Thanks
Peter

http://msdn.microsoft.com/en-us/library/bb397721.aspx

or

http://ejohn.org/blog/javascript-array-remove/

Hope, this helps
 
Hi Alexey

rThanks for your help but actually the array.remove is not my main point.
What I want is I would be able to refer to the array content by issuing

Response.Write ( SampleArray[DynamicStringIndex] ) or
Response.Write(SampleArray["hello"])

The output of the above should be "contents".

Regards,
Peter


Hi

In VBScript, I can use a array with dynamic indexing like the following
-------------------------
Dim SampleArray, DynamicStringIndex, DataValue
..
..
DynamicStringIndex = "hello"
DataValue = "contents"

SampleArray(DynamicStringIndex) = DataValue
..
..
SampleArray.Remove(DynamicStringIndex)

-------------------------

Can someone show me how to accomplish the same in C# javascript in an
ASP.NET web page (.aspx)?

I would also like to how how can I loop through a binded Datagrid
Datasource to perform some java scripts ?

Thanks
Peter

http://msdn.microsoft.com/en-us/library/bb397721.aspx

or

http://ejohn.org/blog/javascript-array-remove/

Hope, this helps
 
Hi Alexey

rThanks for your help but actually the array.remove is not my main point.
What I want is I would be able to refer to the array content by issuing

Response.Write ( SampleArray[DynamicStringIndex] )   or
Response.Write(SampleArray["hello"])

The output of the above should be "contents".

Actually, I don't get it at all. Where is array from? You mean it is
declared in the code behind (in c#) and you wanted to use it in the
javascript code?

Response.Write ( SampleArray[DynamicStringIndex] ) is absolutely
correct if it is on c#:

string[] SampleArray = new string[] {'contents'};
int DynamicStringIndex = 0;
Response.Write ( SampleArray[DynamicStringIndex] );
 
Hi Alexey

I want to be able to use any strings as the index of the array (e.g. "hello"
in my example) such that I can refer to its contents easily. The questions
is what data type should I declare SampleArray and what is the syntax of the
declaration inside an aspx page with C# javascript language.

Thanks
Peter



Hi Alexey

rThanks for your help but actually the array.remove is not my main point.
What I want is I would be able to refer to the array content by issuing

Response.Write ( SampleArray[DynamicStringIndex] ) or
Response.Write(SampleArray["hello"])

The output of the above should be "contents".

Actually, I don't get it at all. Where is array from? You mean it is
declared in the code behind (in c#) and you wanted to use it in the
javascript code?

Response.Write ( SampleArray[DynamicStringIndex] ) is absolutely
correct if it is on c#:

string[] SampleArray = new string[] {'contents'};
int DynamicStringIndex = 0;
Response.Write ( SampleArray[DynamicStringIndex] );
 
Hi Mark

Yes. But can I get some handy information from the web with what I needed
now first ? I understand that learning by reading a whole book is the best
way but I am currently running out of time.... Thanks for your help indeed
for referring me to this good book.

Regards,
Peter
 
Hi Mark

The reason I mention C# java scipt is because I use the code below on my aspx page. Sorry for the mis-leading..

<%@ Page Language="C#" %>
...
...
So, once again, are you trying to create a server-side array object in C# or are you trying to create a client-side array in
JavaScript? Both are possible, but they're not the same thing. I've already given you the C# code to create a server-side
Dictionary object (similar to an array, but much more powerful). If that's not what you want, then I can't help you any further
until you tell me what you do want...

I intend to create a server side C# I believe. In fact, I wanted to loop through a binded data set with and create array items and
store to a server side array (under certain conditions) and reuse the array items (or even delete some of arrange items) when
needed DURING the loop.

I hope I have made myself clear enough.

Regards,
Peter
 
Hi Mark

Thanks very much on clarifying lots about ASP.NET and I have really learned alot.

Referring to my questions earlier, I would like to loop through a dataset which were bind with below

String sqlcmd = "SELECT * from SampleTable'";

OleDbCommand myCommand = new OleDbCommand(sqlcmd, myConnection);
myConnection.Open();
ExampleDataGrid.DataSource = myCommand.ExecuteReader();
ExampleDataGrid.DataBind();
myConnection.Close();

Where "ExampleDataGrid" is the "id" from a DataGrid as mentioned earlier.

I would like to then loop through this kind of datasource and create a seperate array depending on the contents of the datagrid
field values. For the newly build table, I would also like to use the values in the datagrid as the "Index" of the array to easily
pick up it's array item contents. It would be grateful if you can show me how I can archeive this in my script after teh data
binding code above.

Thanks
Peter

Mark Rae said:
Peter,
The reason I mention C# java scipt is because I use the code below on my aspx page. Sorry for the mis-leading..
<%@ Page Language="C#" %>

You are intending to write in-line server-side code as opposed to using code-behind? I'd advise you not to do that.
One of the advantages of ASP.NET over ASP Classic is the ability to keep server-side code separate from the client-side markup -
others disagree, of course...

<script language="javascript">

Don't use that syntax - the language property of the script tag has been deprecated for nearly THIRTEEN years!

Use this instead:

I intend to create a server side C# I believe.

Once again, and I apologise for sounding pedantic here, but you cannot create a C#! C# is a programming language. You can't create
it - it already exists...

In fact, I wanted to loop through a bound data set with and create array items and store to a server side array (under certain
conditions) and reuse the array items (or even delete some of arrange items) when needed DURING the loop.

Again, the problem here is your lack of understanding of how ASP.NET works. Also, as an aside, if you are using ASP.NET 2005 or
later, don't use DataGrids - they are now deprecated in favour of GridViews, and won't (necessarily) be available in future
versions of ASP.NET...

When you fetch a DataSet from your database and bind it to a DataGrid, you do not need to create a separate server-side array -
you can work with the DataSet object directly.

DataSet MyDataSet = MyCreateDataSetMethod() // however you are creating the DataSet
DataTable MyDataTable = MyDataSet.Tables[0];
foreach (DataRow MyDataRow in MyDataTable.Rows) // loop through the DataSet
{
string MyValue1 = MyDataRow["MyColumn1"].ToString();
string MyValue2 = MyDataRow["MyColumn2"].ToString();
}

You can even add and/or delete rows before and/or after the DataSet is bound to the DataGrid:

DataRow MyDataRow = MyDataTable.NewRow();
MyDataRow["MyColumn1"] = "Value1";
MyDataRow["MyColumn2"] = "Value2";
MyDataTable.Rows.Add(MyDataRow);
MyGridView.DataSource = MyDataTable;
MyGridView.DataBind();
 
Mark Rae said:
What database are you using?
I am currently working on a mdb only but will be migrating the data to a sql database. I think I will stick with the mdb until
this has been settled
ExecuteReader() does not create a DataSet - it creates a DataReader. This is not the same as a DataSet - it is a live connection
to the database which can be used once only.
http://www.google.co.uk/search?hl=en&q=ADO.NET+DataReader+DataSet&btnG=Search&meta=

If you need a DataSet, then you'll need to create one like this:

DataSet myDataSet;
using (SqlConnection objSqlConnection = new SqlConnection(MyConnectionString))
{
objSqlConnection.Open();
using (SqlCommand objSqlCommand = new SqlCommand("SELECT * from SampleTable", objSqlConnection))
{
using (SqlDataAdapter objDA = new SqlDataAdapter(objSqlCommand))
{
using (DataSet objDataSet = new DataSet())
{
objDA.Fill(objDataSet);
objSqlConnection.Close();
myDataSet = objDataSet;
}
}
}
}
Thanks for the above example, I will give it a try and modify my codes.
I've already given you the code for that...
Yes.. I think so... I can give it a try
And I've already given you the code for that too. But you already have the DataSet - what is the purpose of this separate array?
What do you need it for? What do you intend to do with it...?
The array will be required as not every contents of the datasource will be copied to the array and some of the contents will be used
as the index of the array (e.g. ExampleArray[ExampleIndex], I am not sure about the syntax) where ExampleIndex may be something like
"apple" and ExampleArray["apple"] will be equaled to "red" (not sure about the syntax too) where both "apple" and "red" comes from
the dataset which are being looped. Entries in the arry may also be removed or replaced during the loop (i.e. where some
conditions are met during looping the dataset, I may want to remove the item ExampleArray["apple"] completely or replaced with
ExampleArray["apple"] = "green" for some reason)
Which newly-built table? I can't see anything in your code which builds a new table...
I should say "array" instead of "table". Sorry for the wrong wordings being used and thanks so much for your patients on giving me
a hand to this newbie.


Thanks
Peter
 
Sorry to bud-in,

Mark, can he use a datatable? He can traverse through each row of the
datatable and prepare the string or array that could be spit into a
javascript array?

Sorry I don't have the exact code but I was thinking along the following
lines....

for (int i = 0; i < datatable.rows.count;i++)
{
mystring += ',' + datatable[0];
}

then write the following:

Resposne.write "<script language='javascript'>";
Response.write " var myvar = '" + mystring + "'";
Response.write " var jsarray = myvar.split(',');
Response.write "</script>";


Peter said:
Mark Rae said:
What database are you using?
I am currently working on a mdb only but will be migrating the data to a sql database. I think I will stick with the mdb until
this has been settled
ExecuteReader() does not create a DataSet - it creates a DataReader. This is not the same as a DataSet - it is a live connection
to the database which can be used once only.
http://www.google.co.uk/search?hl=en&q=ADO.NET+DataReader+DataSet&btnG=Search&meta=

If you need a DataSet, then you'll need to create one like this:

DataSet myDataSet;
using (SqlConnection objSqlConnection = new SqlConnection(MyConnectionString))
{
objSqlConnection.Open();
using (SqlCommand objSqlCommand = new SqlCommand("SELECT * from SampleTable", objSqlConnection))
{
using (SqlDataAdapter objDA = new SqlDataAdapter(objSqlCommand))
{
using (DataSet objDataSet = new DataSet())
{
objDA.Fill(objDataSet);
objSqlConnection.Close();
myDataSet = objDataSet;
}
}
}
}
Thanks for the above example, I will give it a try and modify my codes.
I've already given you the code for that...
Yes.. I think so... I can give it a try
And I've already given you the code for that too. But you already have the DataSet - what is the purpose of this separate array?
What do you need it for? What do you intend to do with it...?
The array will be required as not every contents of the datasource will be copied to the array and some of the contents will be used
as the index of the array (e.g. ExampleArray[ExampleIndex], I am not sure about the syntax) where ExampleIndex may be something like
"apple" and ExampleArray["apple"] will be equaled to "red" (not sure about the syntax too) where both "apple" and "red" comes from
the dataset which are being looped. Entries in the arry may also be removed or replaced during the loop (i.e. where some
conditions are met during looping the dataset, I may want to remove the item ExampleArray["apple"] completely or replaced with
ExampleArray["apple"] = "green" for some reason)
Which newly-built table? I can't see anything in your code which builds a new table...
I should say "array" instead of "table". Sorry for the wrong wordings being used and thanks so much for your patients on giving me
a hand to this newbie.


Thanks
Peter
 
Hey Mark,

No, I just wanted to give Peter an idea...

He doesn't have to do Resposne.write, he can use

literal.text +=
or
label.text = the.whole.javascript.block



Mark Rae said:
Sorry to butt in,

Mark, can he use a datatable?

Please read my previous reply which contains:
DataTable MyDataTable = MyDataSet.Tables[0];

He can traverse through each row of the datatable and prepare the string
or array that could be spit into a javascript array?

And what use would a JavaScript array be to him? He (says he) needs this
array server-side...

Resposne.write "<script language='javascript'>";
Response.write " var myvar = '" + mystring + "'";
Response.write " var jsarray = myvar.split(',');
Response.write "</script>";

Two things:

1) The language property of the <script /> tag has been deprecated for
nearly thirteen years - use <script type="text/javascript"> instead, as I
mentioned in one of my earlier posts...

2) Response.Write should be avoided in ASP.NET. Unlike ASP Classic, ASP.NET
is not linear - it is object-oriented. When you use Response.Write you can
never guarantee 100% where the text will be written into the HTML stream.
For injecting JavaScript (which would not help the OP in this instance), the
ClientScript classes should be used instead...
 
Thanks guys for giving me a hand. Any suggestions on what kind of array
should I use and how would I declare and use it to fit my needs ?

Thanks
Peter
 
Hi Mark

I am sorry but did I miss it ? I couldn't find the code for the array with
"generic" indexing. Do you post lead me to the code again ? Thank you so
much again.

Regards,
Peter
 
Hi Mark

Sorry for the trouble but one last question, I think. Can you write me the
MyConnectionString syntax for accessing a local .mdb file ?

Thanks
Peter

Peter said:
Thank you so much Mark. I will give it a try.

Peter
 
I always have trouble with the connecting strings with various connection
methods. I have been trying to search through the web and cannnot locate
anything about using SqlConnection() to work with a .mdb file. Is this
possible ?

Thanks
Peter

Peter said:
Hi Mark

Sorry for the trouble but one last question, I think. Can you write me
the MyConnectionString syntax for accessing a local .mdb file ?

Thanks
Peter
 
Thanks guys.. I managed to get the Dictionary and OleDb thing working but Mark, any comments on the error below.

Peter

---

Compiler Error Message: CS0103: The name 'MyCreateDataSetMethod' does not exist in the current context

Source Error:



Line 194: logEntriesTable = logEntriesDataSet.Tables[0];
Line 195:
Line 196: DataSet MyDataSet = MyCreateDataSetMethod() ;
Line 197: DataTable MyDataTable= MyDataSet.Tables[0] ;
Line 198: DataRow MyDataRow = MyDataTable.NewRow();


Source File: s:\WebRoot\Logging\Report.aspx Line: 196
 
Back
Top