如何配置ASP.NET GridView以通过Object DataSource填充它,其中object方法需要一个noticeCode来传递C#代码?
protected void Page_Load(object sender, EventArgs e)
{
string username = (string)Request.QueryString["username"];
Student std = Student.GetStudentByUsername(username);
if (std != null)
{
labName.Text = std.StudentName;
labUsername.Text = username;
labRollNo.Text = std.RollNo;
labRegNo.Text = std.RegNo;
Dept dpt = std.Department;
if (dpt != null)
{
labDepartment.Text = dpt.DeptName;
}
else
{
labDepartment.Text = "?";
}
}
/* Student-class has a SessionCode-property */
/* I need to pass this to Notice.GetNoticesBySessionCode()...*/
}
public class Notice
{
public static List<Notice> GetNoticesBySessionCode(string sessionCode)
{
List<Notice> notices = null;
/* EcecuteReader()..... */
return notices;
}
}
答案 0 :(得分:0)
您必须将std.SessionCode
的值绑定到代码隐藏中的ObjectDataSource。在上面的“配置数据源”屏幕中,立即选择“无”。你将动态绑定它。
如果您的ObjectDataSource如下所示:
<asp:ObjectDataSource ID="myObjDS" runat="server"
TypeName="YourNamespace.Notice"
SelectMethod="GetNoticesBySessionCode">
<SelectParameters>
<asp:Parameter Name="sessionCode" Type="String"/>
</SelectParameters>
</asp:ObjectDataSource>
然后
if (std != null)
{
//all the other code you have above.
myObjDS.SelectParameters["sessionCode"].DefaultValue = std.SessionCode;
myObjDS.DataBind(); //ensure you actually need this, may cause a double roundtrip to your DB.
}
答案 1 :(得分:0)
上面的答案非常清楚。只需添加,您还可以在objectdatasource的onselecting事件中添加参数。
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.selecting.aspx