0

我试图从 gridview 的页脚触发 ImageButton 的事件单击,但我没有触发,我将不胜感激,在此先感谢,这是代码

protected void grvBubDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Footer)
    {
         ImageButton imgbtnSendMail = new ImageButton();
         //ImageButton imgEdit = new ImageButton();
         imgbtnSendMail.ImageUrl = "~/Images/mail01.png";
         imgbtnSendMail.AlternateText = "Edit";
         imgbtnSendMail.ToolTip = "for send mail press here";
         imgbtnSendMail.Width = imgbtnSendMail.Height = 20;
         //imgbtnSendMail.CommandName = "sendMail";
         //imgbtnSendMail.CommandArgument = "somevalue";
         imgbtnSendMail.Click += new ImageClickEventHandler(imgbtnSendMail_Click);
         //imgbtnSendMail.Attributes["runat"] = "server";
         //imgbtnSendMail.Attributes["onclick"] = "imgbtnSendMail_Click";
         e.Row.Cells[6].Controls.Add(imgbtnSendMail);
    }
}
protected void imgbtnSendMail_Click(object sender, ImageClickEventArgs e)
{
        string Text = "Image clicked";
}
4

2 回答 2

2

像这样更新 grvBubDetails_RowDataBound 事件;

ImageButton imgbtnSendMail = new ImageButton();
imgbtnSendMail.CommadName = "cmdSendMail"; // add this line
imgbtnSendMail.CommadArgument = "also you can pass a parameter from here";

将 RowCommand 事件添加到网格视图。在 RowCommand 事件处理函数中执行此操作;

if(e.CommandName.equals("cmdSendMail")){
    string Text = "Image clicked";
    string argument = e.CommandArgument;
}

更新:

在 PageLoad 事件之后触发的网格视图的 RowCommand 事件。您的按钮在每次页面重新加载后被删除,并且由于 rowDatabound 事件没有触发而没有重新创建。

工作代码:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<string> list = new List<string>()
            {
                "item 1",
                "item 2",
                "item 3"
            };
            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        // make sure outside of !IsPostback
        // recreate button every page load
        Button btn = new Button();
        btn.CommandName = "cmdSendMail";
        btn.CommandArgument = "sample arg";
        btn.Text = "send mail";
        GridView1.FooterRow.Cells[0].Controls.Add(btn);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Response.Write(e.CommandName + new Random().Next().ToString());
    }
}
于 2013-04-11T09:49:13.027 回答
0

您必须声明ImageButton全局范围(类级别)

ImageButton imgbtnSendMail = new ImageButton();
protected void grvBubDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.Footer)
     {
于 2013-04-11T09:40:09.143 回答