在gridview中编辑,删除和更新 [英] edit,delete and update in gridview
本文介绍了在gridview中编辑,删除和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个gridview,我有编辑,删除和更新按钮。我可以使用他们工作的编辑和删除按钮但更新按钮不起作用当我点击更新按钮,页面刷新,不更新。请帮助我。我的代码在哪里...
hi , i have a gridview and i have edit, delete and update buttons.i can use edit and delete buttons they work but update button doesn't work when i click on update button ,the page refreshes ,doesn't update.please help me.here is my code...
<asp:GridView ID="GridviewTeams"
runat="server"
AutoGenerateColumns="false"
OnRowUpdating="GV_RowUpdating"
OnRowDeleting="GV_RowDeleting"
OnRowEditing="GV_RowEditing"
OnRowCancelingEdit="GV_RowCancelingEdit">
<Columns>
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<asp:TextBox runat="server" ID="tbx_ID" >
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="GV_lblTeamID" Text='<%# Eval("teamID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Team Name">
<EditItemTemplate>
<asp:TextBox runat="server" ID="tbxEditTeamName" ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblEditTeamName" Text='<%# Eval("team_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Team email">
<EditItemTemplate>
<asp:TextBox runat="server" ID="tbxEditTeamEmail" ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lbl_edit_stadium_adress" Text='<%# Eval("team_email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Commands" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LnkBtnUpdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LnkBtnCancel" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LnkBtnEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LnkBtnDelete" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码落后......
code behind...
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
public void Bind()
{
try
{
CTeam cTeam = new CTeam();
GridviewTeams.DataSource = cTeam.GetAllTeams();
GridviewTeams.DataBind();
}
catch (Exception err)
{
Response.Write(err.Message);
}
}
protected void BtnAdd_Click(object sender, EventArgs e)
{
CTeam cteam = new CTeam();
cteam.AddTeams(tbxTeamName.Text,tbxTeamEmail.Text);
Bind();
}
protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
CTeam cteam = new CTeam();
int indexrow = e.RowIndex;
short tid = short.Parse(((Label)GridviewTeams.Rows[indexrow].FindControl("GV_lblTeamID")).Text);
cteam.DeleteTeams(tid);
Bind();
}
protected void GV_RowEditing(object sender, GridViewEditEventArgs e)
{
GridviewTeams.EditIndex = e.NewEditIndex;
Bind();
}
protected void GV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridviewTeams.EditIndex = -1;
Bind();
}
protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
CTeam cteam = new CTeam();
int indexrow = e.RowIndex;
string tname = ((TextBox)GridviewTeams.Rows[e.RowIndex].FindControl("tbxEditTeamName")).Text;
string temail = ((TextBox)GridviewTeams.Rows[e.RowIndex].FindControl("tbxEditTeamEmail")).Text;
cteam.UpdateTeams(tname, temail);
GridviewTeams.EditIndex = -1;
Bind();
}
}
推荐答案
嗨...
使用asp.net在gridview中插入,编辑,更新和删除数据。
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html [ ^ ]
谢谢你。
Hi...
For insert,edit,update and delete data in gridview using asp.net.
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]
Thank u.
亲爱的你的代码是完美的但是你用你的代码犯了一些小错误首先只是添加Text ='<%#Bind( teamID)%>'到EditItemTemplate内的每个文本框,因为当你点击编辑时,现有文字会显示在文本框上。
第二个是你必须添加找到控制语句如下:
TextBox tname =(TextBox)GridviewTeams.Rows [e.RowIndex] .FindControl(tbx EditTeamName);
TextBox temail =(TextBox)GridviewTeams.Rows [e.RowIndex] .FindControl(tbxEditTeamEmail);
我希望它对你有帮助.......: - )
Dear your code is perfect but you make some small mistakes over their with your code first one is firstly just add Text='<%# Bind("teamID") %>' to every textbox inside of EditItemTemplate because when you click on edit existing text will display on textbox.
Second one is you have to add find control statement as like:
TextBox tname = (TextBox)GridviewTeams.Rows[e.RowIndex].FindControl("tbxEditTeamName");
TextBox temail = (TextBox)GridviewTeams.Rows[e.RowIndex].FindControl("tbxEditTeamEmail");
I hope it's helping to you.......:-)
这篇关于在gridview中编辑,删除和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文