日期:2014-05-18  浏览次数:20878 次

GridView中Selectcommand的参数如何赋值
repeater控件中加了<asp:label runat="Server" Text='<%# Eval("DeptName") %>'></asp>控件,然后又加了<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="true" DataSourceID="Accessdatasource_member" OnRowDataBound="Gridview_onRowDataBound">
</asp:GridView>控件,
我在 SelectCommand="SELECT [DeptName],[LevelName],[RoleName] from ([tbDept] left join [tbLevel] on [tbDept.LevelID]=[tbLevel.LevelID]) left join [tbRole] on [tbDept.RoleID]=[tbRole.RoleID] where [DeptName]IN(SELECT [DeptName] from [tbDept] where [ParentID] in(SELECT [ID] from [tbDept] where [b][DeptName]= ?[/b]))">用了一个叫DeptName的参数

我的问题是如何在Gridview_onRowDataBound()方法写代码把label中DeptName的值传递给GridView中的参数[b][DeptName]= ?[/b]?

------解决方案--------------------
在repeater_ItemDataBound先把Label1值获取出来,再在Gridview_onRowDataBound()使用
string deptname=((Label)e.Item.FindControl("Label1")).Text;

------解决方案--------------------
你应该在

Repeater1_OnDataBound 里面
 String DeptName = (e.Item.FindControl("Label1") as Label).Text;
 //或者
 String DeptName = DataBinder.Eval(e.Row.DataItem,"DeptName").ToString();

 得到DeptName 的值,然后

 
Gridview_onRowDataBound()方法里面

if(e.Row.RowType == DataControlRowType.DataRow)
{
 xx.SelectCommand = "SELECT [DeptName],[LevelName],[RoleName] from ([tbDept] left join [tbLevel] on [tbDept.LevelID]=[tbLevel.LevelID]) left join [tbRole] on [tbDept.RoleID]=[tbRole.RoleID] where [DeptName]IN(SELECT [DeptName] from [tbDept] where [ParentID] in(SELECT [ID] from [tbDept] where [DeptName]= '" + DeptName + "'" ;
}

或者

xx.SelectCommand = "SELECT [DeptName],[LevelName],[RoleName] from ([tbDept] left join [tbLevel] on [tbDept.LevelID]=[tbLevel.LevelID]) left join [tbRole] on [tbDept.RoleID]=[tbRole.RoleID] where [DeptName]IN(SELECT [DeptName] from [tbDept] where [ParentID] in(SELECT [ID] from [tbDept] where [DeptName]= @DeptName";
xx.Parameters.Clear();
xx.SelectParameters.Add("@DeptName",DeptName);

------解决方案--------------------
探讨
嗯 搞定没有发生异常 但我所要的结果没有显示出来~~

rotected void Repeater_OnDataBound(object sender,RepeaterItemEventArgs e)
{
// DeptName = ((Label)e.Item.FindControl("label_deptName")).Text;


Label lbl = (La……