This is a migrated thread and some comments may be shown as answers.

Use ComboBox & TextBox Values as Parameters on Button_Click

2 Answers 613 Views
Button
This is a migrated thread and some comments may be shown as answers.
Brett
Top achievements
Rank 1
Brett asked on 09 Apr 2014, 05:34 AM
Hello,

On my page I have a ComboBox where the user can choose a name and I also have a TextBox where the user can enter one or more values. I am trying to figure out how to take the value from the ComboBox (w/ @Name parameter) and the value(s) from the TextBox (w/ @ProductNum parameter(s))
and  use those as Parameters  together (@Name, @ProductNum) in a Procedure that I have created when I click the Generate Template button.

I am stuck and I am not sure what my next step should be. Any help would be appreciated.

aspx
<%@ Page Title="Product Template" Language="C#" MasterPageFile="~/MasterPages/M.Master" AutoEventWireup="true" CodeBehind="ProductTemplate.aspx.cs" Inherits="ProductTemplate" Theme="Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server" ViewStateMode="Inherit">
 
<style>
.addButton
{
    float: right;
    display: block;
}
 
</style>
 
 
    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Height="300px" Width="450px">
  
     <table>
        <tr>
            <td>
                <asp:Label ID="NameLbl" runat="server" Text="Name: " Width="120px" ></asp:Label>
            </td>
            <td>
                <telerik:RadComboBox ID="NameRCB" runat="server" AutoPostBack="True" DataSourceID="dllDataSource"
                    DataTextField="Customer_Name" DataValueField="Customer_Name" Width="200px"
                    AppendDataBoundItems="true">
                    <Items>
                        <telerik:RadComboBoxItem Text="- Name -" Value="0" Selected="true"></telerik:RadComboBoxItem>
                    </Items>
                </telerik:RadComboBox>
            </td>
            <td></td>
            <td>
                <asp:Label ID="ProductLbl" runat="server" Text="Product: "></asp:Label>
            </td>
            <td>
                <telerik:RadTextBox ID="ProductTB" runat="server">
                </telerik:RadTextBox>
            </td>
            <td></td>
            <td>
                <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" Display="None"
                    ControlToValidate="ProductTB" ErrorMessage="Enter a Product Number"></asp:RequiredFieldValidator>
                <telerik:RadButton ID="RbSelect" runat="server" Text=" Select "
                    onclick="RbSelect_Click" />
            </td>
        </tr>
    </table><br />
 
 
    <!-- list -->
 
        <telerik:RadListBox ID="RadListBox1" runat="server" Height="250px">
        </telerik:RadListBox>
 
        <telerik:RadTextBox ID="RadTextBox1" runat="server" TextMode="MultiLine" Height="250px" Width="200px" >
        </telerik:RadTextBox>
    </telerik:RadAjaxPanel>
 
 
    <br />
 
    <telerik:RadButton ID="RbGenerate" runat="server" Text="Generate Template" style="margin-left: 150px" onclick="RbGenerate_Click">
    </telerik:RadButton>
     
    <br />
    <br />
 
    <asp:ValidationSummary runat="server" ShowMessageBox="false" ID="ValidationSummary1">
    </asp:ValidationSummary>
 
    <!-- datasource for RadComboBox -->
    <asp:SqlDataSource ID="dllDataSource" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="dbo.DropDownPROC" SelectCommandType="StoredProcedure" >
    </asp:SqlDataSource>
    <!-- datasource for grid -->
 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" EnableAJAX="true" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="NameRCB">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="OrdersGrid" LoadingPanelID="RadAjaxLoadingPanel1">
                    </telerik:AjaxUpdatedControl>
                    <telerik:AjaxUpdatedControl ControlID="NameRCB"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
    </telerik:RadAjaxLoadingPanel>
 
</asp:Content>


.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data.SqlClient;
using System.Data;
 
namespace EE.Intranet.Purchasing
{
    public partial class ProductTemplate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void RbSelect_Click(object sender, EventArgs e)
        {
             
            RadTextBox1.Text = ProductTB.Text;
            RadListBox1.Items.Add(new RadListBoxItem(HttpUtility.HtmlEncode(ProductTB.Text.Trim())));
            ProductTB.Text = string.Empty;
        }
 
