Sana
Sana

Reputation: 11

Click event of Link button inside a gridview not working in a web page?

Link Button event not working properly... When i click on take quiz link button quiz will start on the next page after completion the link button disabled and text will changed with "Done" and also other fields label text will changed like Quiz Status label text is "open" and after taking quiz it will be "closed" and same with Submit status before "Not Submitted" and after "Submitted". Now with postbackurl of linkbutton the click event is not working on, quiz will start and when i came back to linkbutton page no cell text was changed of the others fields like status "Closed"and if i removed postbackurl of linkbutton and click on it button event work as not for that row but for the whole grid. Please suggest me some solution

****My ASPX code****

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
CellPadding="3" DataKeyNames="QuizID" 
DataSourceID="SqlDataSource2" style="text-align: center" Width="800px" 
onrowdatabound="GridView1_RowDataBound1">
<Columns>
<asp:BoundField DataField="QuizID" HeaderText="QuizID" InsertVisible="False" 
ReadOnly="True" SortExpression="QuizID" />
<asp:BoundField DataField="QuizTitle" HeaderText="QuizTitle" SortExpression="QuizTitle" />
<asp:BoundField DataField="StartDate" HeaderText="StartDate"  SortExpression="StartDate" />
<asp:BoundField DataField="EndDate" HeaderText="EndDate" SortExpression="EndDate" />
<asp:BoundField DataField="TotalMarks" HeaderText="TotalMarks" SortExpression="TotalMarks" />
<asp:TemplateField HeaderText="QuizStatus" SortExpression="QuizStatus">
<ItemTemplate>
<asp:Label ID="qStatus" runat="server" Text='<%# Bind("QuizStatus") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("QuizStatus") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SubmitStatus" SortExpression="SubmitStatus">
<ItemTemplate>
<asp:Label ID="sStatus" runat="server" Text='<%# Bind("SubmitStatus") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("SubmitStatus") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LectureID" HeaderText="LectureID" SortExpression="LectureID" />
<asp:BoundField DataField="CourseID" HeaderText="CourseID" SortExpression="CourseID" />
<asp:TemplateField HeaderText="Start" SortExpression="Start">
<ItemTemplate>
<asp:LinkButton ID="lbStart" runat="server" CausesValidation="false" CommandName="Select" Text="Take Quiz" onclick="lbStart_Click" PostBackUrl='<%# "start.aspx?LecID="+Eval("LectureID") %>'EnableTheming="False"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT * FROM [MQ_Quiz]"></asp:SqlDataSource>
</table>

C# code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;
using System.Collections.Generic;

public partial class StudentQuiz1 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void lbStart_Click(object sender, EventArgs e)
    { 
        int i = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            LinkButton lbtn = (LinkButton)row.FindControl("lbStart");
            if (lbtn != null)
            {
                i++;

                int QID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

                string status = "Closed";

                string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

                SqlConnection connection = new SqlConnection(con);

                string qry = "UPDATE [MQ_Quiz] SET QuizStatus='" + status + "' WHERE QuizID='" + QID + "'";
                SqlCommand cmd = new SqlCommand(qry, connection);
                connection.Open();
                cmd.ExecuteNonQuery();
                connection.Close();
                GridView1.DataBind();
            }
        }
    }

    protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string status = DataBinder.Eval(e.Row.DataItem, "QuizStatus").ToString();
            string start = DataBinder.Eval(e.Row.DataItem, "Start").ToString();

            if (status == "Open")
            {
                Label statuslbl = (Label)e.Row.FindControl("qStatus");
                statuslbl.ForeColor = System.Drawing.Color.Green;

                LinkButton lb = (LinkButton)e.Row.FindControl("lbStart");
                lb.Visible = true;

                Label submitlbl = (Label)e.Row.FindControl("sStatus");
                submitlbl.ForeColor = System.Drawing.Color.Red;

            }
            else if (status == "Closed")
            {
                Label statuslbl = (Label)e.Row.FindControl("qStatus");
                statuslbl.ForeColor = System.Drawing.Color.Red;

                Label submitlbl = (Label)e.Row.FindControl("sStatus");
                submitlbl.Text = "Submitted";
                submitlbl.ForeColor = System.Drawing.Color.Green;


                e.Row.Cells[9].Text = "Done";
                e.Row.Cells[9].Attributes.CssStyle[HtmlTextWriterStyle.Color] = "gray";
                e.Row.Cells[9].Enabled = false;
            }
        }
    }
}

Upvotes: 1

Views: 34323

Answers (3)

Anubrij Chandra
Anubrij Chandra

Reputation: 1432

Use your event like this:

 protected void lbStart_Click(object sender, EventArgs e)
 {
        LinkButton lbtn = (LinkButton)sender;
        GridViewRow row = (GridViewRow)lbtn.NamingContainer;
        if (row != null)
        {

            int QID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
            string status = "Closed";
            string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(con);
            string qry = "UPDATE [MQ_Quiz] SET QuizStatus='" + status + "' WHERE QuizID='" + QID + "'";
            SqlCommand cmd = new SqlCommand(qry, connection);
            connection.Open();
            cmd.ExecuteNonQuery();
            connection.Close();
            GridView1.DataBind();
        }

}

Upvotes: 0

Anubrij Chandra
Anubrij Chandra

Reputation: 1432

Remove PostBackUrl attribute from your link button , onClick event of your link will work.

<asp:LinkButton ID="lbStart" runat="server" CausesValidation="false" CommandName="Select" Text="Take Quiz" onclick="lbStart_Click" EnableTheming="False"></asp:LinkButton>

let me know if it worked. :)

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

In your gridview, add onRowCommand event like this:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
CellPadding="3" DataKeyNames="QuizID" 
DataSourceID="SqlDataSource2" style="text-align: center" Width="800px" 
onrowdatabound="GridView1_RowDataBound1"
OnRowCommand="GridView1_RowCommand">

link button like this:

<asp:LinkButton ID="lbStart" 
                runat="server" 
                CausesValidation="false" 
                CommandName="TakeQuiz" 
                Text="Take Quiz"
                CommandArgument='<%# Eval("LectureID") %>'
                EnableTheming="False">
                </asp:LinkButton>

and in gird view row command event of line button like this:

 protected void grdCustomPagging_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "TakeQuiz")
    {
        LinkButton lnkView = (LinkButton)e.CommandSource;
        string lectureId= lnkView.CommandArgument;
      // write link button click event code here
    }
}

Upvotes: 2

Related Questions