ASP.net DropDownList的GridView的填充 [英] ASP.net DropDownList populates GridView

查看:95
本文介绍了ASP.net DropDownList的GridView的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立在GridView过滤属于特定员工的凭据。将DropDownList为员工提供的名单,一旦选定我想在GridView只填充属于特定员工的条目。

 < ASP:内容ID =HeaderContent=服务器ContentPlaceHolderID =HeadContent>
< / ASP:内容>
< ASP:内容ID =的BodyContent=服务器ContentPlaceHolderID =日程地址搜索Maincontent>
    < ASP:DropDownList的ID =DropDownListEmployee=服务器
                      的AutoPostBack =真
                      OnSelectedIndexChanged =SelectionHasChanged
                      的DataSourceID =SqlDataSource2DataTextField =全名
                      DataValueField =的Employee_IDAppendDataBoundItems =真
                      WIDTH =214px>
        < ASP:ListItem的>选择< / ASP:ListItem的>
    < / ASP:DropDownList的>
    < ASP:SqlDataSource的ID =SqlDataSource2=服务器
                       的ConnectionString =下;%$的ConnectionStrings:DBConnectionString%>中
                       的SelectCommand =SELECT Employee.F_Name +''+ Employee.L_Name AS全名,Employee.Primary_Address,Employee.Primary_Phone,Employee.E_mail,Credentials.Degree,Credentials.Years_Experience,Credentials.Certifications,Credentials.Positions,Employee.Employee_ID FROM员工INNER JOIN凭据Employee.Employee_ID = Credentials.Employee_ID>< / ASP:SqlDataSource的>
    < ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE
                  的DataSourceID =SqlDataSource2的DataKeyNames =的Employee_ID>
        <柱体和GT;
            < ASP:BoundField的数据字段=全名的HeaderText =全名
                            只读=真SORTEX pression =全名/>
            < ASP:BoundField的数据字段=Primary_Address的HeaderText =Primary_Address
                            SORTEX pression =Primary_Address/>
            < ASP:BoundField的数据字段=Primary_Phone的HeaderText =Primary_Phone
                            SORTEX pression =Primary_Phone/>
            < ASP:BoundField的数据字段=企业邮箱的HeaderText =企业邮箱
                            SORTEX pression =企业邮箱/>
            < ASP:BoundField的数据字段=度的HeaderText =度
                            SORTEX pression =度/>
            < ASP:BoundField的数据字段=Years_Experience的HeaderText =Years_Experience
                            SORTEX pression =Years_Experience/>
            < ASP:BoundField的数据字段=资质证书的HeaderText =认证
                            SORTEX pression =认证/>
            < ASP:BoundField的数据字段=位置的HeaderText =阵地
                            SORTEX pression =阵地/>
            < ASP:BoundField的数据字段=的Employee_ID的HeaderText =的Employee_ID
                            InsertVisible =FALSE只读=真
                            SORTEX pression =的Employee_ID/>
        < /专栏>
    < / ASP:GridView的>
< / ASP:内容>


解决方案

您没有说与当前的code什么一切发生的时候,但我敢打赌,当你选择一个员工,重新加载页面,你会得到全体员工在你的数据库列表,对吧?

如果这是真的,其原因是因为相同的SqlDataSource绑定到你的DropDownList都和你的GridView,并为SqlDataSource的Select命令检索所有员工 - 没有WHERE条件来选择所需的员工。

我会用2 SqlDataSources - 一个用于DropDownList的,一个为GridView。第二个SqlDataSource控件将有一个选择命令来获得所需的员工信息的基础上,在DropDownList选择。

您可以修改您SqlDataSource2的SelectCommand只返回全名和雇员领域,因为你不需要休息您的DropDownList:

 < ASP:内容ID =的BodyContent=服务器ContentPlaceHolderID =日程地址搜索Maincontent>
    < ASP:DropDownList的ID =DropDownListEmployee=服务器
                      的AutoPostBack =真
                      OnSelectedIndexChanged =SelectionHasChanged
                      的DataSourceID =SqlDataSource2DataTextField =全名
                      DataValueField =的Employee_IDAppendDataBoundItems =真
                      WIDTH =214px>
        < ASP:ListItem的>选择< / ASP:ListItem的>
    < / ASP:DropDownList的>
    < ASP:SqlDataSource的ID =SqlDataSource2=服务器
                       的ConnectionString =下;%$的ConnectionStrings:DBConnectionString%>中
                       的SelectCommand =SELECT F_Name +''+ L_Name AS全名,FROM的Employee_ID员工>< / ASP:SqlDataSource的>

在你的第二个SqlDataSource的,你需要一个参数(雇员)添加到SelectParameters集合如下图所示,并更新您的SelectCommand采取的参数。

 < ASP:SqlDataSource的ID =gvDataSource=服务器
                       的ConnectionString =下;%$的ConnectionStrings:DBConnectionString%>中
                       的SelectCommand =SELECT Employee.F_Name +''+ Employee.L_Name AS全名,Employee.Primary_Address,Employee.Primary_Phone,Employee.E_mail,Credentials.Degree,Credentials.Years_Experience,Credentials.Certifications,Credentials.Positions,Employee.Employee_ID FROM员工INNER JOIN凭据Employee.Employee_ID = Credentials.Employee_ID WHERE Employee.EmployeeID = ISNULL(@EmployeeID,雇员)>
        < SelectParameters>
            < ASP:ControlParameter控件ID =DropDownListID
                 ConvertEmptyStringToNull =真NAME =雇员
                 属性名=的SelectedValue/>
        < / SelectParameters>

