Question

How can I configure a ASP.NET GridView to populate it by an Object DataSource where the object method needs a noticeCode to pass by the C# code?

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;
        }
    }

alt text

Was it helpful?

Solution

You will have to bind the value of std.SessionCode to the ObjectDataSource in code-behind. In the "Configure Data Source" screen above, choose None for now. You'll be binding it dynamically.

If your ObjectDataSource looks like this:

<asp:ObjectDataSource ID="myObjDS" runat="server"
  TypeName="YourNamespace.Notice"
  SelectMethod="GetNoticesBySessionCode">
  <SelectParameters>
    <asp:Parameter Name="sessionCode" Type="String"/>
  </SelectParameters>
</asp:ObjectDataSource>

Then

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.
}

OTHER TIPS

The above answer has it pretty much well covered. Just to add, you can also add parameters in the onselecting event of the objectdatasource.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.selecting.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top