gridview中添加模版绑定dropdownlist控件,,然后点编辑控件时,如何把我的部门表的数据绑定到dropdownlist上
gridview中添加模版绑定dropdownlist控件,,然后点编辑控件时,如何把我的部门表的数据绑定到dropdownlist上,请大家帮忙!谢谢! --------------------编程问答-------------------- protected void gidview_RowDataBound(object sender, GridViewRowEventArgs e){
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl= (DropDownList)(e.Row.FindControl("对应id"));
ddl.DataSource = 数据源;
ddl.DataTextField = xxx;
ddl.DataValueField = xxx;
ddl.DataBind();
ddl.SelectedValue = ....
{
} --------------------编程问答-------------------- 给你个思路
你先绑定好了,你点模板列的时候,你再显示出来就可以了 --------------------编程问答-------------------- 先将GridView进入编辑模版状态,然后放一个ObjectDataSource把数据源绑定到Dropdownlist上,aspx页面代码
<asp:TemplateField HeaderText="属性分类">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("iPropertySortID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDLPropertySortID" runat="server" DataSourceID="ObjectDataSourcePropertySortName"
DataTextField="nPropertySortName" DataValueField="iPropertySortID" ></asp:DropDownList>
<asp:TextBox ID="txtPropertySortID" runat="server" Text='<%# Bind("iPropertySortID") %>' Visible="false"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
然后在将Dropdownlist初始选择的项为当前row中所显示的项。
asp.cs代码
//编辑状态--------------------编程问答-------------------- protected void gidview_RowDataBound(object sender, GridViewRowEventArgs e)
protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e)
{
//转换为DDL
string s = ((TextBox)GridView3.Rows[e.NewEditIndex].FindControl("txtPropertySortID")).Text;
((DropDownList)GridView3.Rows[e.NewEditIndex].FindControl("DDLPropertySortID")).SelectedValue = s;
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl= (DropDownList)(e.Row.FindControl("对应id"));
ddl.DataSource = 数据源;
ddl.DataTextField = xxx;
ddl.DataValueField = xxx;
ddl.DataBind();
ddl.SelectedValue = ....
{
} --------------------编程问答-------------------- ddl.SelectedValue =这后面的该如何写呢? --------------------编程问答-------------------- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sql = "select * from depName";
DropDownList ddl = ((DropDownList)e.Row.FindControl("ddlyytto"));
ddl.DataSource = this.da.getDataSet(sql);
ddl.DataTextField = "depName";
ddl.DataValueField = "depID";
ddl.DataBind();
ddl.SelectedValue = ((HiddenField)e.Row.FindControl("HiddenField")).Value;
}
}
我这样写报错...
补充:.NET技术 , ASP.NET