asp.net gridview 및 객체 데이터 소스
-
12-09-2019 - |
문제
객체 메소드가 C# 코드를 통과하기 위해 alicecode가 필요한 객체 데이터 소스로 Asp.net GridView를 채우도록 구성하려면 어떻게해야합니까?
StudentControlpanel.aspx.cs
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()...*/
}
통지 .CS
public class Notice
{
public static List<Notice> GetNoticesBySessionCode(string sessionCode)
{
List<Notice> notices = null;
/* EcecuteReader()..... */
return notices;
}
}
해결책
당신은의 가치를 바인딩해야합니다 std.SessionCode
Code-Behind의 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.
}
다른 팁
위의 답변은 거의 잘 덮여 있습니다. 추가하려면 ObjectDatasource의 OnSelecting 이벤트에서 매개 변수를 추가 할 수도 있습니다.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.selecting.aspx
제휴하지 않습니다 StackOverflow