<asp:TemplateColumn HeaderText="类型">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.类型") %>
</ItemTemplate><EditItemTemplate>
<asp:DropDownList id=ht_lx runat="server" Width="50px" DataSource="<%# ht_lx_bind() %>" DataTextField="属性" DataValueField="shuxingid"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>我希望在“编辑”后 在“更新取消”状态下 dropdownlist的默认选项与 <ItemTemplate>下是一样的我的.cs
public SqlDataReader ht_lx_bind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["redhat"].ToString());
SqlCommand cmd =new SqlCommand("select shuxingid,属性 from z_shuxing", conn);
conn.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}百思不得其解 求高手有更好的方法解决不知道说明白了没
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.类型") %>
</ItemTemplate><EditItemTemplate>
<asp:DropDownList id=ht_lx runat="server" Width="50px" DataSource="<%# ht_lx_bind() %>" DataTextField="属性" DataValueField="shuxingid"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>我希望在“编辑”后 在“更新取消”状态下 dropdownlist的默认选项与 <ItemTemplate>下是一样的我的.cs
public SqlDataReader ht_lx_bind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["redhat"].ToString());
SqlCommand cmd =new SqlCommand("select shuxingid,属性 from z_shuxing", conn);
conn.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}百思不得其解 求高手有更好的方法解决不知道说明白了没
表a
编号 类型
1 1
2 3
3 1
表bid 类型1 正式
2 临时
3 实习
我能在<EditItemTemplate></EditItemTemplate>中
动态的绑定表b 但是能不能默认是表a的类型希望在“编辑”后 在“更新取消”状态下 dropdownlist的默认选项与 <ItemTemplate>下是一样的效果
-------------------
能稍微写点关键的代码吗?
{
this.DataGrid.EditItemIndex = (int)e.Item.ItemIndex;
}
你再DataGrid中放个隐藏列存类型ID
private void DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (this.DataGrid.EditItemIndex == e.Item.ItemIndex)
{
RadioButtonList rdl = (RadioButtonList)e.Item.Cells[5].FindControl("ht_lx");
SetSelectedItem(rdl,e.Item.Cells[6].Text);
//SetSelectedItem选中下拉框方法,e.Item.Cells[6].Text你隐藏的类型ID
}
}
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
DropDownList drop=e.Item.FindControl("ht_lx");
drop.SelectedValue= Convert.ToString(DataBinder.Eval(e.Item.DataItem, "类型"))
}
1.先找到你编辑前的下拉框显示值
string str_BH = ((Label)e.Item.Cells[4].Controls[1]).Text; //cell[4]代表那个模板列号2.设置编辑状态,重新绑定数据
DataGrid1.EditItemIndex = e.Item.ItemIndex; DataGrid1.DataSource = Session["dt"]; //我的数据放到session中了
DataGrid1.DataBind();3.查询编辑状态时的DropDownList
DropDownList ddl_BH = (DropDownList)DataGrid1.Items[e.Item.ItemIndex].Cells[4].Controls[1];
//如果你要从数据库取值,在这里还要加一个DropDownList数据绑定4.指定DropDownList索引
ddl_BH.SelectedIndex = ddl_BH.Items.IndexOf(ddl_BH.Items.FindByValue(str_BH));
working1997(haha) ( ) 信誉:100 Blog
==================================================
DataGrid1_EditCommand的事件代码: string str_BH = ((Label)e.Item.Cells[3].Controls[1]).Text.Trim().ToString();
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindToSource();
DropDownList ddl_BH = (DropDownList)DataGrid1.Items[e.Item.ItemIndex].Cells[3].Controls[1];
ddl_BH.SelectedIndex = ddl_BH.Items.IndexOf(ddl_BH.Items.FindByValue(str_BH));
测试了下str_BH能得到值 但是
ddl_BH.SelectedIndex = ddl_BH.Items.IndexOf(ddl_BH.Items.FindByValue(str_BH));没起作用
就是ddl_BH.SelectedIndex = ddl_BH.Items.IndexOf(ddl_BH.Items.FindByValue("实习"));都没起作用
什么原因??
ddl_BH.SelectedIndex = ddl_BH.Items.IndexOf(ddl_BH.Items.FindByValue(str_BH))+1
能起点作用 就是没有动态效果,还是<EditItemTemplate> 和 <ItemTemplate>
绑定值不一样
是不是跟
<EditItemTemplate>
<asp:DropDownList id=ht_lx runat="server" Width="50px" DataSource="<%# ht_lx_bind() %>" DataTextField="属性" DataValueField="shuxingid"></asp:DropDownList>
</EditItemTemplate>
有关啊
===========================
已经给你发消息了
谢谢
ddl_BH.SelectedIndex = ddl_BH.Items.IndexOf(ddl_BH.Items.FindByValue(str_BH));
取不到indexLabel5.Text=ddl_BH.SelectedIndex.ToString(); 总是0直接给值 ddl_BH.SelectedIndex = 2起作用
吐血了,求明白人解惑