Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-05-13
7
Medium Priority
?
813 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
  • 3
  • 2
6 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

579 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