ASP.NET 中GridView控件怎么修改添加数据? 5
3个回答
展开全部
我有个现成的完整实例:
//前台GridView
<asp:GridView ID="gv_friendlink" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="gv_friendlink_RowCancelingEdit"
onrowediting="gv_friendlink_RowEditing"
onrowupdating="gv_friendlink_RowUpdating" CellPadding="4"
ForeColor="#333333" GridLines="None" Font-Names="楷体_GB2312" Font-Size="Medium"
ShowFooter="True" onrowdeleting="gv_friendlink_RowDeleting" Width="758px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>网站名称</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Labelname" runat="server" style="margin-left:10px;" Width="240" Text='<%#Eval("Fname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" style="margin-left:10px;" Width ="200px" Text='<%#Eval("Fname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>网站完整的网址</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" style="margin-left:10px;" Width ="230px" NavigateUrl='<%#Eval("Furl") %>' Target="_blank"><%#Eval("Furl") %></asp:HyperLink>
<%--<asp:Label ID="Labelurl" runat="server" Text='<%#Eval("Furl") %>'></asp:Label>--%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txturl" runat="server" Text='<%#Eval("Furl") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>优先级</HeaderTemplate>
<ItemTemplate>
<center ><asp:Label ID="Labelpriority" runat="server" Text='<%#Eval("FPriority") %>'></asp:Label></center>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpriority" runat="server" Text='<%#Eval("FPriority") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate> 编辑 </HeaderTemplate>
<ItemTemplate>
<center><asp:Button ID="edit" runat="server" Text="编辑" CommandName="edit" /></center>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="update" runat="server" Text="更新" CommandName="Update" />
<asp:Button ID="cancel" runat="server" Text="取消" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate> 删除 </HeaderTemplate>
<ItemTemplate>
<center><asp:Button ID="delete" runat="server" Text="删除" CommandName="delete" OnClientClick="return confirm('真的要删除吗?');" /></center>
</ItemTemplate>
<EditItemTemplate></EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Left" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="LightGray" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<br />
<asp:TextBox ID="txt_fname" runat="server" Width="265px"></asp:TextBox>
<asp:TextBox ID="txt_furl" runat="server" Width="225px"></asp:TextBox>
<asp:TextBox ID="txt_fpriority" runat="server" Width="85px"></asp:TextBox>
<asp:Button ID="btn_insert" runat="server" Text="添加链接" onclick="btn_insert_Click" />
//后台代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
((Label)Master.FindControl("lbTitle")).Text = "友情链接管理";
bind();
}
}
protected void bind()
{
System.Data.DataSet ds = new zdxk.BLL.friendlinks().GetAllList();
gv_friendlink.DataKeyNames = new string[] { "id" };
gv_friendlink.DataSource = ds;
gv_friendlink.DataBind();
}
protected void gv_friendlink_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv_friendlink.EditIndex = -1;
bind();
}
protected void gv_friendlink_RowEditing(object sender, GridViewEditEventArgs e)
{
gv_friendlink.EditIndex = e.NewEditIndex;
bind();
}
protected void gv_friendlink_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
zdxk.Model.friendlinks link = new zdxk.Model.friendlinks();
link.id = Convert.ToInt64(gv_friendlink.DataKeys[e.RowIndex].Value);
link.fname = ((TextBox)(gv_friendlink.Rows[e.RowIndex].FindControl("txtname"))).Text.ToString();
link.furl = ((TextBox)(gv_friendlink.Rows[e.RowIndex].FindControl("txturl"))).Text.ToString();
link.fpriority = Convert.ToInt32(((TextBox)(gv_friendlink.Rows[e.RowIndex].FindControl("txtpriority"))).Text);
if (link.fname == "" || link.furl == "" || link.fpriority <= 0)
{
MessageBox.Show(this, "优先级必须大于0,请填写完整信息!");
e.Cancel = true;
}
;
if (new zdxk.BLL.friendlinks().Update(link))
{
MessageBox.Show(this, "更新成功!");
gv_friendlink.EditIndex = -1;
bind();
}
else MessageBox.Show(this, "更新失败,请重试!");
}
catch (Exception)
{
MessageBox.Show(this,"请输入正确的内容!谢谢!"); e.Cancel = true;
}
}
protected void btn_insert_Click(object sender, EventArgs e)
{
if (txt_fname.Text == "" || txt_fpriority.Text == "" || txt_furl.Text == "")
{
MessageBox.Show(this, "请填写完整链接信息!"); return;
}
try
{
zdxk.Model.friendlinks link = new zdxk.Model.friendlinks();
link.fname = txt_fname.Text;
link.furl = txt_furl.Text;
link.fpriority = Convert.ToInt64(txt_fpriority.Text);
if (link.fpriority <= 0)
{
MessageBox.Show(this, "优先级必须大于0,请填写正确的数据!");
return;
}
if (new zdxk.BLL.friendlinks().Add(link) > 0)
{
txt_fname.Text = txt_fpriority.Text = txt_furl.Text = "";
MessageBox.Show(this, "添加成功!");
bind();
}
else
{
MessageBox.Show(this, "添加失败,请重试!");
}
}
catch
{
MessageBox.Show(this, "优先级必须大于0,请填写正确的数据!");
return;
}
}
protected void gv_friendlink_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
long Id = Convert.ToInt64(gv_friendlink.DataKeys[e.RowIndex].Value);
if (new zdxk.BLL.friendlinks().Delete(Id))
{
MessageBox.Show(this, "删除成功!");
bind();
}
else
{
MessageBox.Show(this, "删除失败,请重试!");
}
}
//前台GridView
<asp:GridView ID="gv_friendlink" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="gv_friendlink_RowCancelingEdit"
onrowediting="gv_friendlink_RowEditing"
onrowupdating="gv_friendlink_RowUpdating" CellPadding="4"
ForeColor="#333333" GridLines="None" Font-Names="楷体_GB2312" Font-Size="Medium"
ShowFooter="True" onrowdeleting="gv_friendlink_RowDeleting" Width="758px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>网站名称</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Labelname" runat="server" style="margin-left:10px;" Width="240" Text='<%#Eval("Fname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" style="margin-left:10px;" Width ="200px" Text='<%#Eval("Fname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>网站完整的网址</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" style="margin-left:10px;" Width ="230px" NavigateUrl='<%#Eval("Furl") %>' Target="_blank"><%#Eval("Furl") %></asp:HyperLink>
<%--<asp:Label ID="Labelurl" runat="server" Text='<%#Eval("Furl") %>'></asp:Label>--%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txturl" runat="server" Text='<%#Eval("Furl") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>优先级</HeaderTemplate>
<ItemTemplate>
<center ><asp:Label ID="Labelpriority" runat="server" Text='<%#Eval("FPriority") %>'></asp:Label></center>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpriority" runat="server" Text='<%#Eval("FPriority") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate> 编辑 </HeaderTemplate>
<ItemTemplate>
<center><asp:Button ID="edit" runat="server" Text="编辑" CommandName="edit" /></center>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="update" runat="server" Text="更新" CommandName="Update" />
<asp:Button ID="cancel" runat="server" Text="取消" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate> 删除 </HeaderTemplate>
<ItemTemplate>
<center><asp:Button ID="delete" runat="server" Text="删除" CommandName="delete" OnClientClick="return confirm('真的要删除吗?');" /></center>
</ItemTemplate>
<EditItemTemplate></EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Left" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="LightGray" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<br />
<asp:TextBox ID="txt_fname" runat="server" Width="265px"></asp:TextBox>
<asp:TextBox ID="txt_furl" runat="server" Width="225px"></asp:TextBox>
<asp:TextBox ID="txt_fpriority" runat="server" Width="85px"></asp:TextBox>
<asp:Button ID="btn_insert" runat="server" Text="添加链接" onclick="btn_insert_Click" />
//后台代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
((Label)Master.FindControl("lbTitle")).Text = "友情链接管理";
bind();
}
}
protected void bind()
{
System.Data.DataSet ds = new zdxk.BLL.friendlinks().GetAllList();
gv_friendlink.DataKeyNames = new string[] { "id" };
gv_friendlink.DataSource = ds;
gv_friendlink.DataBind();
}
protected void gv_friendlink_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv_friendlink.EditIndex = -1;
bind();
}
protected void gv_friendlink_RowEditing(object sender, GridViewEditEventArgs e)
{
gv_friendlink.EditIndex = e.NewEditIndex;
bind();
}
protected void gv_friendlink_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
zdxk.Model.friendlinks link = new zdxk.Model.friendlinks();
link.id = Convert.ToInt64(gv_friendlink.DataKeys[e.RowIndex].Value);
link.fname = ((TextBox)(gv_friendlink.Rows[e.RowIndex].FindControl("txtname"))).Text.ToString();
link.furl = ((TextBox)(gv_friendlink.Rows[e.RowIndex].FindControl("txturl"))).Text.ToString();
link.fpriority = Convert.ToInt32(((TextBox)(gv_friendlink.Rows[e.RowIndex].FindControl("txtpriority"))).Text);
if (link.fname == "" || link.furl == "" || link.fpriority <= 0)
{
MessageBox.Show(this, "优先级必须大于0,请填写完整信息!");
e.Cancel = true;
}
;
if (new zdxk.BLL.friendlinks().Update(link))
{
MessageBox.Show(this, "更新成功!");
gv_friendlink.EditIndex = -1;
bind();
}
else MessageBox.Show(this, "更新失败,请重试!");
}
catch (Exception)
{
MessageBox.Show(this,"请输入正确的内容!谢谢!"); e.Cancel = true;
}
}
protected void btn_insert_Click(object sender, EventArgs e)
{
if (txt_fname.Text == "" || txt_fpriority.Text == "" || txt_furl.Text == "")
{
MessageBox.Show(this, "请填写完整链接信息!"); return;
}
try
{
zdxk.Model.friendlinks link = new zdxk.Model.friendlinks();
link.fname = txt_fname.Text;
link.furl = txt_furl.Text;
link.fpriority = Convert.ToInt64(txt_fpriority.Text);
if (link.fpriority <= 0)
{
MessageBox.Show(this, "优先级必须大于0,请填写正确的数据!");
return;
}
if (new zdxk.BLL.friendlinks().Add(link) > 0)
{
txt_fname.Text = txt_fpriority.Text = txt_furl.Text = "";
MessageBox.Show(this, "添加成功!");
bind();
}
else
{
MessageBox.Show(this, "添加失败,请重试!");
}
}
catch
{
MessageBox.Show(this, "优先级必须大于0,请填写正确的数据!");
return;
}
}
protected void gv_friendlink_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
long Id = Convert.ToInt64(gv_friendlink.DataKeys[e.RowIndex].Value);
if (new zdxk.BLL.friendlinks().Delete(Id))
{
MessageBox.Show(this, "删除成功!");
bind();
}
else
{
MessageBox.Show(this, "删除失败,请重试!");
}
}
展开全部
你说的意思是 修改与更新2个 功能吗,
1、修改的话,如果你修改的数据复制,例如,在GRIDVIEW中没有显示的数据,那你需要另为做个修改页面,然后在GRIDVIEW_DATABIND(行绑定事件中,为每一列绑定哥超链接,例如,update.aspx?id=1;
2、如果修改的比较简单数据,在GRIDVIEW显示的数据,那你在页面中加入个dataDetial控件,里面有updateTempl模版和insertTempl模板,然后你可以弄个objcetdatasource数据源绑定控件,为gridView绑定selectMethod\updateMethod\insertMethod\方法 ,当然还要指定这些方法放在那个.cs后台代码的命名空间例如 myVSBLL.myObj,还有个参数就是绑定的数据类型是什么类型,也是绑定命名空间
1、修改的话,如果你修改的数据复制,例如,在GRIDVIEW中没有显示的数据,那你需要另为做个修改页面,然后在GRIDVIEW_DATABIND(行绑定事件中,为每一列绑定哥超链接,例如,update.aspx?id=1;
2、如果修改的比较简单数据,在GRIDVIEW显示的数据,那你在页面中加入个dataDetial控件,里面有updateTempl模版和insertTempl模板,然后你可以弄个objcetdatasource数据源绑定控件,为gridView绑定selectMethod\updateMethod\insertMethod\方法 ,当然还要指定这些方法放在那个.cs后台代码的命名空间例如 myVSBLL.myObj,还有个参数就是绑定的数据类型是什么类型,也是绑定命名空间
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
去网上搜下Gridview 72般绝技 看博客 会有收获的
这里大家贴代码 估计你复制过去也不能用 也不知道错在哪
这里大家贴代码 估计你复制过去也不能用 也不知道错在哪
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询