您的位置:首页 > 运维架构

GridView控件编辑状态下的DropDownList控件选中原值

2009-08-10 11:29 411 查看
如果DropDownList控件的数据源可以直接调用某个方法就能绑定的话,可以在HTML中对DropDownList进行原值定位
HTML:
<asp:TemplateField HeaderText="专业">
<EditItemTemplate>
<asp:DropDownList id="ddlMajorBind" runat="server" DataTextField="MajorName" DataValueField="MajorID" DataSource="<%#bindMajor() %>" SelectedValue='<%# Bind("MajorID") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label id="Label3" runat="server" Text='<%# Bind("MajorName") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>C#:

public DataSet bindMajor()
{
return new MajorBLL().SelectTheMajorByDeptID(Session["DeptID"].ToString());
}如果DropDownList控件的数据源不能直接调用某个方法绑定,必须在RowDataBind事件中进行绑定的话,原值定位也需要在此事件中完成

if (e.Row.RowType == DataControlRowType.DataRow)
{
//GridView控件的编辑状态下下拉列表选中原值

if (e.Row.FindControl("ddlMajorBind") != null && e.Row.FindControl("ddlDirectionBind") != null)
{
DropDownList ddlMajor = (DropDownList)e.Row.FindControl("ddlMajorBind");
DropDownList ddlDirection = (DropDownList)e.Row.FindControl("ddlDirectionBind");
ddlDirection.DataSource = bindDirection(ddlMajor.SelectedValue);
ddlDirection.DataTextField = "DirectionName";
ddlDirection.DataValueField = "DirectionId";
ddlDirection.DataBind();
ddlDirection.Items.Insert(0, new ListItem("无", ""));
ddlDirection.SelectedValue = DataBinder.Eval(e.Row.DataItem, "DirectionID").ToString();//原值定位
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