那么这个SqlDataSource的分配到GridView,而不是第一个。

需要注意的是通过提交到参数的值在SqlDataSource没有验证 - 这可能是一个安全威胁。

SqlDataSource.Select方法

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.selectparameters.aspx\"相对=nofollow> SqlDataSource.SelectParameters物业

I'm trying to set up the GridView to filter the Credentials that belong to a specific Employee. The dropdownlist provides the list of employees, once selected I want the gridview to only populate entries that belong to the specific employee.

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:DropDownList ID="DropDownListEmployee" runat="server"  
                      AutoPostBack="True" 
                      OnSelectedIndexChanged="SelectionHasChanged"
                      DataSourceID="SqlDataSource2" DataTextField="Fullname"
                      DataValueField="Employee_ID" AppendDataBoundItems="true"
                      Width="214px">
        <asp:ListItem>Select</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                       ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
                       SelectCommand="SELECT Employee.F_Name + ' ' + Employee.L_Name AS Fullname, Employee.Primary_Address, Employee.Primary_Phone, Employee.E_mail, Credentials.Degree, Credentials.Years_Experience, Credentials.Certifications, Credentials.Positions, Employee.Employee_ID FROM Employee INNER JOIN Credentials ON Employee.Employee_ID = Credentials.Employee_ID"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                  DataSourceID="SqlDataSource2" DataKeyNames="Employee_ID">
        <Columns> 
            <asp:BoundField DataField="Fullname" HeaderText="Fullname" 
                            ReadOnly="True" SortExpression="Fullname" />
            <asp:BoundField DataField="Primary_Address" HeaderText="Primary_Address" 
                            SortExpression="Primary_Address" />
            <asp:BoundField DataField="Primary_Phone" HeaderText="Primary_Phone" 
                            SortExpression="Primary_Phone" />
            <asp:BoundField DataField="E_mail" HeaderText="E_mail" 
                            SortExpression="E_mail" />
            <asp:BoundField DataField="Degree" HeaderText="Degree" 
                            SortExpression="Degree" />
            <asp:BoundField DataField="Years_Experience" HeaderText="Years_Experience" 
                            SortExpression="Years_Experience" />
            <asp:BoundField DataField="Certifications" HeaderText="Certifications" 
                            SortExpression="Certifications" />
            <asp:BoundField DataField="Positions" HeaderText="Positions" 
                            SortExpression="Positions" />
            <asp:BoundField DataField="Employee_ID" HeaderText="Employee_ID" 
                            InsertVisible="False" ReadOnly="True" 
                            SortExpression="Employee_ID" />
        </Columns>
    </asp:GridView>
</asp:Content>

解决方案

You didn't say what happenes with your current code, but I'm betting that when you select an employee, the page reloads and you get a list of all the employees in your database, right?

If that's true, the reason is because the same SqlDataSource is bound to both your DropDownList and your GridView, and the Select command for the SqlDataSource retrieves all the employees - there is no WHERE criteria to select a desired employee.

I would use 2 SqlDataSources - one for the DropDownList, and one for the GridView. The second SqlDataSource would have a Select command to get the desired employee's information, based on the selection in the DropDownList.

You can modify your SqlDataSource2's SelectCommand to return only the fullname and EmployeeID fields, as you don't need the rest for your DropDownList:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:DropDownList ID="DropDownListEmployee" runat="server"  
                      AutoPostBack="True" 
                      OnSelectedIndexChanged="SelectionHasChanged"
                      DataSourceID="SqlDataSource2" DataTextField="Fullname"
                      DataValueField="Employee_ID" AppendDataBoundItems="true"
                      Width="214px">
        <asp:ListItem>Select</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                       ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
                       SelectCommand="SELECT F_Name + ' ' + L_Name AS Fullname, Employee_ID FROM Employee"></asp:SqlDataSource>

In your second SqlDataSource, you'll need to add a parameter (EmployeeID) to the SelectParameters collection as shown below, and update your SelectCommand to take the parameter.

    <asp:SqlDataSource ID="gvDataSource" runat="server"
                       ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
                       SelectCommand="SELECT Employee.F_Name + ' ' + Employee.L_Name AS Fullname, Employee.Primary_Address, Employee.Primary_Phone, Employee.E_mail, Credentials.Degree, Credentials.Years_Experience, Credentials.Certifications, Credentials.Positions, Employee.Employee_ID FROM Employee INNER JOIN Credentials ON Employee.Employee_ID = Credentials.Employee_ID WHERE Employee.EmployeeID = IsNull(@EmployeeID, EmployeeID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownListID" 
                 ConvertEmptyStringToNull="true" Name="EmployeeID"
                 PropertyName="SelectedValue" />
        </SelectParameters>

Then assign this SqlDataSource to the GridView instead of the first one.

Note that there is no validation by the SqlDataSource on the values submitted into the parameters - which can be a security threat.

SqlDataSource.Select Method

SqlDataSource.SelectParameters Property

这篇关于ASP.net DropDownList的GridView的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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