SQL数据源selectedvalue参数错误
我有一个下拉菜单和一个网格视图。
gridview 数据源依赖于 dropdown.SelectedValue 下拉
列表:
<asp:DropDownList ID="DropDownListLoggedInUser" runat="server" autopostback="True"
DataSourceID="SqlDataSource3" DataTextField="Medarbejder"
DataValueField="Medarbejder"
onload="DropDownListLoggedInUser_Load">
</asp:DropDownList>
它的数据源:
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:EGWebtidConnectionString %>"
SelectCommand="SELECT [Medarbejder] FROM [Sager] WHERE ([Medarbejder] IS NOT NULL)">
</asp:SqlDataSource>
gridview:
<asp:GridView ID="GridViewSagsoversigt" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Style="text-align: left" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
OnPreRender="GridViewSagsoversigt_PreRender" DataKeyNames="Sagsnr" EnablePersistedSelection="True"
SelectedIndex="0" OnSelectedIndexChanged="GridViewSagsoversigt_SelectedIndexChanged">
它的数据源:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EGWebtidConnectionString %>"
SelectCommand="SELECT Sagsnr, Arbejskort, Adresse, Postnr, [By], Beskrivelse, Bemaerkning, Ansvarlig, Medarbejder FROM Sager WHERE ([Medarbejder] LIKE '%' + @Medarbejder + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListLoggedInUser" Name="Medarbejder"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
页面加载时。在 Dropdown 完全加载并设置 SelectedValue
之前,Gridview 会请求 Dropdown.SelectedValue
。因此 SelectedValue
返回 ""
并且 Gridview 不显示任何内容。
当我单击下拉菜单并选择一个名称时,它会进行回发并且一切都按计划进行。
那么我如何确保首先加载下拉列表,然后加载 gridview?
I have a dropdown and a gridview.
The gridview datasource is dependent on dropdown.SelectedValue
The dropdown:
<asp:DropDownList ID="DropDownListLoggedInUser" runat="server" autopostback="True"
DataSourceID="SqlDataSource3" DataTextField="Medarbejder"
DataValueField="Medarbejder"
onload="DropDownListLoggedInUser_Load">
</asp:DropDownList>
It's datasource:
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:EGWebtidConnectionString %>"
SelectCommand="SELECT [Medarbejder] FROM [Sager] WHERE ([Medarbejder] IS NOT NULL)">
</asp:SqlDataSource>
The gridview:
<asp:GridView ID="GridViewSagsoversigt" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Style="text-align: left" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
OnPreRender="GridViewSagsoversigt_PreRender" DataKeyNames="Sagsnr" EnablePersistedSelection="True"
SelectedIndex="0" OnSelectedIndexChanged="GridViewSagsoversigt_SelectedIndexChanged">
Its datasource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EGWebtidConnectionString %>"
SelectCommand="SELECT Sagsnr, Arbejskort, Adresse, Postnr, [By], Beskrivelse, Bemaerkning, Ansvarlig, Medarbejder FROM Sager WHERE ([Medarbejder] LIKE '%' + @Medarbejder + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListLoggedInUser" Name="Medarbejder"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
When the page loads. The Gridview asks for Dropdown.SelectedValue
, before Dropdown has fully loaded and set a SelectedValue
. So SelectedValue
returns ""
and the Gridview shows nothing.
When I click on the dropdown and choose a name, it does a postback and it all works as planned.
So how can I make sure, that dropdown loads first and gridview loads second?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将控制权授予数据源控件时,您无法显式控制它。您可以做的是在加载下拉列表后显式调用 gridview 上的 DataBind() 以再次重新绑定到数据库。您还可以通过点击 Selecting 事件并设置 e.Cancel = true 来取消第一个 gridview 加载(如果您想消除数据库调用)。
我不知道 DataSourceControls 的顺序是否会影响执行的优先级...我不知道这是否也是一个因素...
HTH。
When you give control to the data source controls, you can't explicitly control this. What you can do is explicitly call DataBind() on the gridview after the loading of the dropdown to rebind to the database again. You can also cancel the very first gridview load (if you want to eliminate a database call) by tapping into the Selecting event and setting e.Cancel = true.
I don't know if the ordering of DataSourceControls affects the priority of the execution... I don't know if that is a factor too...
HTH.
在 Page_Load() 方法中
At Page_Load() method