我正在尝试按照之前的要求做this。我发现的唯一区别是上面代码中包含的附加列表项。
我尝试使用
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
DataValueField
好像是错的——不应该是DepartmentId
吗?同样,您需要有 SelectedValue='<%# Eval("**DepartmentId**") %>'
- DepartmentName
就是 SeletectText
。
使用
GridView_DataBound
事件处理程序解决了这个问题。
在您的情况下,您需要添加
HiddenField
来存储 PK_DepartmentId
值:
<asp:GridView ID="gvExample" runat="server" AutoGenerateColumns="False" OnDataBound="gvExample_DataBound">
<Columns>
<asp:TemplateField HeaderText="Department">
<EditItemTemplate>
<asp:DropDownList ID="ddlDepartment_Edit" runat="server" DataSourceID="dsDepartment_Edit"
DataTextField="DepartmentName" DataValueField="PK_DepartmentId">
</asp:DropDownList>
<asp:HiddenField ID="hfDepartmentId" runat="server" Value='<%# Bind("PK_DepartmentId") %>' />
<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>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ButtonType="Button" />
</Columns>
</asp:GridView>
protected void gvExample_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in gvExample.Rows)
{
DropDownList ddlDepartment = gvRow.FindControl("ddlDepartment_Edit") as DropDownList;
HiddenField hfDepartmentId = gvRow.FindControl("hfDepartmentId") as HiddenField;
if (ddlDepartment != null && hfDepartmentId != null)
{
ddlDepartment.SelectedValue = hfDepartmentId.Value;
}
}
}
为什么你们建议使用循环,当有一个
GridView
专门用于当行的条件发生变化时的方法 - RowDataBound()
?
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvRow = (GridViewRow)e.Row;
HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID");
if (hfAgentID != null)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent");
ddlAgent.SelectedValue = hfAgentID.Value;
}
}
}
这是我发现的最好的......
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
RadioButtonList rbl = gvRow.FindControl("rblPromptType") as RadioButtonList;
HiddenField hf = gvRow.FindControl("hidPromptType") as HiddenField;
if (rbl != null && hf != null)
{
if (hf.Value != "")
{
//clear the default selection if there is one
rbl.ClearSelection();
}
rbl.SelectedValue = hf.Value;
}
}
}
在您的网格上有一个名为
ItemCommand
的事件。为其创建一个方法:
protected void Grid1_ItemCommand(object source, GridCommandEventArgs e)
现在只需创建一个 case 语句,该语句将识别用户何时单击网格上的编辑按钮:
case Grid.EditCommandName:
//set a member variable to the string of the cell you are editing.
//something like: mString = e.item..["Column"].toString();
break;
现在,您已经将一个成员变量设置为您想要在加载/预渲染下拉列表之前选择的字符串。使用事件
OnPrerender
或 OnLoad
作为 dropdownbox
并将所选项目设置为此字符串。
在 Visual Studio 2022 中,我无法使用 asp:HiddenField,但使用隐藏的
<asp:BoundField DataField="Count">
- 效果很好。之后的一个问题是,在 RowDataBound 处理程序中,e.Row.FindControl("Count") 对于隐藏的 我真正想要从“GridViewTest_RowDataBound”处理程序中获取与这一网格行相关的原始一行 SQL 数据。通过访问原始数据,我可以为 DropDownList 执行 FindControl,然后设置 DropDownList.SelectedIndex = iCount。
我找到了一个关于如何做到这一点的模糊参考。 从 GridViewTest_RowDataBound 处理程序中,添加以下代码:
int iCount = (int) DataBinder.Eval(e.Row.DataItem, "Count");
DropDownList DropDownListCount = (e.Row.FindControl("DropDownListCount") as DropDownList);
DropDownListCount.SelectedIndex = iCount;
在上面的示例中,“Count”是返回的 SQL 行集中所需数据列的名称。即使删除
有关使用 DataBinder 的 Microsoft 示例,请参阅 https://learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa479342(v=msdn.10)?redirectedfrom=MSDN。