1

一开始我不得不说我试图找到答案......是的,我是 ASP.Net 世界的新手 :)

我想在 GridView 的 EditItemTemplate 字段中使用 DropDownList。我发现我无法设置参数SelectedValue。它不见了。当我尝试在后面的代码中设置它时,似乎ddlEditPermissions不存在。

<asp:TemplateField HeaderText="opravneni" SortExpression="opravneni">
<edititemtemplate>
    <asp:DropDownList ID="ddlEditPermissions" runat="server" DataSource='<%# getPermissions() %>' OnPreRender="ddlEditPermissions_PreRender"/>
</edititemtemplate>
<insertitemtemplate>
    <asp:TextBox ID="tbEditPermissions" runat="server" Text='<%# Bind("opravneni") %>'></asp:TextBox>
</insertitemtemplate>
<itemtemplate>
    <asp:Label ID="lEditPermissions" runat="server" Text='<%# Bind("opravneni") %>'></asp:Label>
</itemtemplate>

我真的很困惑。谁能给我建议?

4

1 回答 1

1

您可以使用whichRowDataBound在构建和数据绑定后GridView每次触发:GridViewRow

protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
    {
        var ddlEditPermissions = (DropDownList)e.Row.FindControl("ddlEditPermissions");
        // bind DropDown manually
        ddlEditPermissions.DataSource = getPermissions();
        ddlEditPermissions.DataTextField = "Permission_Name"; // presumed text-column
        ddlEditPermissions.DataValueField = "Permission_ID";  // presumed id-column
        ddlEditPermissions.DataBind();

        DataRowView dr = e.Row.DataItem as DataRowView;  // you might need to change this type, use the debugger then to determine it
        ddlEditPermissions.SelectedValue = dr["Permission_ID"].ToString(); // presumed foreign-key-column 
    }
}
于 2013-08-30T08:34:22.553 回答