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

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)
{  ]
quest_capitalAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ViceroyFizzlebottomConnect With a Mentor Commented:
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
 
ViceroyFizzlebottomCommented:
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
 
ViceroyFizzlebottomCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Obadiah ChristopherCommented:
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
 
quest_capitalAuthor Commented:
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
 
quest_capitalAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.