Onclick multiple function paremeters asp:button

Last post 09-27-2009 1:39 PM by nijhawan.saurabh. 2 replies.

Sort Posts:

  • Angry [:@] Onclick multiple function paremeters asp:button

    09-27-2009, 12:50 AM
    • Member
      point Member
    • wiseguy12851
    • Member since 09-27-2009, 4:28 AM
    • Posts 1

    There doesn't seem to be full documentation on an asp.net tutorial as each tutorial just covers part or several parts such as w3 schools web site not covering vb .net or C# and most of ASP .NET but not all giving poorly written tutorials on ASP. Net

    I'm trying to convert a website from PHP to asp .net with all of it's perks. the website is written in very complex PHP code taking full grasp of every PHP feature. Asp .Net offer compatibility which is a major problem in my PHP website as well as flexibility such as master pages that I would have to simulate in PHP.

    I can't get past the first bit of code and I've been looking everywhere at least 10 times a day for the past 14 day's. In my PHP code I have a simple function among hundreds that connects to a database given three paremeters (The table name, the search term, the column to return) and returns a single cell out of a table. I would like this to be avaliable as an ASP button with an onclick event.

    I'm a master at PHP but hardly know the basic syntax of ASP much less anything related to VB Script or C#. All documents point to something called argument name and value but I never could get that to work right and it was only for 1 paremeter, I don't have to have a button but I need some way to simulate such a simple command in ASP .Net VB or C#

  • Re: Onclick multiple function paremeters asp:button

    09-27-2009, 1:23 PM
    • Contributor
      7,249 point Contributor
    • whighfield
    • Member since 01-02-2006, 10:37 PM
    • Winterpeg, Manitoba
    • Posts 1,205

    Not really sure what you are trying to do but each button can have it's own individual event click or can share an event like so

    ASP.Net page code

    <div>
        <asp:Button ID="btnButtonIndividualEvent" runat="server" onclick="btnButtonIndividualEvent_Click" /><br />
        <asp:Button ID="btnButtonSharedEvent0" runat="server" onclick="btnButtonSharedEvent_Click" CommandArgument="0" /><br />
        <asp:Button ID="btnButtonSharedEvent1" runat="server" onclick="btnButtonSharedEvent_Click" CommandArgument="1" /><br />
        <asp:Button ID="btnButtonSharedEvent2" runat="server" onclick="btnButtonSharedEvent_Click" CommandArgument="2" /><br />
    </div>

    Code behind

    public partial class ButtonSample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                // code here to init data and such
            }
        }
    
        /// <summary>
        /// Individual button event
        /// </summary>
        /// <param name="sender">The button as an object</param>
        /// <param name="e">Button arguments</param>
        protected void btnButtonIndividualEvent_Click(object sender, EventArgs e)
        {
            // get a ref to the button
            Button btn = (Button)sender;
    
            // are these dynamic or specific to each button?
            string tableName = "TABLE_NAME";
            string search = "SEARCH_FILTER";
            string column = "COLUMN_LIST";
    
            // our data call
            DataCall(tableName, search, column);
        }
    
        /// <summary>
        /// Shared button event
        /// </summary>
        /// <param name="sender">The button as an object</param>
        /// <param name="e">Button arguments</param>
        protected void btnButtonSharedEvent_Click(object sender, EventArgs e)
        {
            // get a ref to the button
            Button btn = (Button)sender;
    
            string tableName = "TABLE_NAME";
            string search = "SEARCH_FILTER";
            string column = "COLUMN_LIST";
    
            switch (btn.CommandArgument.ToLower())
            {
                case "0":
                    tableName = "TABLE_NAME0";
                    search = "SEARCH_FILTER0";
                    column = "COLUMN_LIST0";
                    break;
                case "1":
                    tableName = "TABLE_NAME1";
                    search = "SEARCH_FILTER1";
                    column = "COLUMN_LIST1";
                    break;
                case "2":
                    tableName = "TABLE_NAME2";
                    search = "SEARCH_FILTER2";
                    column = "COLUMN_LIST2";
                    break;
                default:
                    break;
            }
    
            // our data call
            DataCall(tableName, search, column);
        }
    
        /// <summary>
        /// What are we doing here? and do we return anything?
        /// </summary>
        /// <param name="tableName">The table name</param>
        /// <param name="search">The search filter</param>
        /// <param name="column">columns to return?</param>
        protected void DataCall(string tableName, string search, string column)
        {
            // do code here
        }
    }

    Are you looking for something like this?

    Please mark the most helpful reply/replies as "Answer".

    - William
    http://thefrozencoder.ca
  • Re: Onclick multiple function paremeters asp:button

    09-27-2009, 1:39 PM

    Hi,

    Do you want to have a single function where you could send three parameters and get data from the database.

    Well in that case just add a class to your asp.net project, write a function with three arguments(just pass the name of the database table,search term, and column to return).

    Now on any buttons click you can call this function by either making an onject of this class and calling the function or, having defined the function as static so that you dont have to create an object of the class to call the function.

    For example here's a class named clscommon, it has all the functions related to database querying,whenever you like to do something related to databases, just create an object of this class and call the respective function.


     public class clsCommon 
        {
            public List<clsComboPrp> GetNameList(String table)
            {
                List<clsComboPrp> temp = new List<clsComboPrp>();
                String myselectquery = "select * from " + table;
                MySqlCommand mycommand = new MySqlCommand(myselectquery, con);
                mycommand.CommandType = CommandType.Text;
                if (con.State == ConnectionState.Open)
                    con.Close();
                con.Open();
                MySqlDataReader dr = mycommand.ExecuteReader();
                clsComboPrp obj = null;
                if (dr.HasRows)
                    while (dr.Read())
                    {
                        obj = new clsComboPrp();
                        obj.Id = dr[0].ToString();
                        obj.Name = dr[1].ToString();
                        temp.Add(obj);
                    }
                dr.Close();
                dr.Dispose();
                mycommand.Dispose();
                con.Close();
                return temp;
            }
            public String GetName(String table, String Id)
            {
                String name = "";
                String query = "select `Name` from `" + table + "` where `ID`=" + Id;
                MySqlCommand cmd = new MySqlCommand(query, con);
                if (con.State == ConnectionState.Open)
                    con.Close();
                con.Open();
                MySqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    name = dr[0].ToString();
                }
                cmd.Dispose();
                con.Close();
                return name;
            }


    Saurabh Nijhawan(B.Tech. CSE,GGSIPU,New Delhi)
    Application Architect, Eminent Solutions, New Delhi.
    Freelancer | Teacher
    Remember to click "Mark as Answer" on the post, if it helped you.
    ASP.NET Weblog
    http://www.saurabhnijhawan.com
    Learning Made Easy


Page 1 of 1 (3 items)