Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-13
7
Medium Priority
?
809 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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Integration Management Part 2
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

963 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