Solved

C# How to get the submit value from a button.

Posted on 2009-05-13
7
799 Views
Last Modified: 2012-05-06
C# How to get the submit value from a button.
I want to have my code do something if the submit button is lets say true
Ex. something like below
if (submit.value == true)
{  ]
0
Comment
Question by:quest_capital
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 6

Expert Comment

by:ViceroyFizzlebottom
ID: 24375401
Well, a button generally just fires an event handler to run code. The only values you could conceivably get from a button would be the Text property or other properties associated with the button.

There's no "Value" property per se. What exactly are you trying to accomplish?
0
 
LVL 6

Expert Comment

by:ViceroyFizzlebottom
ID: 24375432
Here is a list of all the available properties exposed by the Button class:
http://msdn.microsoft.com/en-us/library/system.windows.controls.button_members(VS.85).aspx
0
 
LVL 20

Expert Comment

by:informaniac
ID: 24375564
As ViceroyFizzle suggests....

.Value property is for a <input type="submit" which is a html control.

If u r placing an <asp:button

then u'll get if(bntId.Text=="true")
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:quest_capital
ID: 24376150
The code below has two buttons one asp:Button (Save) and one html "Submit" button.
The submit button creates a gridview from a sql query.
The asp:Button "Save" button saves the sql query.
However the problem is when I click the "save" button it also runs the query as thou I clicked the "Submit" button.
So I want to some how make the code unserstand that I clicked the "Save" button.
I know this code could have been written better from the start but I am new to C# and I am piecing code together.
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;
using System.Data.SqlClient;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using core;
 
public partial class mydesktop_query : System.Web.UI.Page
{
    private void loaddata()
    {
        string selected = dd_query.SelectedValue;
 
        // Query Dropdow
        string Access_sql = "select id, q_name from query";
        OleDbConnection AccessCnn = new OleDbConnection(dal.AccessCnn);
        AccessCnn.Open();
        OleDbDataAdapter da1 = new OleDbDataAdapter(Access_sql, AccessCnn);
        DataSet ds1 = new DataSet();
        da1.Fill(ds1, "Table");
        dd_query.DataSource = ds1;
        dd_query.DataMember = ds1.Tables[0].TableName;
        dd_query.DataTextField = "q_name";
        dd_query.DataValueField = "id";
        dd_query.DataBind();
        dd_query.Items.Insert(0, new ListItem("Select Query to Load", ""));
 
        AccessCnn.Close();
        AccessCnn.Dispose();
 
        //dd_query.Text = selected;
    }
 
 
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = MasterPage.userinfo.appname.ToString() + " (Query Creator 1.0)";
 
        ResultGrid.Visible = false;
        ResultLbl.Text = "";
 
        String cnnStr = dal.DevCnn;
        SqlConnection SqlProdCnn;
        DataSet ds = null;
 
        // Status dropdown
        string Selected = dd_status.SelectedValue;
 
        ArrayList list = new ArrayList();
        list.Add("Dev");
        list.Add("Val");
        list.Add("Prod");
 
        dd_status.DataSource = list;
        dd_status.DataBind();
 
        //string s = Save.Text;
 
        if (!IsPostBack)
        {
            loaddata();
        }
 
 
        if (this.IsPostBack)
        {
            dd_status.Text = Selected;
            string sql = SqlCtrl.Text.Trim();
 
            if (Selected == "Dev")
            {
                cnnStr = dal.DevCnn;
            }
            if (Selected == "Val")
            {
               cnnStr = dal.ValCnn;
            }
            if (Selected == "Prod")
            {
                cnnStr = dal.ProdCnn;
            }
 
            string dd_query_Selected = dd_query.SelectedValue;
            if (dd_query_Selected.Length == 0)
            {
                SqlProdCnn = new SqlConnection(cnnStr);
                try
                {
                    System.Security.Principal.WindowsImpersonationContext impersonationContext;
                    impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
 
                    SqlProdCnn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(sql, SqlProdCnn);
                    ds = new DataSet();
                    da.Fill(ds, "Table");
 
 
                    if (sql.Length >= 6)
                    {
                        ResultGrid.DataSource = ds.Tables[0];
                        ResultGrid.DataBind();
                        ResultGrid.Visible = true;
                        ResultLbl.Text = ResultGrid.Rows.Count.ToString() + " rows selected";
                    }
                    else
                    {
                        //na
                    }
                    impersonationContext.Undo();
                }
 
                catch (System.Exception ex)
                {
                    SqlProdCnn.Close();
                    SqlProdCnn.Dispose();
                    ResultLbl.Text = ex.Message;
                }
                finally
                {
                    SqlProdCnn.Close();
                    SqlProdCnn.Dispose();
                }
            }
            else
            {
 
                // Loads sql box
                string Access_sql2 = "select id, q_name, query from [query] where [id] = " + dd_query_Selected;
                OleDbConnection AccessCnn2 = new OleDbConnection(dal.AccessCnn);
                AccessCnn2.Open();
                OleDbDataAdapter da2 = new OleDbDataAdapter(Access_sql2, AccessCnn2);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "Table");
                DataTable dt2 = ds2.Tables[0];
 
                string sqlquery = dt2.Rows[0]["query"].ToString();
                SqlCtrl.Text = sqlquery;
 
                AccessCnn2.Close();
                AccessCnn2.Dispose();
 
            }
 
