我有一个gridview,在编辑部分有下拉列表,我想在编辑时绑定数据库中的selectedvalue。在设计器部分中没有SelectedValue属性,它给出了运行时错误。怎么办?有没有办法从代码隐藏处理它?</ p>
<asp:TemplateField HeaderText="Company">
<EditItemTemplate>
<asp:DropDownList ID="DDLCompany" runat="server" DataValueField="cname" DataTextField="cname" SelectedValue = '<%# Bind("cname") %>' >
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CompanyLabel" runat="server" Text='<%# Bind("cname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DDLCompany = (DropDownList)e.Row.FindControl("DDLCompany");
DropDownList DDLPrinter = (DropDownList)e.Row.FindControl("DDLPrinter");
if (DDLCompany != null)
{
DDLCompany.DataSource = userobj.FetchCompanyList();
DDLCompany.DataBind();
DDLCompany.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();
}
if (DDLPrinter != null)
{
DDLPrinter.DataSource = userobj.FetchPrinterList();
DDLPrinter.DataBind();
DDLPrinter.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();
}
}
}
答案 0 :(得分:1)