使基于一个DropDownList选择一个ASP.NET GridView的变化 [英] Make an ASP.NET GridView change based on a DropDownList selection

查看:227
本文介绍了使基于一个DropDownList选择一个ASP.NET GridView的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一千种方式,使这项工作,我不能。我有一个从数据库.MDF绑定到数据列中的一个DropDownList。我也有它下面的GridView正在使用查询生成器在同一数据库绑定到列从4个不同的表。

我知道要做到这一点,你必须WHERE子句添加到查询字符串,看起来像这样 - WHERE([专栏] = @column) - 然后你必须把SelectParameters在HTML code。但我不知道如何将其正确写入。

我不断收到错误,当我在浏览器中运行的页面。这样的错误列暧昧,列不存在,必须声明标量变量@column等,我认为它可能有事可做的事实,三表有一个名为列描述和2他们有别名。

这里是HTML:

 <%@页标题=LANGUAGE =VB的MasterPageFile =〜/ MasterPage.masterAutoEventWireup =false的codeFILE =ByCategory.aspx。 VB继承=_默认%GT;< ASP:内容ID =内容1ContentPlaceHolderID =头=服务器>
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>
< ASP:标签ID =Label2的=服务器文本=选择一个类别:>< / ASP:标签>
< BR />
< ASP:DropDownList的ID =DropDownList1=服务器的DataSourceID =SqlDataSource2DataTextField =说明DataValueField =描述>< / ASP:DropDownList的>
< ASP:SqlDataSource的ID =SqlDataSource2=服务器的ConnectionString =<%$的ConnectionStrings:的ConnectionString%>中的SelectCommand =SELECT [说明] FROM【类别】>< / ASP:SqlDataSource的>
&安培; NBSP;< ASP:按钮的ID =btnGo=服务器文本=转到/>
&安培; NBSP;
< BR />
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE的cellpadding =4的DataSourceID =SqlDataSource3前景色=#333333网格=无可见=假>
    < AlternatingRowStyle背景色=白前景色=#284775/>
    <柱体和GT;
        < ASP:BoundField的数据字段=whenCreated的HeaderText =whenCreatedSORTEX pression =whenCreated/>
        < ASP:BoundField的数据字段=说明的HeaderText =说明SORTEX pression =说明/>
        < ASP:BoundField的数据字段=名字的HeaderText =名字SORTEX pression =名字/>
        < ASP:BoundField的数据字段=姓氏的HeaderText =姓氏SORTEX pression =姓氏/>
        < ASP:BoundField的数据字段=表达式1的HeaderText =表达式1SORTEX pression =表达式1/>
        < ASP:BoundField的数据字段=表达式2的HeaderText =表达式2SORTEX pression =表达式2/>
    < /专栏>
    < EditRowStyle背景色=#999999/>
    < FooterStyle背景色=#5D7B9DFONT-粗体=真前景色=白/>
    < HeaderStyle背景色=#5D7B9DFONT-粗体=真前景色=白/>
    < PagerStyle背景色=#284775前景色=白Horizo​​ntalAlign =中心/>
    < RowStyle的BackColor =#F7F6F3前景色=#333333/>
    < SelectedRowStyle背景色=#E2DED6FONT-粗体=真前景色=#333333/>
    < SortedAscendingCellStyle背景色=#E9E7E2/>
    < SortedAscendingHeaderStyle背景色=#506C8C/>
    < SortedDescendingCellStyle背景色=#FFFDF8/>
    < SortedDescendingHeaderStyle背景色=#6F8DAE/>
< / ASP:GridView的>
< ASP:SqlDataSource的ID =SqlDataSource3=服务器的ConnectionString =<%$的ConnectionStrings:的ConnectionString%>中的SelectCommand =SELECT bugEntry.whenCreated,bugEntry.description,employee.firstName,employee.lastName,category.description AS表达式1,status.description AS EXPR2 FROM bugEntry INNER JOIN类别在bugEntry.cat_id = category.cat_id INNER JOIN状态ON bugEntry。 STATUS_ID = status.status_id CROSS JOIN员工>
< / ASP:SqlDataSource的>
< / ASP:内容>


解决方案

我也有这种情况,这是我该如何解决。

我必将我的GridView的数据源,并使用SqlDataSource向导的帮助下建立查询
因为你使用多个表时,您配置数据源使用第一个选项指定自定义SQL语句或存储过程
现在点击下一步,然后使用查询生成器
在窗口上方第一窗格中,右键单击并添加表
选择你的表列按您的要求。

现在查询窗格写这在新的生产线在年底

WHERE([专栏] = @栏)

现在确定,然后单击下一步

新的窗口将出现
选择参数源
和控件ID单击下一步,你是好去。

希望这将有助于

I've tried a thousand ways to make this work and I can't. I have a DropDownList bound to the data in a column from an .mdf database. I also have a GridView below it that is bound using the Query Builder to columns from 4 different tables in that same database.

I know to do this you have to add a WHERE clause to the query string that looks something like this -- WHERE ([column] = @column) -- And then you have to put a SelectParameters in the HTML code. But I don't know how to write it correctly.

I keep getting errors when I run the page in the browser. Errors like "Column Ambiguous", "Column doesn't exist", "Must declare scalar variable @column", etc. I think it may have something to do with the fact that three of the tables have a column named description and 2 of them have aliases.

Here's the HTML:

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="ByCategory.aspx.vb" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label2" runat="server" Text="Select a category:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="description" DataValueField="description"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [description] FROM [category]"></asp:SqlDataSource>
&nbsp;<asp:Button ID="btnGo" runat="server" Text="Go" />
&nbsp;
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource3" ForeColor="#333333" GridLines="None" Visible="False">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:BoundField DataField="whenCreated" HeaderText="whenCreated" SortExpression="whenCreated" />
        <asp:BoundField DataField="description" HeaderText="description" SortExpression="description" />
        <asp:BoundField DataField="firstName" HeaderText="firstName" SortExpression="firstName" />
        <asp:BoundField DataField="lastName" HeaderText="lastName" SortExpression="lastName" />
        <asp:BoundField DataField="Expr1" HeaderText="Expr1" SortExpression="Expr1" />
        <asp:BoundField DataField="Expr2" HeaderText="Expr2" SortExpression="Expr2" />
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT bugEntry.whenCreated, bugEntry.description, employee.firstName, employee.lastName, category.description AS Expr1, status.description AS Expr2 FROM bugEntry INNER JOIN category ON bugEntry.cat_id = category.cat_id INNER JOIN status ON bugEntry.status_id = status.status_id CROSS JOIN employee">


</asp:SqlDataSource>
</asp:Content>

解决方案

i also have this scenario this is how i solved

i bound my Gridview with data source and build query with the help of the SqlDataSource Wizard when you configure data source use first option "specify custom SQl statement or stored procedure " because you are using multiple table now click next and use query builder on Top first pane of window right click and add your table choose column from your table as per your requirement

now in query pane write this in new line at the end

WHERE ([column]=@column)

now ok and click next

new window will appear select parameter source and control id click next and you are good to go

hope this will help

这篇关于使基于一个DropDownList选择一个ASP.NET GridView的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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