سؤال

كيف يمكنني تكوين GridView ASP.NET لملء استخدامه بواسطة مصدر بيانات كائن حيث تحتاج طريقة الكائن إلى NoticeEcode لتمرير رمز 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()...*/
    }

إشعار

public class Notice
{
public static List<Notice> GetNoticesBySessionCode(string sessionCode)
        {
            List<Notice> notices = null;

            /*      EcecuteReader().....         */

            return notices;
        }
    }

alt text

هل كانت مفيدة؟

المحلول

سوف تضطر إلى ربط قيمة 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.
}

نصائح أخرى

الإجابة المذكورة أعلاه مغطاة بشكل جيد للغاية. فقط لإضافة، يمكنك أيضا إضافة معلمات في حدث OnSelecting of the ObjectDatasource.

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top