如何从我以编程方式构建的 gridview 内的链接按钮调用服务器端方法?

发布于 2024-07-25 03:36:30 字数 2169 浏览 5 评论 0原文

我有以下代码。 我以编程方式构建一个 grdiview,然后浏览并将其中一列更改为链接按钮。 不幸的是,它永远不会回调服务器端 lb_Click 方法。 有人遇到过这种情况吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CommonMethods.logOn(Request.ServerVariables["LOGON_USER"]))
        {
            Response.Redirect("http://www.google.com");
        }

        // Now we have to build the questions
        using (var context = new nVoteDataContext())
        {
            var retrievedQuestions = from q in context.questions
                                     select new
                                                {
                                                    q.id,
                                                    q.question1,
                                                    q.questionType
                                                };

            GridView_questions.DataSource = retrievedQuestions;
            GridView_questions.DataBind();
            GridView_questions.HeaderRow.Cells[0].Text = "#";
            GridView_questions.HeaderRow.Cells[1].Text = "Question";

            foreach (GridViewRow s in GridView_questions.Rows)
            {
                if (s.RowType == DataControlRowType.DataRow)
                {
                    var lb = new LinkButton();
                    lb.CausesValidation = true;
                    lb.Attributes.Add("runat", "server");
                    lb.CommandName = "lb_Click";
                    lb.CommandArgument = s.Cells[0].Text;
                    lb.Text = s.Cells[1].Text;
                    s.Cells[1].Text = string.Empty;
                    s.Cells[1].Controls.Add(lb);
                }
            }

        }
    }

    public void lb_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow s in GridView_questions.Rows)
        {
            if (s.RowType == DataControlRowType.DataRow)
            {
                s.BackColor = System.Drawing.Color.Red;
            }
        }
    }
}

I have the following code.
I build a grdiview programamtically then I go through and change one of the columns to linkbuttons. Unfortunately, it never calls back to server side lb_Click method.
Has any one faced this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CommonMethods.logOn(Request.ServerVariables["LOGON_USER"]))
        {
            Response.Redirect("http://www.google.com");
        }

        // Now we have to build the questions
        using (var context = new nVoteDataContext())
        {
            var retrievedQuestions = from q in context.questions
                                     select new
                                                {
                                                    q.id,
                                                    q.question1,
                                                    q.questionType
                                                };

            GridView_questions.DataSource = retrievedQuestions;
            GridView_questions.DataBind();
            GridView_questions.HeaderRow.Cells[0].Text = "#";
            GridView_questions.HeaderRow.Cells[1].Text = "Question";

            foreach (GridViewRow s in GridView_questions.Rows)
            {
                if (s.RowType == DataControlRowType.DataRow)
                {
                    var lb = new LinkButton();
                    lb.CausesValidation = true;
                    lb.Attributes.Add("runat", "server");
                    lb.CommandName = "lb_Click";
                    lb.CommandArgument = s.Cells[0].Text;
                    lb.Text = s.Cells[1].Text;
                    s.Cells[1].Text = string.Empty;
                    s.Cells[1].Controls.Add(lb);
                }
            }

        }
    }

    public void lb_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow s in GridView_questions.Rows)
        {
            if (s.RowType == DataControlRowType.DataRow)
            {
                s.BackColor = System.Drawing.Color.Red;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2024-08-01 03:36:30

您需要为链接按钮添加一个处理程序,以在 Click 事件上调用 lb_Click

lb.Click += lb_Click;

You need to add a handler for the linkbutton to call lb_Click on the Click event

lb.Click += lb_Click;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文