[英]Click event of Link button inside a gridview not working in a web page?
鏈接按鈕事件無法正常工作...當我單擊“參加測驗”時,鏈接按鈕測驗將在完成后在下一頁上開始,鏈接按鈕被禁用,文本將更改為“完成”,其他字段的標簽文本也會更改,例如“測驗狀態”標簽文本為“打開”,測驗后將為“關閉”,並且在“未提交”之前和“已提交”之前與“提交”狀態相同。 現在,使用linkbutton的postbackurl,單擊事件將無法進行,測驗將開始,當我返回到linkbutton頁面時,其他字段(如狀態“ Closed”)的單元格文本均未更改,並且如果我刪除了linkbutton的postbackurl並單擊它,事件工作不是針對該行而是針對整個網格。 請給我一些解決方案
****我的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;
}
}
}
}
在您的gridview中,添加onRowCommand事件,如下所示:
<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>
在網格視圖的行按鈕中,行命令事件如下所示:
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
}
}
從鏈接按鈕中刪除PostBackUrl屬性,鏈接的onClick事件將起作用。
<asp:LinkButton ID="lbStart" runat="server" CausesValidation="false" CommandName="Select" Text="Take Quiz" onclick="lbStart_Click" EnableTheming="False"></asp:LinkButton>
讓我知道是否有效。 :)
像這樣使用您的事件:
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.