            loaddata();
        }
    }
 
    protected void Save_Click(object sender, EventArgs e)
    {
        string query;
        query = SqlCtrl.Text;
 
        string[] separator = new string[] { "/*", "*/" };
        string[] textlist = query.Split(separator, StringSplitOptions.None);
 
        ArrayList strsList = new ArrayList();
        foreach (string s in textlist)
        strsList.Add(s);
 
        int count = strsList.Count - 1;
 
        if (count == 2)
        {
            string sql_title;
            string sql_query;
 
            sql_title = strsList[1].ToString().Trim();
            sql_query = strsList[2].ToString().Trim();
 
 
            string Access_sql2 = "select id, q_name, query from [query] where [q_name] = '" + sql_title + "'";
            OleDbConnection AccessCnn2 = new OleDbConnection(dal.AccessCnn);
            AccessCnn2.Open();
            OleDbDataAdapter da2 = new OleDbDataAdapter(Access_sql2, AccessCnn2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2, "Table");
 
            int rwcount = ds2.Tables[0].Rows.Count;
 
            //DataTable dt2 = ds2.Tables[0];
 
            if (rwcount == 0)
            {
                // Insert
                BAL_Core cor1 = new BAL_Core();
                cor1.userid = dal.win_uid;
                cor1.q_name = sql_title;
                cor1.query = query;
                cor1.Insert_Query();
            }
            else 
            {
                //Save.OnClientClick.
                //Update
                BAL_Core cor2 = new BAL_Core();
                cor2.q_name = sql_title;
                cor2.query = query;
                cor2.Update_Query();
            }
 
            AccessCnn2.Close();
            AccessCnn2.Dispose();
        }
        loaddata();
    }
}

Open in new window

0
 

Author Comment

by:quest_capital
ID: 24376194
here is the gui page
    <div style="text-align:center;">
    <asp:TextBox id="SqlCtrl" runat="server" Width="1100px" Height="140px" TextMode="MultiLine" Wrap="False"></asp:TextBox>
        <div style="text-align:left; width:1100px; line-height:30px">
        <table border=0 width="100%" CELLPADDING=0 CELLSPACING=0 BORDERCOLOR=white>
        <tr>
        <td>
        <asp:DropDownList ID="dd_status" runat="server"></asp:DropDownList>
        <INPUT TYPE="submit" VALUE="Execute"> 
        <asp:Button ID="Save" runat="server" Text="Save" onclick="Save_Click" OnClientClick="return confirm('Are you sure you want to save this record?');" />
        <asp:Label id="ResultLbl" runat="server"></asp:Label>
        </td>
        <td align=right>
        <asp:DropDownList ID="dd_query" runat="server" AutoPostBack="True"></asp:DropDownList>
        </td>
        </tr>
        </table>
        </div>
    </div>
    <br />
    <div style=width:1200;height:300;text-align:center;> 
    <asp:GridView ID="ResultGrid" runat="server">
    </asp:GridView>
    </div>

Open in new window

0
 
LVL 6

Accepted Solution

by:
ViceroyFizzlebottom earned 500 total points
ID: 24376704
From what I can see, regardless of which button you press, a PostBack will be submitted to the server. However, the loaddata() method is getting called whether it was a PostBack or not.

In any event, the easiest option would be to check which control actually initiated the PostBack. The snippet below should allow you to get the control the user selected and then you can adjust your logic accordingly.
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
   // Do stuff depending on which button was pressed
}

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

635 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