asp.net C#行命令中的我的Gridview没有';t在单击更新按钮后激发
本文关键字:单击 更新 按钮 命令 net 没有 Gridview 我的 asp | 更新日期: 2023-09-27 17:58:43
我创建了带有未绑定数据源的网格视图。因此按钮是动态创建的。asp.net没有任何干扰。所有的事情都是动态完成的。但在我单击更新按钮后,gridview中的rowcommand不会触发。你能帮忙吗。
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
DataTable dt;
LinkButton RC_1,RC_3,RC_4;
TextBox TX_01, TX_02, TX_03;
Button RC_2 ;
int rowindex,flag = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable();
MakeDataTable();
}
else
{
dt = (DataTable)ViewState["DataTable"];
BindGrid();
}
ViewState["DataTable"] = dt;
}
protected void B_01_Click(object sender, EventArgs e)
{
AddToDataTable();
BindGrid();
ClearFormData();
GV.Width = 10;
}
protected void B_02_Click(object sender, EventArgs e)
{
LB.Text = "Button 2 is clicked";
}
protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RC_1 = new LinkButton();
RC_2 = new Button();
RC_1.ID = "id_edit" + e.Row.RowIndex;
RC_1.Text = "Edit";
RC_2.ID = "id_delete" + e.Row.RowIndex;
RC_2.Text = "Delete";
RC_2.CommandName = "CMD_Delete";
RC_1.CommandName = "CMD_Edit" ;
RC_1.EnableViewState = true;
RC_2.EnableViewState = true;
//RC_1.Click += new EventHandler(RC_1_Click);
e.Row.Cells[3].Controls.Add(RC_1);
e.Row.Cells[4].Controls.Add(RC_2);
}
}
private void MakeDataTable()
{
dt.Columns.Add("Date", typeof(string));
dt.Columns.Add("Time", typeof(string));
dt.Columns.Add("Number", typeof(string));
dt.Columns.Add("Edit", typeof(string));
dt.Columns.Add("Name", typeof(string));
}
private void AddToDataTable()
{
DataRow NRow = dt.NewRow();
NRow[0] = TXT_01.Text;
NRow[1] = TXT_02.Text;
NRow[2] = TXT_03.Text;
dt.Rows.Add(NRow);
}
private void BindGrid()
{
GV.DataSource = dt;
GV.DataBind();
}
private void ClearFormData()
{
TXT_01.Text = "";
TXT_02.Text = "";
TXT_03.Text = "";
}
protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if((e.CommandName) == "CMD_Update")
{
GridViewRow gvr_upd = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
rowindex = Convert.ToInt32(gvr_upd.RowIndex);
GV.Rows[rowindex].Cells[1].Controls.Remove(TX_02);
GV.Rows[rowindex].Cells[0].Text = TX_01.Text;
GV.Rows[rowindex].Cells[1].Text = "Sandesh";
GV.Rows[rowindex].Cells[2].Text = TX_03.Text;
LB.Text = GV.Rows[rowindex].Cells[1].Text + " Updated Successfully ";
LB.Visible = true;
}
if ((e.CommandName) == "CMD_Delete")
{
GridViewRow gvr_del = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
rowindex = Convert.ToInt32(gvr_del.RowIndex) ;
dt.Rows.RemoveAt(rowindex);
BindGrid();
LB.Text = (rowindex + 1) + " Deleted Successfully ";
LB.Visible = true;
}
if ((e.CommandName) == "CMD_Edit")
{
GridViewRow gvr_edit = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
rowindex = Convert.ToInt32(gvr_edit.RowIndex);
RC_3 = new LinkButton();
RC_4 = new LinkButton();
TX_01 = new TextBox();
TX_02 = new TextBox();
TX_03 = new TextBox();
RC_3.ID = "id_update" + rowindex;
RC_3.Text = "Update ";
RC_4.ID = "id_cancel" + rowindex;
RC_4.Text = "Cancel";
RC_3.CommandName = "CMD_Update";
RC_4.CommandName = "CMD_Cancel";
RC_3.CausesValidation = false;
RC_3.EnableViewState = true;
RC_4.EnableViewState = true;
RC_3.Click += new EventHandler(RC_3_Click);
GV.Rows[rowindex].Cells[3].Text = "";
GV.Rows[rowindex].Cells[3].Controls.Add(RC_3);
GV.Rows[rowindex].Cells[3].Controls.Add(RC_4);
TX_01.Text = GV.Rows[rowindex].Cells[0].Text;
TX_02.Text = GV.Rows[rowindex].Cells[1].Text;
TX_03.Text = GV.Rows[rowindex].Cells[2].Text;
GV.Rows[rowindex].Cells[0].Text = "";
GV.Rows[rowindex].Cells[1].Text = "";
GV.Rows[rowindex].Cells[2].Text = "";
GV.Rows[rowindex].Cells[0].Controls.Add(TX_01);
GV.Rows[rowindex].Cells[1].Controls.Add(TX_02);
GV.Rows[rowindex].Cells[2].Controls.Add(TX_03);
}
}
protected void RC_3_Click(object sender, EventArgs e)
{
LB.Text = " Vishwas Updated Successfully ";
LB.Visible = true;
}
protected void RC_3_Cclick()
{
LB.Text = " Updated Successfully ";
LB.Visible = true;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" EnableSessionState="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Testing Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TXT_01" runat="server" MaxLength="10" Width="128px"></asp:TextBox>
<asp:TextBox ID="TXT_02" runat="server"></asp:TextBox>
<asp:TextBox ID="TXT_03" runat="server"></asp:TextBox>
<asp:Button ID="B_01" runat="server" onclick="B_01_Click" Text="Button" />
<asp:Button ID="B_02" runat="server" onclick="B_02_Click" Text="Button" />
<asp:Label ID="LB" runat="server">"Hi Answer"</asp:Label>
<br />
</div>
<asp:GridView ID="GV" runat="server" PageIndex="1"
Width="309px" EmptyDataText=""No Data"" Font-Names="Arial" ForeColor="Black"
UseAccessibleHeader="False" PageSize="5"
onrowcommand="GV_RowCommand" onrowdatabound="GV_RowDataBound"
EnableViewState="False">
<RowStyle Font-Names="Arial Black" />
<EditRowStyle ForeColor="Black" Font-Names="Arial Black" Font-Size="Medium" />
</asp:GridView>
</form>
</body>
</html>
您可以在bind方法中的databind()
之前附加GridView
中的EventHandler事件。
private void BindGrid()
{
GV.DataSource = dt;
GV.RowCommand += new GridViewRowEventHandler(GV_RowCommand);
GV.DataBind();
}
委托是创建动态控件时的最佳选项。请检查以下代码,我在动态控制应用程序中使用此代码:
updateButton.Click += delegate (System.Object o, System.EventArgs e)
{
//GV_RowCommand Binding Code here
};