<asp:TemplateField HeaderText="父节点">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource_ZipNode" DataTextField="DistrictDesc"
DataValueField="DistrictId" SelectedValue='<%# Bind("ParentId") %>'
AppendDataBoundItems="True" ondatabinding="DropDownList1_DataBinding"
ondatabound="DropDownList1_DataBound"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Enabled="false"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource_ZipNode" runat="server"
ConnectionString="<%$ ConnectionStrings:connStr %>"
SelectCommand="prd_SelectDistrictNodes" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ParentId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
要点已经标出
//上面这种方法有局限性,还有一种通用的方法
<EditItemTemplate> 模版里的DropDownList 不用SelectedValue绑定
在GridView事件 _RowUpdating
DropDownList objtemp_ZipId=GV_SheBeiDelete.Rows[e.RowIndex].FindControl("DropDownList3") as DropDownList;
DropDownList objtemp_DvsTypeId = GV_SheBeiDelete.Rows[e.RowIndex].FindControl("DropDownList4") as DropDownList;
e.NewValues["ZipId"] = objtemp_ZipId.Text;
e.NewValues["DvsTypeId"] = objtemp_DvsTypeId.Text;
当然对于编辑模版里的 DropDownList 要写事件 DropDownList4_DataBound
DropDownList obj = sender as DropDownList;
try
{
obj.Text = DvsTypeId_GV_SheBeiDelete; ; //变量,其取值在GridView的RowEditing事件里
}
catch
{
}
本文介绍ASP.NET中使用GridView控件结合DropDownList实现数据编辑的方法。一种方式是在编辑模板中直接设置DropDownList的SelectedValue属性,另一种则是在_RowUpdating事件中获取DropDownList的值更新到数据源。文中还涉及了绑定事件及获取控件实例的具体步骤。
195

被折叠的 条评论
为什么被折叠?



