0

假设我有 ProjectCode 文本框:

<td align="left" width="200px">
  <asp:TextBox ID="TbProjectCode" runat="server" Width="194px"></asp:TextBox>
</td>

和一个图像按钮:

<asp:ImageButton ID="BtnSearch" runat="server" ImageUrl="../Support/Image/MagnifierGlass.png" Width="75%" Height="75%" OnClientClick="openNewWin();return false;" />

和一个网格视图:

<asp:Panel ID="PanelDGV" runat="server" Height="100%" ScrollBars="None" Width="100%">
  <asp:GridView ID="DGV" runat="server" AutoGenerateColumns="False" GridLines="None" AllowPaging="true" PageSize="2" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
      <Columns>
  <asp:BoundField DataField="ProjectCode" HeaderText="Project Code" />
  <asp:BoundField DataField="ProjectName" HeaderText="Project Name" />
  <asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
       </asp:ButtonField>
          </Columns>
             <PagerStyle CssClass="pgr"></PagerStyle>
                <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
                    </asp:GridView>
                       </asp:Panel>

并且我正在使用带有查询的存储过程(我从另一个数据库中获取值,请注意 Master 中的双点..[MS_Project]):

SELECT [projectCode],[projectName]
  FROM Master..[MS_Project]
  WHERE [projectCode] like '%' + @ProjectCode + '%'
  ORDER BY [projectCode] ASC

我想创建一个搜索功能,所以用户在文本框中输入他们想要的项目代码,然后单击图像按钮,然后搜索结果应该显示在gridview中,有没有这样做?谢谢你。

编辑

我在.vb中添加:

    Protected Sub BtnSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BtnSearch.Click
    Dim ds As New DataSet()

    Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("CfgConnectionString").ToString())
        Using command As New SqlCommand()
            command.CommandType = CommandType.StoredProcedure
            command.CommandText = "msProject_Select"
            command.Connection = connection

            command.Parameters.AddWithValue("@ProjectCode", TbProjectCode.Text)

            connection.Open()
            Dim a As New SqlDataAdapter(command)
            a.Fill(ds)
        End Using
    End Using

    DGV2.DataSource = ds
    DGV2.DataBind()
End Sub

结束类

4

1 回答 1

0

这是你可以做的:

protected void BtnSearch_Click(object sender, ImageClickEventArgs e) 
{
    DataSet ds = new DataSet();

    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ToString()))
        {
            using (SqlCommand command = new SqlCommand())
            {
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "youProcedureName";
                command.Connection = connection;

                command.Parameters.AddWithValue("@ProjectCode", TbProjectCode.Text);

                connection.Open();
                SqlDataAdapter a = new SqlDataAdapter(command);
                a.Fill(ds);
             }
        }

    DGV.DataSource = ds;
    DGV.DataBind();
}

这个想法很简单。在您的 Search(click) 事件中,您将使用新查询重新绑定您GridViewDataSourceSELECT查询。

VB.NET

Protected Sub BtnSearch_Click(sender As Object, e As ImageClickEventArgs)
Dim ds As New DataSet()

Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("YourConnectionString").ToString())
    Using command As New SqlCommand()
        command.CommandType = CommandType.StoredProcedure
        command.CommandText = "youProcedureName"
        command.Connection = connection

        command.Parameters.AddWithValue("@ProjectCode", TbProjectCode.Text)

        connection.Open()
        Dim a As New SqlDataAdapter(command)
        a.Fill(ds)
    End Using
End Using

DGV.DataSource = ds
DGV.DataBind()
End Sub

试试这个vb代码。我已经使用转换工具来获得这个。也许您可能需要在这里进行一些修改。

根据您的评论:

在您的页面上添加此行。

public string CfgConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("CfgConnectionString‌​").ConnectionString;

并像这样更改您的第一行:

Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("CfgConnectionString").ToString())

你也失踪了

Dim ds As New DataSet()
于 2013-05-13T07:27:57.857 回答