首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我单击LinkButton时,它不会调用函数"LinkButton_Click“

当我单击LinkButton时,它不会调用函数"LinkButton_Click“
EN

Stack Overflow用户
提问于 2020-08-16 23:20:06
回答 1查看 105关注 0票数 0

我需要在后台做的LinkButton,所以我不能在前端使用OnClientClick。你知道怎么解决这个问题吗?

代码语言:javascript
复制
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函数

代码语言:javascript
复制
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);"?

这是

代码语言:javascript
复制
<asp:GridView CssClass="GridView"  ID="staffList" AutoGenerateColumns="true" runat="server" onrowdatabound="GridView_RowDataBound">

</asp:GridView>

整个后端代码

代码语言:javascript
复制
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");
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-17 02:31:16

当您单击该按钮时,将出现PostBack,并且Gridview绑定以及按钮链接将丢失。下面将gridView.DataSource保存到会话中。请注意,如果删除了(!IsPostPack):

代码语言:javascript
复制
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

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63438564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档