我正按照之前的要求做this。我发现的唯一区别是上面代码中包含的附加List项。
我尝试使用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
答案 0 :(得分:8)
DataValueField
似乎错了 - 不应该是DepartmentId
吗?同样,您需要SelectedValue='<%# Eval("**DepartmentId**") %>'
- DepartmentName
为SeletectText
。
答案 1 :(得分:3)
使用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;
}
}
}
答案 2 :(得分:1)
为什么你们建议使用循环,当有一个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;
}
}
}
答案 3 :(得分:0)
在您的网格上有一个名为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
,并将所选项目设置为此字符串。
答案 4 :(得分:0)
这是我发现的最好的......
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;
}
}
}