当单击列表框中的数据时,数据将转到文本框 [英] when click on a data in listbox the data goes to textbox

查看:114
本文介绍了当单击列表框中的数据时,数据将转到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,程序员,这里有个小问题.如您所见,我将数据绑定到列表框,并且效果很好.现在我想要的是,当我单击列表框中的数据时,数据将转到文本框.
这是我的代码
关于绑定数据表单列表框

hello programmers, got a small problem here. as you see i bound my data to my listbox and it works perfectly. now i want is that when i click data from the listbox, the data goes to the textbox.
here is my code
On binding data form listbox

Dim strSQL As String
      Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
      strSQL = "SELECT [CourseTitle] FROM [Training]"
      connection.Open()
      Dim command As New SqlCommand(strSQL, connection)
      listTitle.DataSource = command.ExecuteReader
      listTitle.DataTextField = "CourseTitle"
      listTitle.DataValueField = "CourseTitle"
      listTitle.DataBind()
      command.Dispose()
      connection.Close()


在我的清单标题上


on my listtitle

Protected Sub listTitle_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbTitle.SelectedIndexChanged
    textboxTitle.Text = listTitle.SelectedItem.ToString

End Sub


但是当我运行它时,发生了一个错误,说
Object reference not set to an instance of an object.指向textboxtitle.text =(listTitle.SelectedItem.ToString())
请帮助我不知道是什么问题.
当我将其更改为


but when i run it an error occured saying
Object reference not set to an instance of an object. pointing at textboxtitle.text = (listTitle.SelectedItem.ToString())
plss help me dont know what is the problem.
and when i change it to

Protected Sub listTitle_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbTitle.SelectedIndexChanged
    textboxTitle.Text = listTitle.SelectedValue.ToString
End Sub


没有错误,但数据没有出现在文本框中


感谢所有人和您的时间.愿上帝保佑您的帮助

对不起我的英语.


there is no error but the data doesnt appear on textbox


Thanks to all and for your time. May God bless you for your helping hand

sorry for my english.

推荐答案

在editTP.vb
on editTP.vb
Imports System.Web
Imports System.Data.SqlClient
Imports System.Data
Partial Class EditTP
    Inherits System.Web.UI.Page

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

        Dim strSQL As String
        Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        strSQL = "SELECT [CourseTitle] FROM [tblTrainingPlan] WHERE ([Category] LIKE '%' + @Category + '%')"
        connection.Open()

        Dim command As New SqlCommand(strSQL, connection)
        command.Parameters.AddWithValue("@Category", ddcategory.Text)
        cbTitle.DataSource = command.ExecuteReader
        cbTitle.DataTextField = "CourseTitle"
        cbTitle.DataValueField = "CourseTitle"
        cbTitle.DataBind()
        command.Dispose()
        connection.Close()
        'textboxTitle.Text = cbTitle.SelectedValue.ToString

    End Sub

    Protected Sub cbTitle_SelectedIndexChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbTitle.SelectedIndexChanged

        textboxTitle.Text = ("Double Click was fired selected item is " + cbTitle.SelectedValue.ToString)


    End Sub
End Class


在我的源代码上


on my source code

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="left: 0px; width: 298px; position: relative; top: 0px; height: 32px">
            <asp:DropDownList ID="ddcategory" runat="server" Height="27px" Width="293px" AutoPostBack="True">
                <asp:ListItem Value="Fluor University Functional Courses (instructor-led)">Fluor University Functional Courses (instructor-led)</asp:ListItem>
                <asp:ListItem Value="GEE 51 Bulletin Requirements">GEE 51 Bulletin Requirements</asp:ListItem>
                <asp:ListItem Value="Cross Functional Courses">Cross Functional Courses</asp:ListItem>
                <asp:ListItem Value="Functional Courses (online)">Functional Courses (online)</asp:ListItem>
                <asp:ListItem Value="Local Functional Courses (instructor-led)">Local Functional Courses (instructor-led)</asp:ListItem>
                <asp:ListItem Value="External Courses">External Courses</asp:ListItem>
                <asp:ListItem Value="7Other Courses">Other Courses</asp:ListItem>
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList></div>
        <div style="left: 0px; width: 293px; position: relative; top: 14px; height: 666px; overflow: auto;">
            <asp:ListBox ID="cbTitle" runat="server" Font-Size="X-Small"

                Height="640px" Width="269px" DataTextField="CourseTitle" DataValueField="CourseTitle" SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox></div>
        <asp:Button ID="Button1" runat="server" Text="Button" Width="142px" />&nbsp;
        <div style="left: 405px; width: 314px; position: relative; top: -658px; height: 66px">
            <asp:TextBox ID="textboxTitle" runat="server" Height="36px" Width="296px"></asp:TextBox></div>
        &nbsp;
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%


ConnectionStrings:TestSQLConnectionString %> " span> =" 从[tblTrainingPlan]的位置选择[CourseTitle]([类别]喜欢'%'+ @Category +'%')" < SelectParameters > < asp:ControlParameter ControlID =" 名称 类别" PropertyName SelectedValue" 类型 =" > < /SelectParameters > < /asp:SqlDataSource > < /div > < /form > < /body > < /html >
ConnectionStrings:TestSQLConnectionString %>" SelectCommand="SELECT [CourseTitle] FROM [tblTrainingPlan] WHERE ([Category] LIKE '%' + @Category + '%')"> <SelectParameters> <asp:ControlParameter ControlID="ddcategory" Name="Category" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:SqlDataSource> </div> </form> </body> </html>


如果未在页面加载中回发条件,则将其放入
put if not post back condition in your page load
<br />
<br />
If Not IsPostBack<br />
Dim strSQL As String<br />
        Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")<br />
        strSQL = "SELECT [CourseTitle] FROM [tblTrainingPlan] WHERE ([Category] LIKE ''%'' + @Category + ''%'')"<br />
        connection.Open()<br />
 <br />
        Dim command As New SqlCommand(strSQL, connection)<br />
        command.Parameters.AddWithValue("@Category", ddcategory.Text)<br />
        cbTitle.DataSource = command.ExecuteReader<br />
        cbTitle.DataTextField = "CourseTitle"<br />
        cbTitle.DataValueField = "CourseTitle"<br />
        cbTitle.DataBind()<br />
        command.Dispose()<br />
        connection.Close()<br />
End If<br />



看看是否可行



see if that works


这篇关于当单击列表框中的数据时,数据将转到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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