RKh*_*RKh 10 c# asp.net asp.net-4.0
我有一个GridView控件,显示所有员工的列表.当用户从此列表中选择任何员工时,该记录将显示在Web窗体上,所有输入控件都预先填充了这些值.
我想知道这样做的任何好方法.我应该将所有输入控件绑定到任何SqlDataSource,还是应该通过从DataSet中选择值来重新填充所有输入控件.
首先,将GridView上的选择按钮添加为:
<asp:ButtonField Text="Select" CommandName="ViewMe" ButtonType="Button" />
Run Code Online (Sandbox Code Playgroud)
然后添加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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢这种命令按钮的方式,因为你可以添加比选择,编辑,甚至删除或复制更多的命令......只是因为任何原因(例如通过更改页面)可以完成索引更改,并且还需要选择.
我使用亚音速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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
957 次 |
| 最近记录: |