如何配置ASP.NET的GridView由一个DataSource对象,其中对象的方法需要一个noticeCode由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;
        }
    }

“替代文字”

有帮助吗?

解决方案

您将不得不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.
}

其他提示

以上回答有它几乎很好地覆盖。我想补充,你还可以在ObjectDataSource控件的onselecting事件添加参数。

HTTP:// MSDN .microsoft.com / EN-US /库/ system.web.ui.webcontrols.objectdatasource.selecting.aspx

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top