本例中实现单击更新修改图标(imagebutton)时取得相应行的数据并填充到下面的panel容器中,这时panel的Visible为true,修改相应数据单击更新后隐藏下面的panel容器.
更新操作如下图所示:
步骤如下:
1.创建panel如图所示,具体就不用说了吧,需要在Page_Load事件中添加如下代码
设置panel的可见性为false
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
BindDG();
this.Panel1.Visible=false;
}
}
2.在datagrid中创建模板列private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
BindDG();
this.Panel1.Visible=false;
}
}
3.在模板列中加入imagebutton并为其指定ImageUrl和CommandName,代码如下:
更新.aspx
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="Imagebutton2" runat="server" CommandName="UpDt" ImageUrl="../Pic/edit2.gif"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
4.在datagrid的ItemCommand事件中添加如下代码<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="Imagebutton2" runat="server" CommandName="UpDt" ImageUrl="../Pic/edit2.gif"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
单击修改图标时触发
private void DG_userinfo_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="Del")
{
string uid = e.Item.Cells[0].Text.ToString();
string [] values = {uid};
OperateXmlByDataSet.DeleteXmlRows(ViewState["filename"].ToString(),"userid",values);
BindDG();
}
if(e.CommandName=="UpDt")
{
string userid = e.Item.Cells[0].Text.ToString();
ViewState["uid"] = userid;
this.Txt_id.Text = userid;
this.Txt_name.Text = e.Item.Cells[1].Text.ToString();
this.Txt_pass.Text = e.Item.Cells[2].Text.ToString();
this.Txt_mail.Text = e.Item.Cells[3].Text.ToString();
this.Txt_site.Text = e.Item.Cells[4].Text.ToString();
this.Txt_msn.Text = e.Item.Cells[5].Text.ToString();
this.Panel1.Visible=true;
}
}
private void DG_userinfo_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="Del")
{
string uid = e.Item.Cells[0].Text.ToString();
string [] values = {uid};
OperateXmlByDataSet.DeleteXmlRows(ViewState["filename"].ToString(),"userid",values);
BindDG();
}
if(e.CommandName=="UpDt")
{
string userid = e.Item.Cells[0].Text.ToString();
ViewState["uid"] = userid;
this.Txt_id.Text = userid;
this.Txt_name.Text = e.Item.Cells[1].Text.ToString();
this.Txt_pass.Text = e.Item.Cells[2].Text.ToString();
this.Txt_mail.Text = e.Item.Cells[3].Text.ToString();
this.Txt_site.Text = e.Item.Cells[4].Text.ToString();
this.Txt_msn.Text = e.Item.Cells[5].Text.ToString();
this.Panel1.Visible=true;
}
}
5."更新"按钮事件,我是用xml存储数据的,由于涉及代码较多,这里只给出部分源码,有想要的请留言
更新事件
void Xml_UpDt(string userid)
{
string username = this.Txt_name.Text;
string userpass = this.Txt_pass.Text;
string usermail = this.Txt_mail.Text;
string usersite = this.Txt_site.Text;
string usermsn = this.Txt_msn.Text;
string [] Columns = {"username","userpass","usermail","usersite","usermsn"};
string [] ColumnsValues = {username,userpass,usermail,usersite,usermsn};
OperateXmlByDataSet.UpdateXmlRow(ViewState["filename"].ToString(),Columns,ColumnsValues,"userid",userid);
}
private void Btn_update_Click(object sender, System.EventArgs e)
{
Xml_UpDt(ViewState["uid"].ToString());
BindDG();
this.Panel1.Visible=false;
}
void Xml_UpDt(string userid)
{
string username = this.Txt_name.Text;
string userpass = this.Txt_pass.Text;
string usermail = this.Txt_mail.Text;
string usersite = this.Txt_site.Text;
string usermsn = this.Txt_msn.Text;
string [] Columns = {"username","userpass","usermail","usersite","usermsn"};
string [] ColumnsValues = {username,userpass,usermail,usersite,usermsn};
OperateXmlByDataSet.UpdateXmlRow(ViewState["filename"].ToString(),Columns,ColumnsValues,"userid",userid);
}
private void Btn_update_Click(object sender, System.EventArgs e)
{
Xml_UpDt(ViewState["uid"].ToString());
BindDG();
this.Panel1.Visible=false;
}