none
在updatepanel放一个带按钮的gridview,这个按钮无法触发事件 RRS feed

  • 问题

  • 在updatepanel放一个带按钮的gridview,这个按钮无法触发事件,
    这个按钮就失效,不知道该怎么办了,帮帮我谢谢
    2009年7月16日 8:40

答案

  • gridview 的按钮是需要指定command name 后 在gridview 的 Itemcommand事件截取的

    答案900, 目标五颗星 Aurvana Air, 音乐真的是随风飘来的 凉宫春日 永无止境的八月 你到底什么时候结束阿。。。。
    2009年7月16日 9:06
  • 楼主你好,

    如果你是加的command button,你可以如韦恩所说那样处理。如果你是模板列中加的button,你可以直接添加button的click事件进行处理。
    因为你的gridview是放在updatepanel中的,如果你是在button事件中操作一些控件,必须将这些控件也放在updatepanel中。你可以参考我下面的例子试试。
    <%@ Page Language="C#" %>
    
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Linq" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.Security" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.Web.UI.HtmlControls" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>
    <%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
    <%@ Import Namespace="System.Web.Services" %>
    <%@ Import Namespace="System.ComponentModel" %>
    <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
            
          protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.GridView1.DataSource = this.GenerateDT;
                this.GridView1.DataBind();
    
                ChangeBackColor();
               
            }
    
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            string s = string.Empty;
            foreach (TableCell item in this.GridView1.HeaderRow.Cells)
            {
                if (item.Controls.Count != 0)
                {
                    s += (item.Controls[0] as LinkButton).Text;
                }
                else
                    s += item.Text;
    
            }
            Response.Write(s);
    
        }
        public DataTable GenerateDT
        {
            get
            {
                if (ViewState["ds"] != null)
                {
                    return (ViewState["ds"] as DataSet).Tables[0];
    
                }
                else
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Id", typeof(int));
                    dt.Columns.Add("Test1", typeof(string));
                    dt.Columns.Add("Test2", typeof(string));
                    dt.Columns.Add("Test3", typeof(string));
                    dt.Columns.Add("UserName", typeof(string));
                    dt.Columns.Add("Password", typeof(string));
                    dt.Columns.Add("Bool", typeof(System.Boolean));
                    dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
                    DataRow dr1 = dt.NewRow();
                    dr1[0] = "1";
                    dr1[1] = "red";
                    dr1[2] = "test3";
                    dr1[3] = "test3";
                    dr1[4] = "Name1";
                    dr1[5] = "";
                    dr1[6] = true;
                    dt.Rows.Add(dr1);
                    DataRow dr2 = dt.NewRow();
                    dr2[0] = "2";
                    dr2[1] = "blue";
                    dr2[2] = "test3";
                    dr2[3] = "test3";
                    dr2[4] = "Name2";
                    dr2[5] = "2";
                    dr2[6] = true;
                    dt.Rows.Add(dr2);
                    DataRow dr3 = dt.NewRow();
                    dr3[0] = "3";
                    dr3[1] = "yellow";
                    dr3[2] = "test3";
                    dr3[3] = "test3";
                    dr3[4] = "Name2";
                    dr3[5] = "";
                    dr3[6] = true;
                    dt.Rows.Add(dr3);
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    ViewState["ds"] = ds;
                    return dt;
    
                }
            }
            set { ViewState["ds"] = value; }
        }
        
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow gvr = e.Row;
            if (gvr.RowType == DataControlRowType.Pager)
            {
                gvr.ID = "test";
                gvr.Controls[0].ID = "subTest";
    
                Table htc = (gvr.Controls[0].Controls[0] as Table);
    
                TableCell tc = new TableCell();
                tc.Controls.Add(new LiteralControl("Page No"));
                htc.Controls[0].Controls.AddAt(0, tc);
                //TableCell tc = new TableCell();
                //tc.Controls.Add(new LiteralControl("Page No"));
                //gvr.Cells.AddAt(0, tc);
    
            }
            else
                if (gvr.RowType==DataControlRowType.DataRow)
                {
                    gvr.Attributes.Add("onmouseover", "javascript:this.style.backgroundColor='#808080'");
                    gvr.Attributes.Add("onmouseout","javascript:this.style.backgroundColor='#ffffff'");
                }
    
        }
    
    
        protected void ChangeBackColor()
        {
            foreach (GridViewRow gvr in this.GridView1.Rows)
            {
                gvr.Cells[3].BackColor = System.Drawing.Color.FromName(gvr.Cells[3].Text);
            }
        }
    
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write(this.GridView1.SelectedRow.RowIndex.ToString());
        }
    
        protected void ChkDel_CheckedChanged(object sender, EventArgs e)
        {
    
        }
    
     
    
    
        protected void Button3_Click(object sender, EventArgs e)
        {
            
        }
    
        protected void Button3_Click1(object sender, EventArgs e)
        {
            this.TextBox2.Text = "test";
        }
    
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName=="Test")
            {
                this.TextBox2.Text = "test";
            }
        }
    
        protected void GridView1_RowDataBound(object sender, GridViewCommandEventArgs e)
        {
        }
        
      </script>  
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>  
            
            <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AllowPaging="True"
                PageSize="3" AutoGenerateSelectButton="True" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound"
                OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 
                    onrowcommand="GridView1_RowCommand">
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:TemplateField HeaderText="Index">
                        <ItemTemplate>
                            <%#((GridViewRow)(Container)).RowIndex %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:ButtonField ButtonType="Button" Text="Button" CommandName="Test" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="Button3" runat="server" Text="Button" 
                                onclick="Button3_Click1" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <SelectedRowStyle BackColor="LightCyan" ForeColor="DarkBlue" Font-Bold="true" />
            </asp:GridView>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
          </ContentTemplate>
            </asp:UpdatePanel>        
    </div>
       
     
    
        </form>
    </body>
    </html>

    Microsoft Online Community Support
    2009年7月17日 3:37