        protected void RbGenerate_Click(object sender, EventArgs e)
        {
 
            /*using (SqlConnection conn = new SqlConnection("connection1"))
            {
                conn.Open();
 
                SqlCommand comm = new SqlCommand("dbo.sp_Proccccc", conn);
                comm.CommandType = CommandType.StoredProcedure;
                comm.Parameters.Add(new SqlParameter("@Name", Name));
                comm.Parameters.Add(new SqlParameter("@ProductNum", ProductNumber));
                comm.ExecuteNonQuery();
 
                conn.Close();
            }*/
        }
    }
}



I have also attached a pic of what  my current page looks like.

Thank you,

Brett

2 Answers, 1 is accepted

Sort by
0
Brett
Top achievements
Rank 1
answered on 09 Apr 2014, 10:27 PM
Edit:

-How can I grab the value that is currently selected in the ComboBox and use that as a parameter,something like,
comm.Parameters.Add(new SqlParameter("@Name", NameRCB.SelectedIndex? ));
Same thing with grabbing the value from either a RadListBox or RadTextBox ,
comm.Parameters.Add(new SqlParameter("@ProductNum", RadListBox1.SelectedIndex? ));
I would like the ability to copy-paste multiple values into one of these tools and run my procedure for each value in it.
*I will remove either the RadListBox or RadTextBox once I find out which one is the better option for meeting my needs.


public partial class ProductTemplate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void RbSelect_Click(object sender, EventArgs e)
    {
        RadTextBox1.Text = RadTextBox1.Text + "\n" + ProductTB.Text;
        RadListBox1.Items.Add(new RadListBoxItem(ProductTB.Text.Trim()));
        //RadListBox1.Items.Add(new RadListBoxItem(HttpUtility.HtmlEncode(ProductTB.Text.Trim())));
        ProductTB.Text = string.Empty;
    }
 
    protected void RbGenerate_Click(object sender, EventArgs e)
    {
 
        using (SqlConnection conn = new SqlConnection("connection1"))
        {
            conn.Open();
 
            SqlCommand comm = new SqlCommand("intranet.dbo.sp_Proccccc", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.Add(new SqlParameter("@Name", NameRCB.SelectedIndex? ));
            comm.Parameters.Add(new SqlParameter("@ProductNum", RadListBox1.SelectedIndex? ));
            comm.ExecuteNonQuery();
 
            conn.Close();
        }
    }
}
0
Danail Vasilev
Telerik team
answered on 11 Apr 2014, 11:24 AM
Hello Brett,

You can use the SelectedItem.Text and SelectedValue properties in order to obtain the text and the value of the selected item from the combobox and the list box. For example:

C#:
protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = string.Format("Selected Item Text: {0}, Selected Item Value: {1}", RadComboBox1.SelectedItem.Text, RadComboBox1.SelectedValue);
    Label2.Text = string.Format("Selected Item Text: {0}, Selected Item Value: {1}", RadListBox1.SelectedItem.Text, RadListBox1.SelectedValue);
}

ASPX:
<asp:Label ID="Label1" Text="" runat="server" />
<br />
<telerik:RadComboBox ID="RadComboBox1" runat="server">
    <Items>
        <telerik:RadComboBoxItem Text="text1" Value="1" Selected="true" />
        <telerik:RadComboBoxItem Text="text2" Value="2" />
    </Items>
</telerik:RadComboBox>
<br />
 
<asp:Label ID="Label2" Text="" runat="server" />
<br />
<telerik:RadListBox ID="RadListBox1" runat="server">
    <Items>
        <telerik:RadListBoxItem Text="text11" Value="11" />
        <telerik:RadListBoxItem Text="text22" Value="22" Selected="true" />
    </Items>
</telerik:RadListBox>

Once you obtain these values you can use them inside your SQL query.

Regards,
Danail Vasilev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Button
Asked by
Brett
Top achievements
Rank 1
Answers by
Brett
Top achievements
Rank 1
Danail Vasilev
Telerik team
Share this question
or