views:

1464

answers:

2

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;
            }
        }
    }
}
+1  A: 

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

lb.Click += lb_Click;
Ken Pespisa
Wow - I am dumb! :-) Thanks a ton!
Matt
A: 

It's Quite simple, Please first have a look over c# code which will be written in aspx.cs

protected void lnkCustomerID_Click(object sender, EventArgs e)

{

LinkButton link = (LinkButton)sender;

GridViewRow gv = (GridViewRow)(link.Parent.Parent);

LinkButton CustomerID = (LinkButton)gv.FindControl("lnkCustomerID");

Session["customerid"] = CustomerID.Text;

Response.Redirect("customer_detail.aspx");

}

and here's the code of gridview's link button for your .aspx page

<asp:GridView ID="GridView1" runat="server" Width="100%" EnableTheming="false" AutoGenerateColumns="false">

<Columns>

<asp:TemplateField HeaderText="Customer ID">

<ItemTemplate>

<asp:LinkButton ID="lnkCustomerID" Text='<%# Bind("CustomerID")%>' runat="server" OnClick="lnkCustomerID_Click"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Customer Name">

<ItemTemplate>

<asp:Label ID="lblCustomerName" Text='<%# Bind("CustomerName")%>' runat="server"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Customer Age">

<ItemTemplate>

<asp:Label ID="lblCustomerAge" Text='<%# Bind("CustomerAge")%>' runat="server"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

for more details please visit http://nice-tutorials.blogspot.com/