全部回复

  • gridview 的按钮是需要指定command name 后 在gridview 的 Itemcommand事件截取的

    答案900, 目标五颗星 Aurvana Air, 音乐真的是随风飘来的 凉宫春日 永无止境的八月 你到底什么时候结束阿。。。。
    2009年7月16日 9:06
  • 楼主你好,

    如果你是加的command button,你可以如韦恩所说那样处理。如果你是模板列中加的button,你可以直接添加button的click事件进行处理。
    因为你的gridview是放在updatepanel中的,如果你是在button事件中操作一些控件,必须将这些控件也放在updatepanel中。你可以参考我下面的例子试试。
    <%@ Page Language="C#" %>
    
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Linq" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.Security" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.Web.UI.HtmlControls" %>
    <%@ Import Namespace="System.Web.UI.WebControls" %>
    <%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
    <%@ Import Namespace="System.Web.Services" %>
    <%@ Import Namespace="System.ComponentModel" %>
    <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
            
          protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.GridView1.DataSource = this.GenerateDT;
                this.GridView1.DataBind();
    
                ChangeBackColor();
               
            }
    
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            string s = string.Empty;
            foreach (TableCell item in this.GridView1.HeaderRow.Cells)
            {
                if (item.Controls.Count != 0)
                {
                    s += (item.Controls[0] as LinkButton).Text;
                }
                else
                    s += item.Text;
    
            }
            Response.Write(s);
    
        }
        public DataTable GenerateDT
        {
            get
            {
                if (ViewState["ds"] != null)
                {
                    return (ViewState["ds"] as DataSet).Tables[0];
    
                }
                else
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Id", typeof(int));
                    dt.Columns.Add("Test1", typeof(string));
                    dt.Columns.Add("Test2", typeof(string));
                    dt.Columns.Add("Test3", typeof(string));
                    dt.Columns.Add("UserName", typeof(string));
                    dt.Columns.Add("Password", typeof(string));
                    dt.Columns.Add("Bool", typeof(System.Boolean));
                    dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
                    DataRow dr1 = dt.NewRow();
                    dr1[0] = "1";
                    dr1[1] = "red";
                    dr1[2] = "test3";
                    dr1[3] = "test3";
                    dr1[4] = "Name1";
                    dr1[5] = "";
                    dr1[6] = true;
                    dt.Rows.Add(dr1);
                    DataRow dr2 = dt.NewRow();
                    dr2[0] = "2";
                    dr2[1] = "blue";
                    dr2[2] = "test3";
                    dr2[3] = "test3";
                    dr2[4] = "Name2";
                    dr2[5] = "2";
                    dr2[6] = true;
                    dt.Rows.Add(dr2);
                    DataRow dr3 = dt.NewRow();
                    dr3[0] = "3";
                    dr3[1] = "yellow";
                    dr3[2] = "test3";
                    dr3[3] = "test3";
                    dr3[4] = "Name2";
                    dr3[5] = "";
                    dr3[6] = true;
                    dt.Rows.Add(dr3);
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    ViewState["ds"] = ds;
                    return dt;
    
                }
            }
            set { ViewState["ds"] = value; }
        }
        
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow gvr = e.Row;
            if (gvr.RowType == DataControlRowType.Pager)
            {
                gvr.ID = "test";
                gvr.Controls[0].ID = "subTest";
    
                Table htc = (gvr.Controls[0].Controls[0] as Table);
    
                TableCell tc = new TableCell();
                tc.Controls.Add(new LiteralControl("Page No"));
                htc.Controls[0].Controls.AddAt(0, tc);
                //TableCell tc = new TableCell();
                //tc.Controls.Add(new LiteralControl("Page No"));
                //gvr.Cells.AddAt(0, tc);
    
            }
            else
                if (gvr.RowType==DataControlRowType.DataRow)
                {
                    gvr.Attributes.Add("onmouseover", "javascript:this.style.backgroundColor='#808080'");
                    gvr.Attributes.Add("onmouseout","javascript:this.style.backgroundColor='#ffffff'");
                }
    
        }
    
    
        protected void ChangeBackColor()
        {
            foreach (GridViewRow gvr in this.GridView1.Rows)
            {
                gvr.Cells[3].BackColor = System.Drawing.Color.FromName(gvr.Cells[3].Text);
            }
        }
    
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write(this.GridView1.SelectedRow.RowIndex.ToString());
        }
    
        protected void ChkDel_CheckedChanged(object sender, EventArgs e)
        {
    
        }
    
     
    
    
        protected void Button3_Click(object sender, EventArgs e)
        {
            
        }
    
        protected void Button3_Click1(object sender, EventArgs e)
        {
            this.TextBox2.Text = "test";
        }
    
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName=="Test")
            {
                this.TextBox2.Text = "test";
            }
        }
    
        protected void GridView1_RowDataBound(object sender, GridViewCommandEventArgs e)
        {
        }
        
      </script>  
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>  
            
            <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AllowPaging="True"
                PageSize="3" AutoGenerateSelectButton="True" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound"
                OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 
                    onrowcommand="GridView1_RowCommand">
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:TemplateField HeaderText="Index">
                        <ItemTemplate>
                            <%#((GridViewRow)(Container)).RowIndex %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:ButtonField ButtonType="Button" Text="Button" CommandName="Test" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="Button3" runat="server" Text="Button" 
                                onclick="Button3_Click1" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <SelectedRowStyle BackColor="LightCyan" ForeColor="DarkBlue" Font-Bold="true" />
            </asp:GridView>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
          </ContentTemplate>
            </asp:UpdatePanel>        
    </div>
       
     
    
        </form>
    </body>
    </html>

    Microsoft Online Community Support
    2009年7月17日 3:37