将“查看”和“删除”按钮添加到gridview [英] Add View and Delete button to gridview

查看:98
本文介绍了将“查看”和“删除”按钮添加到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,其中我通过sql命令绑定数据。

aspx页面:

< asp:GridView ID =gv_adsrunat =server> 
< / asp:GridView>



aspx.cs:

  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
受保护 void BindGridview()
{
var o =会话[ 通告];
if (o!= null
{
ses = o.ToString();
}

SqlCommand cmd = new SqlCommand( sps_myads,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ email,ses);
尝试
{
con.Open();
gv_ads.EmptyDataText = 找不到记录;
gv_ads.DataSource = cmd.ExecuteReader();
gv_ads.DataBind();
con.Close();
}
catch (例外情况)
{
throw ex;
}
}



存储过程:

  ALTER   PROCEDURE  [dbo]。[sps_myads] 
@ email nvarchar 100
AS
BEGIN
选择 CONVERT VARCHAR 10 ),registertime, 104 as Register_On,r.ad_id,c.category_name,sc.subcategory_name,title from dbo.tbl_adregister r INNER JOIN tbl_category c ON r.category = c.category_id INNER JOIN tbl_subcategory sc on r.subcategory = sc.subcategory_id
其中 useremail = @ email
END





根据我的查询显示结果,我的问题是我的结果右侧每行需要两个按钮:一个是View,另一个是Delete。

查看按钮:当我点击查看时按钮它将重定向到其他页面,其中ad_id作为参数,以便我可以显示一个特定点击行ad_id的完整详细信息。

删除按钮:当我点击特定行的删除按钮时应该要求确认对话框删除,点击确定后会删除gridview中的特定行数据。



请帮助程序员。

解决方案

这可能对你有帮助。



HTTP:/ /forums.asp.net/t/1723602.aspx?What+is+Item+Template+ [ ^ ]


试试这个:

For View按钮:

 <   asp:templatefield     headertext   =  No Of Seats    xmlns:asp   = #unknown >  
< itemtemplate >
< asp:linkbutton id = LinkBut​​ton1 runat = 服务器

postbackurl = <% #Eval( id,< span class =code-string> Yourpage.aspx?id = {0})%> forecolor = 红色 >
转到。< ; / asp:linkbutton > >
< / itemtemplate >
< / asp:templatefield >





删除按钮:

使用 RowCommand 活动



 <   asp:templatefield     headertext   = 删除    xmlns:asp   = #unknown >  
< itemtemplate >
< asp:imagebutton id = ImageButton3 runat = server >
ImageUrl =../ image / delete.pngCommandName =DeleteOnClientClick = 返回确认(您确定要删除此条目吗?); />

< / asp:imagebutton > < / itemtemplate >
< / asp:templatefield >





In .cs页面:

  protected   void  GridView1_RowCommand( object  sender,GridViewCommandEventArgs e)
{
try
{

if (e.CommandName == 删除
{
ImageButton img =(ImageButton)e.CommandSo urce as ImageButton;
GridViewRow row = img.NamingContainer as GridViewRow;

标签lbid =(标签)row.FindControl( label1) ; // idlabel
bid = Convert.ToInt32(lbid.Text);

// 传递出价以删除

}



}
catch (例外情况)
{
Page.ClientScript.RegisterClientScriptBlock( typeof (页面), 脚本 alert('无法删除,因为它在其他页面中使用!!!' ); true );
}

}





你可以在rowcommand中实现查看过程


参考:

1. 如何更新 - 删除 - 插入在GridView-in-csharpnet中 [ ^ ]

2. 演练:创建主/详细网页Visual Studio [ ^ ]

I have one gridview in which i bind data through sql command.
aspx page:

<asp:GridView ID="gv_ads" runat="server" >  
</asp:GridView>


aspx.cs:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
protected void BindGridview()
    {
        var o = Session["advertise"];
        if (o != null)
        {
            ses = o.ToString();
        }

        SqlCommand cmd = new SqlCommand("sps_myads", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@email", ses);
        try
        {
            con.Open();
            gv_ads.EmptyDataText = "No Records Found";
            gv_ads.DataSource = cmd.ExecuteReader();
            gv_ads.DataBind();
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


stored procedure:

ALTER PROCEDURE [dbo].[sps_myads] 
	@email nvarchar(100)
AS
BEGIN
	select CONVERT(VARCHAR(10),registertime ,104) as Register_On, r.ad_id, c.category_name, sc.subcategory_name, title from dbo.tbl_adregister r INNER JOIN tbl_category c ON r.category = c.category_id INNER JOIN tbl_subcategory sc on r.subcategory = sc.subcategory_id
	where useremail=@email
END



It showing me result according to my query, my problem is i need two buttons for every row at right side of my result: one is View and other is Delete.
View button: when i click on view button it will redirect to other page with "ad_id" as parameter so that i can display complete details of one particular clicked row "ad_id".
Delete button: when i click on delete button of particular row it should ask a confirmation dialog to delete and after click ok it will delete particular row data from gridview.

Please help programmers.

解决方案

this might help you.

http://forums.asp.net/t/1723602.aspx?What+is+Item+Template+[^]


Try this:
For View button:

<asp:templatefield headertext="No Of Seats" xmlns:asp="#unknown">
<itemtemplate>
<asp:linkbutton id="LinkButton1" runat="server"

 postbackurl="<%#Eval("id","Yourpage.aspx?id={0}") %>" forecolor="Red"> 
                                       Go to.</asp:linkbutton> >
</itemtemplate>
</asp:templatefield>



For Delete Button:
Use RowCommand Event

<asp:templatefield headertext="Delete" xmlns:asp="#unknown">
            <itemtemplate>
            <asp:imagebutton id="ImageButton3" runat="server">
             ImageUrl ="../image/delete.png" CommandName ="Delete" OnClientClick="return confirm("Are you sure you want to delete this entry?");"  />
            
            </asp:imagebutton></itemtemplate>
            </asp:templatefield>



In .cs page:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  {
      try
      {

          if (e.CommandName == "Delete")
          {
              ImageButton img = (ImageButton)e.CommandSource as ImageButton;
              GridViewRow row = img.NamingContainer as GridViewRow;

              Label lbid = (Label)row.FindControl("label1");//idlabel
              bid = Convert.ToInt32(lbid.Text);

            // pass the bid to delete

          }



      }
      catch (Exception ex)
      {
          Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Cannot be Deleted because it is used in other Pages!!!');", true);
      }

  }



you can achieve view process in rowcommand also


Refer:
1. How-to-Update-Delete-Insert-in-GridView-in-csharpnet[^]
2. Walkthrough: Creating Master/Detail Web Pages in Visual Studio[^]


这篇关于将“查看”和“删除”按钮添加到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