【问题标题】:Add button in gridview runtime with click event使用单击事件在gridview运行时添加按钮
【发布时间】:2014-05-26 22:49:15
【问题描述】:

我要做的就是在 gridview 中添加一个按钮,我想通过该按钮对当前行数据执行一些功能

这是我的gridview代码

<asp:GridView ID="dgvHMDEditorialManage" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="dgvHMDEditorialManage_RowCommand" OnRowCreated="dgvHMDEditorialManage_RowCreated" >
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

                </asp:GridView>  

C#代码

 using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
        {
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            DataSet ds = new DataSet();

            dgvHMDEditorialManage.DataSource = cmd.ExecuteReader();
            dgvHMDEditorialManage.DataBind();

        }  

我在这里设置gridview的数据源
现在我想在gridview中添加一个按钮,点击它我想用当前行数据执行一些功能
谁能建议我如何做到这一点
谢谢

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

你也可以这样做

    <asp:GridView ID="dgvHMDEditorialManage" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    <Columns>
                    <asp:TemplateField>
       <ItemTemplate>
            <asp:Button ID="Buttonid" runat="server" CommandName="Item" Text="Test" OnClick="abc_onclick" ></asp:Button>    
            </ItemTemplate>
        </asp:TemplateField>
                    <asp:TemplateField>
        <ItemTemplate>
        <asp:Label ID="mm" runat="server" Text='<%# Bind("Menu") %>'></asp:Label>
        </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
        <ItemTemplate>
        <asp:Label ID="mm1" runat="server" Text='<%# Bind("ParentID") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField><asp:TemplateField>
        <ItemTemplate>
        <asp:Label ID="mm2" runat="server" Text='<%# Bind("MenuID") %>'></asp:Label>
        </ItemTemplate>
 </asp:TemplateField>
 </Columns>
                </asp:GridView> 

在代码后面

 protected void abc_onclick(object sender, EventArgs e)
        {
            Button btnEdit = (Button)sender;

            GridViewRow grdrow = (GridViewRow)((Button)sender).NamingContainer;
            Label rowNumber = (Label)dgvHMDEditorialManage.Rows[grdrow.RowIndex].Cells[0].FindControl("mm");
            Label rowNumber1 = (Label)dgvHMDEditorialManage.Rows[grdrow.RowIndex].Cells[1].FindControl("mm1");
            Label rowNumber2 = (Label)dgvHMDEditorialManage.Rows[grdrow.RowIndex].Cells[2].FindControl("mm2");

            string abc = rowNumber.Text;
            string dealId = rowNumber1.Text;
            string dealTitle = rowNumber2.Text; ;
            //do something
        }

和页面加载代码

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            loadGrid();
            }
        }
        public void loadGrid()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Menu", typeof(string));
            dt.Columns.Add("ParentID", typeof(string));
            dt.Columns.Add("MenuID", typeof(string));


            dt.Rows.Add("Name", "1", "1");
            dt.Rows.Add("Name1", "1", "2");
            dt.Rows.Add("Name2", "1", "3");
            dt.Rows.Add("Name3", "1", "4");
            dt.Rows.Add("Name4", "1", "5");
            dt.Rows.Add("Class", "2", "6");
            dt.Rows.Add("Class1", "2", "7");
            dt.Rows.Add("Class2", "2", "8");
            dt.Rows.Add("Class3", "2", "9");
            dt.Rows.Add("Class4", "2", "10");
            dgvHMDEditorialManage.DataSource = dt;
            dgvHMDEditorialManage.DataBind();

        }

【讨论】:

  • 它是一个复制的尝试我已经应用了这个解决方案
  • 由于保护级别,调试错误无法访问
  • 当您尝试此解决方案时,它会给出此错误..?类型“System.Web.UI.WebControls.GridView”没有名为“TemplateField”的公共属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 2013-05-24
  • 2020-08-13
  • 1970-01-01
相关资源
最近更新 更多