10

我有一个显示所有员工列表的 GridView 控件。当用户从此列表中选择任何员工时,记录将显示在 Web 表单上,所有输入控件都预先填充了值。

我想知道任何好的方法来做到这一点。我应该将所有输入控件绑定到任何 SqlDataSource,还是应该通过从 DataSet 中选取值来重新填充所有输入控件。

4

3 回答 3

7

首先,您将 GridView 上的选择按钮添加为:

<asp:ButtonField Text="Select" CommandName="ViewMe" ButtonType="Button" />

然后添加OnRowCommand="RowCommand"属性GridView以在单击按钮时调用此函数以及函数后面的代码:

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

我更喜欢这种命令按钮方式,因为您可以添加比选择更多的命令,或编辑,甚至删除或复制......可以出于任何原因(例如通过更改页面)完成索引更改,并且还需要再次选择。

我使用 subsonic 2 DAL 从数据库加载数据。我的程序中的示例代码是:

    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);
        }
   }
于 2012-07-06T08:48:56.780 回答
2

我在我的应用程序中使用了这段代码-

更好的方法是调用这个gridview_select_index_change()事件方法

 private void PickValues(int SerialNum) 
{ 
    DataSet ds = new DataSet(); 
    try 
    { 
        string Query = "SELECT * FROM tw_main WHERE sno = " + SerialNum; 
        ds = reuse.ReturnDataSet(Query, "Scheme"); 

        //Add Scheme Code to new Session Variable 
        Session.Add("SerialNumber", ds.Tables[0].Rows[0]["sno"].ToString()); 
        //Set Update Flag 
        TaskFlag = "UPDATE"; 

        //Fill values of selected record on the Entry Form 
        if (ds.Tables[0].Rows[0]["schm_code"].ToString().Length > 0) 
            lblSchemeCode.Text = ds.Tables[0].Rows[0]["schm_code"].ToString(); 

        ddlType.SelectedValue = ds.Tables[0].Rows[0]["schemetype"].ToString(); ddlDistrict.Text = ds.Tables[0].Rows[0]["dist_nm"].ToString(); ddlBlock.Text = ds.Tables[0].Rows[0]["block_nm"].ToString(); 
        txtSchemeName.Text = ds.Tables[0].Rows[0]["schm_nm"].ToString(); 
        txtPopulation2001.Text = ds.Tables[0].Rows[0]["population_2001"].ToString(); 
        txtSupplySource.Text = ds.Tables[0].Rows[0]["supply_source"].ToString(); 
        txtApprovalYear.Text = ds.Tables[0].Rows[0]["yr_approval"].ToString(); 
        txtApprovalLetterNum.Text = ds.Tables[0].Rows[0]["approval_letter_num"].ToString(); 
        txtApprovalAmount.Text = ds.Tables[0].Rows[0]["sch_apr_amt"].ToString(); 
        txtWaitedLetterNum.Text = ds.Tables[0].Rows[0]["sch_waited_details"].ToString(); 
        txtSchTransferLetterNum.Text = ds.Tables[0].Rows[0]["schm_trans_details"].ToString(); 
        txtSchTransferDate.Text = ds.Tables[0].Rows[0]["schm_trans_date"].ToString(); 
        txtOtherRemarks.Text = ds.Tables[0].Rows[0]["remarks"].ToString(); 
    } 
    catch (Exception ex) 
    { 
        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "Warning", "alert('" + ex.Message.ToString() + "');",true); 
    } 
    finally 
    { 
        ds.Dispose(); 
        gridSerialNo = 0; 
    } 
}

编辑

这样做可能有更好的方法,但这肯定很好。

于 2012-07-06T08:48:17.687 回答
2

由于您要创建数据访问层,因此我执行此任务的方式是创建一个具有所有属性的类

班级:

public class tw_main
{
     public string SchemeCode {get;set;}
}

达尔:

public class dal
{
  public tw_main getSelectedValue(pass the parameters required by the method)
  {
    //your connection and query code
    var twmain = new tw_main();
    twmain.SchemaCode =  ds.Tables[0].Rows[0]["schm_code"].ToString(); 

    return twmain;
  }
}

网页:

//depending upon where you add this a reference may need to be imported (using) to the namespace
  var dalObj = new dal();
  var tw = dalObj.getSelectedValue();
lblSchemeCode.Text = tw.SchemaCode;
于 2012-07-18T22:46:13.790 回答