ASP.net GridViewコントロールとオブジェクトのデータソース
-
12-09-2019 - |
質問
どのようにして、オブジェクトのメソッドは、C#コードで渡すnoticeCodeを必要とするオブジェクトデータソースでそれを埋めるために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()...*/
}
Notice.cs
public class Notice
{
public static List<Notice> GetNoticesBySessionCode(string sessionCode)
{
List<Notice> notices = null;
/* EcecuteReader()..... */
return notices;
}
}
解決
あなたはコードビハインドでのObjectDataSourceにstd.SessionCode
の値をバインドする必要があります。上記の「データソースの構成」画面では、今のところなし]を選択します。あなたはそれを動的に結合することがあります。
あなたの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イベントでパラメータを追加することができます。
所属していません StackOverflow