[英]When I click on the LinkButton, it doesn't call the function “LinkButton_Click”
LinkButton 我需要在后端完成,所以我無法在前端使用 OnClientClick。 知道如何解決嗎?
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
TableCell tc = new TableCell();
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Staff ID";
e.Row.Cells[1].Text = "Staff Name";
e.Row.Cells[2].Text = "Position";
e.Row.Cells[3].Text = "Status";
e.Row.Cells[4].Text = "Phone";
e.Row.Cells[5].Text = "Email";
//Add an addition column, else cant add linkbutton at the last column
e.Row.Controls.Add(tc);
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
//add linkbutton at every last column
e.Row.Controls.Add(tc);
LinkButton link = new LinkButton();
link.ID = "lbEdit";
link.Text = "Edit";
e.Row.Cells[6].Controls.Add(link);
link.Click += new EventHandler(LinkButton_Click);
if (e.Row.Cells[3].Text == "Inactive")
{
e.Row.Cells[3].CssClass = "redCell";
}
}
}
這是 LinkButton_Click function
protected void LinkButton_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('It works!')</script>");
Response.Redirect("~/product.aspx");
}
或者我可以在哪里粘貼我的“link.Click += new EventHandler(LinkButton_Click);”?
這是 <asp:GridView />
<asp:GridView CssClass="GridView" ID="staffList" AutoGenerateColumns="true" runat="server" onrowdatabound="GridView_RowDataBound">
</asp:GridView>
整個后端代碼
public partial class staff : System.Web.UI.Page
{
string sqlConn = Convert.ToString(ConfigurationManager.AppSettings["connectionString"]);
//LinkButton link = new LinkButton();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
MySqlConnection conn = new MySqlConnection(sqlConn);
string sql = "SELECT * from Staff";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
staffList.DataSource = dt;
staffList.DataBind();
}
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
TableCell tc = new TableCell();
LinkButton link = new LinkButton();
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Staff ID";
e.Row.Cells[1].Text = "Staff Name";
e.Row.Cells[2].Text = "Position";
e.Row.Cells[3].Text = "Status";
e.Row.Cells[4].Text = "Phone";
e.Row.Cells[5].Text = "Email";
//Add an addition column, else cant add linkbutton at the last column
e.Row.Controls.Add(tc);
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
//add linkbutton at every last column
e.Row.Controls.Add(tc);
link.ID = "lbEdit";
link.Text = "Edit";
e.Row.Cells[6].Controls.Add(link);
link.Click += new EventHandler(LinkButton_Click);
//maybe can create a for loop at here to store all data
if (e.Row.Cells[3].Text == "Inactive")
{
e.Row.Cells[3].CssClass = "redCell";
}
}
}
protected void LinkButton_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('It works!')</script>");
Response.Redirect("~/product.aspx");
}
}
當您單擊按鈕時,會發生 PostBack,並且 Gridview 綁定以及按鈕鏈接將丟失。 下面將 gridView.DataSource 保存到 session 中。 注意 if (:IsPostPack) 被刪除:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["gvDS"] != null)
{
GridView1.DataSource = Session["gvDS"];
GridView1.DataBind();
}
else
BindGridView();
}
private void BindGridView()
{
MySqlConnection conn = new MySqlConnection(sqlConn);
string sql = "SELECT * from Staff";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
staffList.DataSource = dt;
staffList.DataBind();
Session["gvDS"] = GridView1.DataSource; // save into session
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.