Using listview datakeyname for listbox parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrobbins
    New Member
    • May 2009
    • 11

    Using listview datakeyname for listbox parameter

    I have managed to put a listbox in my listview and have gotten the box to appear but it is empty. I want to click on the Edit link and based on the datakeyname value pass the parameter to the objectdatasourc e to fill the listbox. I'm trying to use IEnumerable and am not getting it. Can someone help with an understandable example. Or another way to do it.

    I'm at a loss to what I am to return (see ??? ) and it doesn't like my (SqlInt32 PersonnelNumber ) telling me it can't convert to "SqlInt32? (in my EmployeeObject) " -
    Code:
    public IEnumerable<AuthorizationObjectClass> GetEmployeeNoAuthorizations(SqlInt32 PersonnelNumber)
            {
                DataSet ds;
                AuthorizationObjectClass auth = new AuthorizationObjectClass();
                try
                {
                    //EmployeeObjectClass Emp = new EmployeeObjectClass();
                    Emp.PersonnelNumber = PersonnelNumber;
                    ds = data.GetNotSelectedAuthorizations(Emp);
                    if (ds.Tables.Count > 0)
                    {
                        auth.AuthorizationID = Convert.ToInt32(ds.Tables[0].Rows[0]["AuthorizationID"]);
                        auth.AuthorizationDefinition = Convert.ToString(ds.Tables[0].Rows[0]["AuthorizationDescription"]);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} Exception caught.", e);
                }
                return  ???? ;
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    and it doesn't like my (SqlInt32 PersonnelNumber ) telling me it can't convert to "SqlInt32? (in my EmployeeObject) "
    I'm guessing the SqlInt32 is a single item from within the Employee object..

    If your return type is the SqlInt32, don't try to return the entire employee object: return just the ID number .... return myEmployeeObjec t.PersonnelNumb er;

    If you want to return the entire EmployeeObject, then change your return type from a SqlInt32 to an EmployeeObject type.... return myEmployeeObjec t;

    Comment

    • jrobbins
      New Member
      • May 2009
      • 11

      #3
      listbox in Listview

      Can someone please tell me how to get a listbox to populate inside a listview when the Edit button is clicked based on either a hidden field or the datakeynames

      Comment

      • jrobbins
        New Member
        • May 2009
        • 11

        #4
        obtained id value can't bind to datatable to listbox

        [QUOTE]I was able to retrieve my id field; but when I try to bind results to listbox (NOAUTH), my listbox is showing as null and won't allow the binding. Any ideas?

        QUOTE]
        Code:
          protected void lvSearchResults_ItemEditing(Object sender, ListViewEditEventArgs e)
            {
        
                string id = lvSearchResults.DataKeys[e.NewEditIndex].Value.ToString();
                
                ListViewItem item = lvSearchResults.Items[e.NewEditIndex];
               CoreDataManager lbNoAuth = new CoreDataManager();
        
                DataTable dt = lbNoAuth.GetNotSelectedAuthorizations(id);
        
                ListBox NoAuth = (ListBox)item.FindControl("lbNoAuthorizations");
        
                
                if (dt.Rows.Count > 0)
                {
                             
                    NoAuth.DataSource = dt;
                    NoAuth.DataTextField = dt.Columns[1].ColumnName;
                    NoAuth.DataValueField = dt.Columns[0].ColumnName;

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          I don't do a lot of this stuff, but...

          Code:
                  
          ListBox NoAuth = (ListBox)item.FindControl("lbNoAuthorizations");
          If the result of your FindControl is null (no matching controls found) then NoAuth would be null.

          Comment

          • jrobbins
            New Member
            • May 2009
            • 11

            #6
            passing datakey to objectsource to populate listbox inside a listview

            I finally figured this after lots of searching. I have a listbox inside my listview. I grab the datakey on the OnItemEditing event in the listview and then pass the objectdatasourc e the datakey as the default parameter.
            Code:
            protected void lvSearchResults_OnItemEditing(object sender, ListViewEditEventArgs e)
                {
                        ListViewItem currentItem = lvSearchResults.Items[e.NewEditIndex];
                    
                        string id = lvSearchResults.DataKeys[e.NewEditIndex].Value.ToString();
                        Response.Write("datakey:  " + id);
                        odsNotSelectedAuthorizations.SelectParameters["PersonnelNumber"].DefaultValue = id;
                        odsSelectedAuthorizations.SelectParameters["PersonnelNumber"].DefaultValue = id;
                }
            Last edited by jrobbins; May 6 '09, 04:53 PM. Reason: to make clearer

            Comment

            Working...