问题描述
我有一个GridView控件,显示所有员工的列表。当用户选择从该列表中的任何员工,记录了所有输入Web窗体上显示控制pre-充满值。
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-filled with the values.
我想知道有什么好办法做到这一点。我应该绑定所有输入控件任何的SqlDataSource或从DataSet采摘值,我应该重新填充所有输入控件。
I want to know any good approach to do this. Should I bind all input controls to any SqlDataSource or should I re-populate all input controls by picking values from the DataSet.
推荐答案
首先,添加上你的GridView的选择按钮为:
First you add the select button on your GridView as:
<asp:ButtonField Text="Select" CommandName="ViewMe" ButtonType="Button" />
那么添加 OnRowCommand =RowCommand
在 GridView控件属性
按钮被点击时要调用这个函数并在后面的功能code:
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);
}
}
}
}
我preFER命令按钮,因为你可以比选择,或编辑,甚至删除或复制......刚刚指数的变化可以以任何理由进行添加更多命令的这种方式(通过改变页面EG)而且还需要重新选择。
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.
我用亚音速2 DAL从数据库加载数据。从我的程序样本code是:
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);
}
}
这篇关于这是建议的方式来填补Web窗体上的所有控件,当用户选择一个记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!