[英]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. 我的
RadComboBox
应该下拉并显示供我选择的选项。 the options are name but the selected values are numbers corresponding to the ID of the center. 选项是名称,但所选值是与中心ID对应的数字。
<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. 基本上当用户从
RadComboBox1
选择一个值时,我想将该值存储在一个int
变量中。
Hope that DataValueField
is assigned while binding the grid, and the Value is convertible to an integer. 希望在绑定网格时分配
DataValueField
,并且Value可以转换为整数。 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. 然后我想为此转换建议
int.TryParse
,以便您使用其返回值来检查转换是否成功。 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? 是否有任何特殊原因需要在
NeedDataSource
事件中对RadComboBox
控件进行数据绑定? This will not guarantee that your RadComboBox
control will be loaded correctly due to the life cycle of your page/control. 由于页面/控件的生命周期,这不能保证您的
RadComboBox
控件将被正确加载。
What you possibly can do is to add your data-binding code to Page_Load
event, something like below. 您可以做的是将您的数据绑定代码添加到
Page_Load
事件,如下所示。
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.
另外,对于将字符串转换为整数,请参考un-lucky的答案,这是建议的方法。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.