格式异常和下拉值 [英] Format Exception and Drop down value

查看:61
本文介绍了格式异常和下拉值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢其工作.
但在这里我解释了我想要的.
我有1个下拉列表产品.
1个文本框标价.
1个搜索按钮.
如果我不给出任何产品或价格,它将显示所有产品.
然后在按钮点击事件中调用搜索方法.这里是我的示例代码

Thanks its work.
But here i explain what i want.
i have 1 dropdownlist product.
1 text box take price.
1 search button.
if i don''t give any product or price it will display all product.
and i call my search method in button click event.here my sample code

int productid = int.Parse(DropDownListProduct.SelectedValue.ToString());
        
            int price;
            if (int.TryParse(txtPrice.Text, out price))
            {
                price = Convert.ToInt32(txtPrice.Text);
            }
            else
            {
                price =null or ""; give me ERROR
            }


ctlDropDown.Items.InsertAt(0,new ListItem("SELECT PRODUCT",null);
如果我什么都没选择,请给我错误.然后将其放在databind()表单加载事件下


ctlDropDown.Items.InsertAt(0, new ListItem("SELECT PRODUCT", null);
Give me Error if i don''t select anything.and i put it under databind() form load event

推荐答案



int.TryParse不会引发异常.它返回一个布尔值,其中包含是否成功解析了该值.它还将int作为输出参数,因此成功后您仍然可以访问int值.您可以通过以下方式使用它:

Hi,

int.TryParse wont throw an exception. It returns a boolean containing if the value was successfully parsed. It also takes an int as an output parameter so you still have access to the int value when successful. You can use it in the following way:

int price = 0;
if(int.TryParse(txtPrice.Text, out price)) {
  // Do code here
}



在此示例中,它不会引发异常,仅在输入值有效时才执行相关代码.

至于没有选择列表中的第一个产品.如果没有看到您的代码,就很难给出建议.但是我最好的猜测是您需要添加一个占位符.使列表中的第一项-选择-".然后在您的代码中检查该值,如果已选择则忽略它.



In this example it doesn''t throw an exception and only executes the relevant code when the input value is valid.

As for not selecting the first product in the list. Without seeing your code it''s difficult to give advice. But my best guess would be that you need to add a place holder. Make the first item in the list "--Select--". Then in your code check for this value and ignore it if it''s selected.


这篇关于格式异常和下拉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