如何在GridView EditTemplate中设置DropDownList的SelectedValue
我正在尝试这个作为之前问过。我发现的唯一区别是上面代码中包含的附加列表项。
我尝试使用 AppendDataBoundItems=true ,但它仍然无法正常工作。我还想将其默认值设置为 itemtemplate 标签中显示的值,即 DropDownList 的 SelectedValue='<%# Eval("DepartmentName") %>'
但该属性是我在下拉列表中不可用。 可能是什么原因。 ??
<EditItemTemplate>
<asp:DropDownList ID="ddlDepartment_Edit" runat="server"
DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName"
DataValueField="PK_DepartmentId">
</asp:DropDownList>
<asp:SqlDataSource ID="dsDepartment_Edit" runat="server"
ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"
ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown"
SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' >
</asp:Label>
</ItemTemplate>
我正在使用 GridView
I am trying to do this as asked earlier. The only difference that I found is additional List item that was included in above code.
I tried to use AppendDataBoundItems=true
but it is still not working. I also want to set the its default value to the value that was being displayed in label of itemtemplate i.e. DropDownList's SelectedValue='<%# Eval("DepartmentName") %>'
but thie property is not available to me in dropdownlist.
What could be the reason. ??
<EditItemTemplate>
<asp:DropDownList ID="ddlDepartment_Edit" runat="server"
DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName"
DataValueField="PK_DepartmentId">
</asp:DropDownList>
<asp:SqlDataSource ID="dsDepartment_Edit" runat="server"
ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"
ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown"
SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' >
</asp:Label>
</ItemTemplate>
I am using GridView
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
DataValueField
似乎是错误的 - 不应该是DepartmentId
吗?同样,您需要有SelectedValue='<%# Eval("**DepartmentId**") %>'
-DepartmentName
将是SeletectText
。DataValueField
seems to be wrong - shouldn't it beDepartmentId
? Similarly, you need to haveSelectedValue='<%# Eval("**DepartmentId**") %>'
-DepartmentName
would be theSeletectText
.使用 GridView_DataBound 事件处理程序解决了该问题。
在您的情况下,您需要添加一个
HiddenField
来存储PK_DepartmentId
值:The use of the
GridView_DataBound
event handler solves the problem.In your case you will need to add a
HiddenField
to store thePK_DepartmentId
value:为什么你们建议使用循环,当有专门为行条件变化而设计的 GridView 方法 - RowDataBound() 时?
Why are you guys suggesting to use loops, when there is a
GridView
method specifically made for when a row's condition changes - theRowDataBound()
?这是我发现的最好的......
This is the best i have found....
在您的网格上有一个名为
ItemCommand
的事件。为其创建一个方法:现在只需创建一个 case 语句,该语句将识别用户何时单击网格上的编辑按钮:
现在您有一个成员变量设置为您希望在加载/预渲染下拉列表之前选择的字符串。使用
dropdownbox
事件OnPrerender
或OnLoad
并将所选项目设置为此字符串。On your grid there is an event called
ItemCommand
. Create a method for it:Now simply create a case statement that will recognize when the user has clicked the edit button on the grid:
Now you have a member variable set to the string you want to be selected before the dropdown is even loaded/prerendered. Use the event
OnPrerender
orOnLoad
for thedropdownbox
and set the selected item to this String.在 Visual Studio 2022 中,我无法使用 asp:HiddenField,但使用隐藏的返回 null。
- 效果很好。之后的一个问题是,在 RowDataBound 处理程序中,e.Row.FindControl("Count") 对于隐藏的我真正想要从“GridViewTest_RowDataBound”处理程序中获取与这一网格行相关的原始一行 SQL 数据。通过访问原始数据,我可以为 DropDownList 执行 FindControl,然后设置 DropDownList.SelectedIndex = iCount。
我找到了一份关于如何做到这一点的模糊参考。
从 GridViewTest_RowDataBound 处理程序中,添加以下代码:
在上面的示例中,“Count”是返回的 SQL 行集中所需数据列的名称。即使在之后,该代码仍然有效。被删除。
请参阅 https: //learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa479342(v=msdn.10)?redirectedfrom=MSDN 使用 DataBinder 的 Microsoft 示例。
In Visual Studio 2022, I wasn't able to use a asp:HiddenField, but using a
<asp:BoundField DataField="Count">
that was hidden - worked out well. One problem after that was that from the RowDataBound handler, e.Row.FindControl("Count") returned null for the hidden <asp:BoundField DataField="Count">.What I really wanted to do from the "GridViewTest_RowDataBound" handler was to get at the original one row of SQL DATA that was related to this one Grid row. With access to that original data, I could do a FindControl for the DropDownList and then set DropDownList.SelectedIndex = iCount.
I found one obscure reference for how to do that.
From your GridViewTest_RowDataBound handler, add the following code:
In the example above, "Count" is the name of the desired data column in the SQL rowset returned. That code works even after the <asp:BoundField DataField="Count"> is removed.
See https://learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa479342(v=msdn.10)?redirectedfrom=MSDN for a Microsoft example using DataBinder.