C# DropDownlList的SelectedValue
我有一个gridview,在编辑部分有dropdownlist,我想在编辑时绑定数据库中的selectedvalue。在designer部分中没有SelectedValue属性,它给出运行时错误。有什么帮助吗??有没有办法从代码隐藏中处理它C# DropDownlList的SelectedValue,c#,asp.net,C#,Asp.net,我有一个gridview,在编辑部分有dropdownlist,我想在编辑时绑定数据库中的selectedvalue。在designer部分中没有SelectedValue属性,它给出运行时错误。有什么帮助吗??有没有办法从代码隐藏中处理它 <asp:TemplateField HeaderText="Company"> <EditItemTemplate> <asp:DropDownList
<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>
- 在代码隐藏中,您必须进行检查 对于e.Row.RowType== DataControlRowType.DataRow&& e、 Row.RowState== DataControlRowState.在中编辑 在找到您的 下拉列表。
- 在aspx上,您可以设置 按以下方式选择CedValue:
- 在代码隐藏中,您必须进行检查 对于e.Row.RowType== DataControlRowType.DataRow&& e、 Row.RowState== DataControlRowState.在中编辑 在找到您的 下拉列表。
- 在aspx上,您可以设置 按以下方式选择CedValue:
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();
}
}
}