Which is the recommended way to fill all controls on a Web Form when user selects a record?

后端 未结 3 1837
既然无缘
既然无缘 2021-01-17 17:39

I have a GridView control which shows a list of all employees. When user selects any employee from this list, the record is shown on a Web Form with all input controls pre-f

3条回答
  •  走了就别回头了
    2021-01-17 18:17

    First you add the select button on your GridView as:

    
    

    then you add the OnRowCommand="RowCommand" property on GridView to call this function when the button is clicked and on code behind the function:

    protected void RowCommand(object sender, GridViewCommandEventArgs e)
    {
        // if the ViewMe command is fired
        if (e.CommandName == "ViewMe")
        {
            // go to find the index on the grid view
            int iTheIndexNow;
            if (int.TryParse(e.CommandArgument.ToString(), out iTheIndexNow))
            {
                // Set and highlight the selected
                TheGridView.SelectedIndex = iTheIndexNow;
    
                // do we have the table data id ?
                if (TheGridView.SelectedValue != null)
                {
                    // now load the controls data using this id
                    LoadValuesFromDatabaseToControls(TheGridView.SelectedValue);
                }    
            }
        }
    }
    

    I prefer this way of command button because you can add more commands than the select, or edit, even the delete or copy... the just index change can be done for any reason (eg by changing page) and is also need again the select.

    I use the subsonic 2 DAL for loading the data from the database. A sample code from my programs is:

        void LoadValuesFromDatabaseToControls(string editID)
        {
            // Load it from database
            AthUserMaiListName OneRow = new AthUserMaiListName(editID);
    
            if (OneRow.IsNotExist)
            {
                // a custom control that show messages on top.
                uResult.addMsg("Fail to load id " + editID, MsgType.error);
                // close the view of the controls
                dbViewPanel.Visible = false;
            }
            else // else we have the data and go for show them
            {
              // show this panel that contains the controls.
              dbViewPanel.Visible = true;
    
              // I keep my current edit id
              lblID.Text = editID;
    
              // just be sure that the select exist on DrowDownList
              MyUtils.SelectDropDownList(ddlEventType, OneRow.CAddedFrom);
    
              txtEmail.Text = OneRow.CEmail;
              txtFirstName.Text = OneRow.CFirstName;
              txtLastName.Text = OneRow.CLastName;
              txtInsideTitle.Text = OneRow.CInsideTitle;
              txtCompanyName.Text = OneRow.CCompanyName;        
    
              txtCreated.Text = DateTimeRender(OneRow.CreatedOn);
              txtModified.Text = DateTimeRender(OneRow.ModifiedOn);
            }
       }
    

提交回复
热议问题