gpt4 book ai didi

c# - 使用单击事件在 gridview 运行时添加按钮

转载 作者:行者123 更新时间:2023-11-30 18:29:24 24 4
gpt4 key购买 nike

我想做的就是在 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 中添加一个按钮,点击它我想用当前行数据执行一些功能
谁能建议我怎么做
谢谢

最佳答案

你也可以这样做

    <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();

}

关于c# - 使用单击事件在 gridview 运行时添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23035323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com