点击网格中不能在网页中工作的链接按钮事件?

时间:2014-04-20 09:04:42

标签: c# asp.net

链接按钮事件无法正常工作...当我点击拍摄测验链接按钮测验将在下一页开始完成后禁用链接按钮,文字将随着"完成"以及其他字段标签文本将更改,如测验状态标签文本是"打开"在参加测验后,它将被关闭"和#34;未提交"之前提交状态相同然后"提交"。现在通过链接按钮的postbackurl,click事件不起作用,测验将开始,当我回到linkbutton页面时,没有更改其他字段的单元格文本,例如status" Closed"以及如果我删除了linkbackton的linkbackton并单击它按钮事件工作不是为该行而是为整个网格。请建议我一些解决方案

****我的ASPX代码****

<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#代码

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;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

在gridview中,添加如下所示的RowCommand事件:

<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">
像这样的

链接按钮:

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

并在gridview行命令事件中的行按钮如下:

 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
    }
}

答案 1 :(得分:0)

从链接按钮中删除PostBackUrl属性,链接的onClick事件将起作用。

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

让我知道它是否有效。 :)

答案 2 :(得分:0)

使用您的活动:

 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();
        }

}