Solved

onclick how to run my own method in c sharp

Posted on 2006-06-29
21
311 Views
Last Modified: 2008-01-09
Hello this code runs a javascrpt with name saveSelection.
I want it to run a c# method. How to do that?

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox Velg = (CheckBox)e.Row.FindControl("Velg");
            Velg.Attributes.Add("onclick", "saveSelection(this,'" + e.Row.Cells[0].Text + "');");
        }
    }
0
Comment
Question by:m-jansen
  • 14
  • 7
21 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17011541
Have you heard anything about AJAX (AjaxPro)?

Bob
0
 

Author Comment

by:m-jansen
ID: 17011568
Very little. Think I've heard the name mentioned some place.
0
 

Author Comment

by:m-jansen
ID: 17011592
Why do you ask?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 17011753
AjaxPro is a library that makes it possible to call server-side methods from client-side Javascript.  We use it here to get a simplify generating DataSet objects, since C#/VB.NET is so much easier.

Bob
0
 

Author Comment

by:m-jansen
ID: 17011829
ohh ok. So I must use the evening to learn Ajax... ok.ok :)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17011858
What is saveSelection doing?

Bob
0
 

Author Comment

by:m-jansen
ID: 17011896
right now it's doing nothing more that to view some info from a row in gridview. I wanted to use a c# method instead becuase it's easier to find what I need there.

   function saveSelection(obj, idxId)
    {        
        alert("obj: "+obj + " idexId: " + idxId);
    }
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17011970
What method do you need to call?  One possible method is to call the method, get the result, place the result into a hidden <input> element and then extract the result from the Javascript side.

Bob
0
 

Author Comment

by:m-jansen
ID: 17011980
But if it can be done by useing javascript method i would be glad too.... or i'll start to learn ajax
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17012002
So, which C# method are you looking to call?

Bob
0
 

Author Comment

by:m-jansen
ID: 17012049
going to call all kinds of methods I think.

convert this:
Velg.Attributes.Add("onclick", "saveSelection(this,'" + e.Row.Cells[0].Text + "');");

to work with c# methods

like this
Velg.Attributes.Add("onclick", "mycsharpmethod(e)");

but I get error: e is undefined
0
 

Author Comment

by:m-jansen
ID: 17012077
I must make GridViewRowEventArgs e protected and available for both my asp script and class file, but that's not a good solution I guess.
0
 

Author Comment

by:m-jansen
ID: 17012123
I mean like this....
Velg.Attributes.Add("onclick", "mycsharpmethod('" + e + "');");
0
 

Author Comment

by:m-jansen
ID: 17012172
or maybe this like this....
Velg.Attributes.Add("onclick", "'" + list.Add(21) + "'");
0
 

Author Comment

by:m-jansen
ID: 17012289
I've installed AjaxPro.2.dll in project now. Feel free to tell me how do this with ajax.
0
 

Author Comment

by:m-jansen
ID: 17012448
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox Velg = (CheckBox)e.Row.FindControl("Velg");
            Velg.Attributes.Add("onclick", ""+ list.Add(32) +"");
        }
    }

This compiles and run without exeptions, but it does not add 32 to my array list. Why not?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 17012966
1) Add a reference to AjaxPro.dll to the project

2) Add using AjaxPro;

3) Add the [AjaxPro.AjaxMethod()] attribute to the method that you need to call

4) Register the class that you need to access:

     Utility.RegisterTypeForAjax(typeof(DataAccessLayer));

5) Here is an example in a Javascript function to call the method:

   DataAccessLayer.ViewResults(QLNAME, QLFIELDS, QLFILTER, QLSORT, mdbExportResultCallback);

6) Here are a few lines from the call back function:

   function mdbExportResultCallback(res)
   {
       writeLine("Received data from the server, importing into Access database.");
            
      if (res.value.Tables[0].Columns[0].Name == "Error")
      {
          alert(res.value.Tables[0].Rows[0][res.value.Tables[0].Columns[0].Name]);
          return;
      }

7) You can see from the Javascript code, that you have the same properties as a DataSet/DataTable (it's like magic!!!).

Bob
0
 

Author Comment

by:m-jansen
ID: 17018068
well, thanks. That really learned me alot. But My question is still unsolved.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17030472
Tell me, my friend, where you are still having a problem, and how I can help.

Bob
0
 

Author Comment

by:m-jansen
ID: 17030536
To be honest I installed Atlas instead. That was better to use with my VS2005. But my problem is now solved. I didn’t need to use javascript directly. I don’t remember right now what that solved it, but Atlas did it a lot easier for me to use Ajax. So since you directed me that way I think your answer posted 06/29/2006 09:00PM BST can be considered as accepted.
0
 

Author Comment

by:m-jansen
ID: 17030556
Thanks for all help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question