简体   繁体   中英

how can I store the RadComboBox selected value in C# code?

My RadComboBox is supposed to drop down and display options for me to select. the options are name but the selected values are numbers corresponding to the ID of the center.

 <telerik:RadComboBox DataValueField="OPERATION_CENTER_ID" RenderingMode="Full" EmptyMessage="Select Operation Center" DataTextField="OPERATION_CENTER_NAME" ID="RadComboBox1" runat="server"></telerik:RadComboBox> 

  protected void rdOrders_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e) { //Get OPERATION_CENTER_ID in f1 from f2 query (it is an int) string f2 = "select * from [dbo].[GetOperationCentersInfo]('F')"; SQLHelper a = new SQLHelper(SQLHelper.ConnectionStrings.EmallShippingConnectionString); int ID = RadComboBox1.SelectedValue; { string f1 = "select * from [dbo].[GetReadyDeliveryOrderItems](" + ID + ")"; DataTable DataTable1 = a.getQueryResult(f1); //Orders Query by OPERATION_CENTER_ID DataTable DataTable2 = a.getQueryResult(f2); //Operation centers Query DataTable testDataTable = new DataTable(); rdOrders.DataSource = DataTable1; RadComboBox1.DataSource = DataTable2; RadComboBox1.DataBind(); } 

Basically when the user selects a value from RadComboBox1 , I want to store that value in an int variable.

Hope that DataValueField is assigned while binding the grid, and the Value is convertible to an integer. Then I would like to suggest int.TryParse for this conversion, So that you use its return value to check whether the conversion is successful or not. See the code below:

int ID;
if(Int32.TryParse(RadComboBox1.SelectedValue,out ID))
{
  // You can proceed with ID 
}
else
{
  // Conversion failed
}

Is there any particular reason that you have to data-bind your RadComboBox control in a NeedDataSource event? This will not guarantee that your RadComboBox control will be loaded correctly due to the life cycle of your page/control.

What you possibly can do is to add your data-binding code to Page_Load event, something like below.

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      BindRadComboBox();
   }
}

private void BindRadComboBox()
{
    string f2 = "select * from [dbo].[GetOperationCentersInfo]('F')";

     SQLHelper a = new SQLHelper(SQLHelper.ConnectionStrings.EmallShippingConnectionString);

       int ID = RadComboBox1.SelectedValue; {
       string f1 = "select * from [dbo].[GetReadyDeliveryOrderItems](" + ID + ")";
       DataTable DataTable1 = a.getQueryResult(f1); //Orders Query by OPERATION_CENTER_ID
       DataTable DataTable2 = a.getQueryResult(f2); //Operation centers Query
       DataTable testDataTable = new DataTable();
       rdOrders.DataSource = DataTable1;
       RadComboBox1.DataSource = DataTable2;
       RadComboBox1.DataBind();
}

You will need to double check the code that fetches the data though. Also for converting string to integer, please refer to un-lucky's answer, which is recommended way of doing it.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM