输入字符串的格式不正确

时间:2012-04-12 11:54:32

标签: asp.net .net vb.net visual-studio asp.net-controls

下午全部,

我有一个下拉列表,从我的SQL数据库中提取数据,并为用户提供两个选项(每周/每月)。数据库具有每个这样的ID。 Weekly设置为1,Monthly设置为2.此下拉列表链接到gridview,gridview根据所选项目提取/显示数据。所有这一切都非常好。

我遇到的这个问题是我想在我的Page_ load事件中添加一些代码来填充包含所选项目的文本框。当用户访问该页面时,我还想将下拉列表设置为默认为每周。我认为以下两位代码可以工作,但我得到了消息'输入字符串格式不正确'。

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'This works fine
    lblTodaysDate.Text = GetDate()


    'I thought i could complete an If Statement to get the text box to work.
    If DropDownList1.SelectedValue = 1 Then
        txtMeeting.Text = "SMC Weekly Meeting"
    Else
        txtMeeting.Text = "SMC Monthly Meeting"
    End If


End Sub

我是.net的新手但已经读过我可能需要将我的int转换成字符串吗?

提前获得的任何帮助都会受到很多关注。

此致 贝蒂。

3 个答案:

答案 0 :(得分:2)

只需尝试将所需的值括在引号中,如下所示:

If DropDownList1.SelectedValue = "1" Then
    txtMeeting.Text = "SMC Weekly Meeting"
Else
    txtMeeting.Text = "SMC Monthly Meeting"
End If

答案 1 :(得分:1)

首先检查您的值是否为数字,如果是,则将其转换为整数,并将其与1进行比较:

If IsNumeric(DropDownList1.SelectedValue) AndAlso CInt(DropDownList1.SelectedValue)=1
    txtMeeting.Text = "SMC Weekly Meeting"
Else
    txtMeeting.Text = "SMC Monthly Meeting"
End If

答案 2 :(得分:0)

您需要检查IsPostBack否则将重置ddl SelectValue

注意:我的Vb.Net有点生疏

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Me.IsPostBack Then

        'This works fine
        lblTodaysDate.Text = GetDate()

        'I thought i could complete an If Statement to get the text box to work.
        If DropDownList1.SelectedValue = "1" Then
            txtMeeting.Text = "SMC Weekly Meeting"
        Else
            txtMeeting.Text = "SMC Monthly Meeting"
        End If
    Else
        ' Put your code to populate the ddl here


    End If
End Sub