当用户选择记录时,这是填充Web窗体上所有控件的推荐方式



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

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

首先在GridView上添加选择按钮作为:

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

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

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

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

我使用亚音速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);
        }
   }

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

更好的方法是在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; 
    } 
}

编辑

也许有更好的方法可以做到这一点,但这肯定很好。

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

类别:

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

DAL:

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;

相关内容

最新更新