在GridView中的EditItemTemplate放入一DropDownList,DropDownList通过方法绑定数据,请教DropDownList如何实现默认所在的数据值?
解决方案 »
- 提个问题?关于用户id
- WIN7下 应用System.DirectoryServices.DirectoryEntry问题
- 问个郁闷的问题!TextBox怎么超过了TD的范围!
- 请各们高手帮帮忙
- VisualStudio2005 生成的文件是UTF-8编码的,能不能更改成GB2312?
- ------------------高手请进,十分着急------------------
- 页面2是从页面1转过来的,,,,那我在页面2中怎么取得页面1?
- guwei4037版主麻烦看下昨天回答过Http接口的问题
- SQL查询语句的问题!!!!(新手学习中)
- 连接远程数据库这2个哪个速度快呢?
- ajax问题 浏览器问题
- VS2005中使用TreeView的几个小问题,马上给分
public DataSet RoleDownList()
{
OleDbConnection conn=DataOption.Connstring();
OleDbDataAdapter Adapter = new OleDbDataAdapter("select * from Role",conn);
DataSet ds = new DataSet();
Adapter.Fill(ds,"Role");
return ds;
DataOption.Dispose();
}
Role表有N项,也就是DropDownList有N项了,其中编辑某条记录时(记录源select Uid,Name,Password,Rname from [User] inner join [Role] on Role.Rid=User.RoleId),那么我想上面记录DropDownList默认User的Rname 啊
<asp:TemplateField HeaderText="角色">
<EditItemTemplate>
<asp:DropDownList ID="DownListRole" runat="server" DataSource="<%# RoleDownList() %>"
DataTextField="Rname" DataValueField="Rid">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblRoleId" runat="server" Text='<%# Eval("Rname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>后台:
public partial class UserManager : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["name"].Value == "" || Request.Cookies["name"].Value == null)
{
Response.Redirect("default.aspx");
} } public DataSet RoleDownList()
{
OleDbConnection conn=DataOption.Connstring();
OleDbDataAdapter Adapter = new OleDbDataAdapter("select * from Role",conn);
DataSet ds = new DataSet();
Adapter.Fill(ds,"Role");
return ds;
DataOption.Dispose();
} protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#FFFFCC';");
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c;");
LinkButton Del = (LinkButton)e.Row.FindControl("LinkButton2");
if (Del.Text == "删除")
{
Del.OnClientClick = "return confirm('执行后不可还原,确实要删除吗')";
}
}
} protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string Name = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Name"));
if (Name== Request.Cookies["name"].Value)
{
LinkButton Del = (LinkButton)e.Row.FindControl("LinkButton2");
if (Del.Text == "删除")
{ Del.Visible = false; }
}
} } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
}
我是个新手,希望指点
DataTextField="Rname" DataValueField="Rid">
这样写就对啊,你现在显示的是哪个字段?
Role是角色表,User用户表!通过Role.Rid=User.RoleId关系可知编辑所要的默认值,但我不知如可实现!我试写过好几天,但没成功!请教罗
我去试试
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == GridView1.EditIndex)
{ DataRowView rowItem = (DataRowView)e.Row.DataItem; DropDownList clsName = (DropDownList)e.Row.FindControl("uClassName");
if (rowItem["ClassName"] != DBNull.Value)
{
clsName.Items.FindByText(rowItem["ClassName"].ToString()).Selected = true;
} }
}
}
<asp:DropDownList ID="uClassName" runat="server">
....
</asp:DropDownList>
</EditItemTemplate>
1: .cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
int rid = Convert.ToInt32(((DataRowView)e.Row.DataItem)["Rid"]);
DropDownList dd = (DropDownList)e.Row.FindControl("DropDownList2");
DataTable table = dd.DataSource as DataTable;
dd.SelectedIndex = table.Rows.IndexOf(table.Rows.Find(rid));
}
}
public DataTable GridSource
{
get
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Rid", typeof(int));
for (int i = 1; i < 3; i++)
{
DataRow row = table.NewRow();
row["ID"] = i;
row["Rid"] = i;
table.Rows.Add(row);
}
return table;
}
}
public DataTable DropSource
{
get
{
DataTable table = new DataTable();
DataColumn col=new DataColumn("Rid", typeof(int));
table.Columns.Add(col);
table.PrimaryKey = new DataColumn[] { col };
table.Columns.Add("Rname", typeof(string));
for (int i = 1; i < 3; i++)
{
DataRow row = table.NewRow();
row["Rid"] = i;
row["Rname"] = i+"aa";
table.Rows.Add(row);
}
return table;
}
}
2:页面
<asp:GridView ID="GridView1" DataSource="<%# GridSource %>" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" DataSource="<%#DropSource %>" DataTextField="Rname" DataValueField="Rid" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
谢谢如位
!这行DataRowView rowItem = (DataRowView)e.Row.DataItem;
法将类型为“System.Data.Common.DbDataRecord”的对象强制转换为类型“System.Data.DataRowView”。
???????
其它不变
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
DropDownList dd = (DropDownList)e.Row.FindControl("DropDownList2");
if(drv["Rid"]!=DBNull.Value)
dd.Items.FindByValue(((DataRowView)e.Row.DataItem)["Rid"].ToString()).Selected = true;
}
}
但他的程序是出了点问题:
if (e.Row.RowIndex == GridView1.EditIndex)
{
}
这一条件要去掉。为什么?
原因很空间:此时我们不是在编辑,而是在DataBind,所以这条件在这里就有BUG,如果放上去,永远都不可能处理掉,结果与前面的完全一样。你可以试一下,GridView1.EditIndex永远是-1。
结果:去掉这个条件就可以啦!祝你成功!