//以下是编辑 { this.dg.EditItemIndex=e.Item.ItemIndex; this.ShowAdmin();DropDownList ddb=dg.Items[e.Item.ItemIndex].FindControl("dpt_P_ID") as DropDownList; //DropDownList ddb=e.Item.FindControl("dpt_P_ID") as DropDownList;//此方法不行的。 MyDrop.DataTextField="P_Explain"; MyDrop.DataValueField="P_ID"; MyDrop.DataSource=fde.ExecuteDataSet("ShowPopedom 1"); MyDrop.DataBind(); ddb.Items.FindByText((e.Item.FindControl("lb_PID") as Label).Text).Selected=true; }
关键代码是:DropDownList ddb=dg.Items[e.Item.ItemIndex].FindControl("dpt_P_ID") as DropDownList; ddb.Items.FindByText((e.Item.FindControl("lb_PID") as Label).Text).Selected=true;
SelectedIndex(DropDownList1.Items.Count-1)?
this.ddljiguan.SelectedValue = rs["籍贯"].ToString().Trim();就可以了
{
this.dg.EditItemIndex=e.Item.ItemIndex;
this.ShowAdmin();DropDownList ddb=dg.Items[e.Item.ItemIndex].FindControl("dpt_P_ID") as DropDownList;
//DropDownList ddb=e.Item.FindControl("dpt_P_ID") as DropDownList;//此方法不行的。
MyDrop.DataTextField="P_Explain";
MyDrop.DataValueField="P_ID";
MyDrop.DataSource=fde.ExecuteDataSet("ShowPopedom 1");
MyDrop.DataBind();
ddb.Items.FindByText((e.Item.FindControl("lb_PID") as Label).Text).Selected=true;
}
DataSet ds = CC.GetDataSet("select * from News where id='" + Request.QueryString["id"] + "'", "News");
DataRow[] row = ds.Tables["News"].Select();
foreach (DataRow rs in row)
{
//显示编辑的新闻类别名
this.txtNewsTitle.Text = rs["biaoti"].ToString();
//显示编辑的新闻内容
this.txtNewsContent.Text = rs["neirong"].ToString();
//显示编辑的新闻标题
this.labTitle.Text = rs["biaoti"].ToString();
//显示编辑的新闻类型
this.ddlNewsType.SelectedValue = rs["leibie"].ToString().Trim();//dropdownlist不绑定
} 数据源,而是设置其items属性, 以上代码在下面情况是可以的
<asp:DropDownList ID="ddlNewsType" runat="server" Width="116px" CssClass="css"
Height="24px">
<asp:ListItem>国内新闻</asp:ListItem>
<asp:ListItem>国际新闻</asp:ListItem>
</asp:DropDownList> 以上代码在下面情况是不可以的
<asp:DropDownList ID="ddlNewsType" runat="server" Width="116px" CssClass="css"
Height="24px" DataSourceID="SqlDataSource1" DataTextField="新闻类别"
DataValueField="id">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DataConnectionString %>"
SelectCommand="SELECT [id], [新闻类别] FROM [新闻类别]" ></asp:SqlDataSource>
DROPDOWNLIST.SelectedValue=从数据库查询出来的相应值。
if(item!=null)
ddb.Items.SelectedIndex=ddb.Items.IndexOf(item);