ASP.net GridView y fuente de datos de objetos
-
12-09-2019 - |
Pregunta
¿Cómo puedo configurar un ASP.NET GridView para poblarlo por una fuente de datos de objetos donde el método de objeto necesita un noticeCode que pasar por el código C #?
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;
}
}
Solución
Usted tendrá que unir el valor de std.SessionCode
a la ObjectDataSource de código subyacente. En la pantalla "Configurar origen de datos" más arriba, elija Ninguno por el momento. Se le enlazando de forma dinámica.
Si su ObjectDataSource se ve así:
<asp:ObjectDataSource ID="myObjDS" runat="server"
TypeName="YourNamespace.Notice"
SelectMethod="GetNoticesBySessionCode">
<SelectParameters>
<asp:Parameter Name="sessionCode" Type="String"/>
</SelectParameters>
</asp:ObjectDataSource>
Entonces
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.
}
Otros consejos
La respuesta anterior ha que más o menos bien cubierto. Sólo para añadir, también se puede añadir parámetros en caso OnSelecting del ObjectDataSource.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow