问题描述
你好
我正在处理应用程序,它需要在gridview中生成动态列,然后用linkbutton绑定。
这么多已完成,但我必须添加点击event to link按钮但事件没有被触发。
下面是我试过的代码。
以下代码是gridview的rowbound事件。
我尝试过的事情:
foreach(日期时间dt,天数)
{
LinkButton lb = new LinkButton();
lb.ID =lb+ i;
lb.Attributes.Add(runat,server);
lb.Text = r1 [dt.Date.ToString(dd-MM-yyyy)]。ToString( );
lb.CommandName =UpDate;
lb.Command + = lb_Command;
// lb.OnClientClick + = new EventHandler(onLinkClick);
// lb.Click + = new System.EventHandler(onLinkClick);
// lb.DataBinding + = lb_DataBinding ;
row.Cells [i + 2] .Controls.Add(lb);
i ++ ;
if(dt.Date< = System.DateTime.Today)
{
lb.Enabled = false;
}
}
}
Hello
I am working on application which requires to generate dynamic columns in gridview and then bound with linkbutton.
this much is done , but i have to add click event to link button but event is not fired .
below is code which i have tried.
below code is inside rowbound event of gridview.
What I have tried:
foreach (DateTime dt in days)
{
LinkButton lb = new LinkButton();
lb.ID = "lb" + i;
lb.Attributes.Add("runat", "server");
lb.Text = r1[dt.Date.ToString("dd-MM-yyyy")].ToString();
lb.CommandName = "UpDate";
lb.Command += lb_Command;
// lb.OnClientClick += new EventHandler(onLinkClick);
// lb.Click += new System.EventHandler(onLinkClick);
// lb.DataBinding += lb_DataBinding;
row.Cells[i + 2].Controls.Add(lb);
i++;
if (dt.Date <= System.DateTime.Today)
{
lb.Enabled = false;
}
}
}
推荐答案
<asp:content id="Content2" contentplaceholderid="MainContent" runat="server" xmlns:asp="#unknown">
<asp:gridview id="GridView1" runat="server" onrowcreated="GridView1_RowCreated">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
</asp:content>
背后的代码:
CODE BEHIND:
using System;
using System.Web.UI.WebControls;
using System.Data;
namespace WebFormDemo
{
public partial class DynamicControlInGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
BindGridView();
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
LinkButton lb = new LinkButton();
lb.ID = "LinkButton1";
lb.Text = "Click Me!";
lb.Click += OnLinkClick;
PlaceHolder p = (PlaceHolder)e.Row.FindControl("PlaceHolder1");
p.Controls.Add(lb);
}
}
protected void OnLinkClick(object sender, EventArgs e) {
LinkButton lb = (LinkButton)sender;
GridViewRow row = (GridViewRow)lb.NamingContainer;
if (row != null) {
Response.Write("Found it!");
}
}
private void BindGridView() {
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
public DataTable CreateDataSource() {
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("ID", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Lastname", typeof(string)));
dr = dt.NewRow();
//add values to each columns
dr["ID"] = 1;
dr["Name"] = "Vincent";
dr["LastName"] = "Durano";
dt.Rows.Add(dr);
return dt;
}
}
}
这篇关于将click事件添加到gridview中动态创建的linkbutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!