且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将“查看”和“删除”按钮添加到gridview

更新时间:2023-11-30 22:46:22

这可能对你有帮助。



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 Yourpage.aspx?id = {0})%> forecolor = 红色 >
转到。&lt ; / asp:linkbutton > >
< / itemtemplate >
< / asp:templatefield >





删除按钮:

使用 RowCommand 活动



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

< / 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[^]