当用户选择记录时,建议使用哪种方法来填充Web窗体上的所有控件?

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

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

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

 

然后在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;