gridview中if (!IsPostBack)的问题
本文关键字:问题 IsPostBack if gridview | 更新日期: 2023-09-27 17:49:29
我有一个gridview,一些列是可编辑的文本字段,另一列是模板字段中的按钮,带有命令名称(ED)。
我有一个问题的回发,因为当我键入下一个文本到文本框,我需要能够单击保存按钮,并保存gridview的新数据,为了做到这一点,我需要在我的populategridview周围的页面加载if (!IsPostBack),以阻止它覆盖我改变的数据。
这将工作得很好,但现在当点击我的按钮,它不击中命令名称和按钮只是消失,而不是做任何事情。如果我删除If (!IsPostBack)按钮工作正常,但我无法获得新输入的文本数据。
下面是我的一些代码:
protected void Page_Load(object sender, EventArgs e)
{
GetUserInfo();
constPageID = Convert.ToInt16(Request.QueryString["PageID"]);
if (!IsPostBack)
{
PopulateGridview();
}
}
private void PopulateGridview()
{
try
{
using (SqlConnection conn = new SqlConnection(GetConnection.GetConnectionString()))
{
SqlCommand sqlComm = new SqlCommand("PL_UserColumns_Get", conn);
sqlComm.Parameters.AddWithValue("@PageID", constPageID);
sqlComm.Parameters.AddWithValue("@UserID", strUserID);
sqlComm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlComm;
da.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
gvOrder.DataSource = ds.Tables[0];
gvOrder.DataBind();
}
}
}
catch (SqlException ex)
{
ExceptionHandling.SQLException(ex, constPageID, constIsSiteSpecific);
}
}
protected void gvOrder_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ED")
{
GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer;
int rowIndex = gvr.RowIndex;
Label ID = gvr.FindControl("lblID") as Label;
string id = ID.Text;
try
{
SqlConnection dbConnection = new SqlConnection();
dbConnection.ConnectionString = GetConnection.GetConnectionString();
SqlCommand dbCommand = new SqlCommand("PL_UserColumns_ED", dbConnection);
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.Parameters.Add("@PageID", SqlDbType.Int).Value = Convert.ToInt16(constPageID);
dbCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt16(strUserID);
dbCommand.Parameters.Add("@ColumnID", SqlDbType.Int).Value = Convert.ToInt16(id);
dbConnection.Open();
dbCommand.ExecuteNonQuery();
dbConnection.Close();
}
catch (SqlException ex)
{
ExceptionHandling.SQLException(ex, constPageID, constIsSiteSpecific);
}
Response.Redirect("ModifyColumns.aspx?&PageID=" + constPageID);
}
}
}
行数据绑定:protected void gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = ds.Tables[0];
DropDownList ddl = new DropDownList();
TextBox txt = new TextBox();
int index = 1;
int indexenabled = 1;
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl = e.Row.FindControl("ddlNewO") as DropDownList;
txt = e.Row.FindControl("txtNewT") as TextBox;
}
if (e.Row.RowIndex == 0)
{
ddl.Items.Add("1");
ddl.Enabled = false;
txt.Enabled = false;
}
else if (e.Row.RowIndex != 0)
{
ddl.Items.Remove("1");
//Create ED button
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnED = new Button();
btnED.ID = "btnED";
btnED.CssClass = "buttonsmall";
btnED.CommandName = "ED";
btnED.EnableViewState = true;
DataRow r = dt.Rows[e.Row.RowIndex];
if (r.ItemArray[3].ToString() == "1")
{
btnED.Text = "Disable";
e.Row.CssClass = "RowEnabled";
foreach (DataRow r2 in dt.Rows)
{
if (r2.ItemArray[3].ToString() == "1")
{
string listitem = Convert.ToString(indexenabled+1);
ddl.Items.Add(listitem);
indexenabled++;
}
}
int itemtoremove = ddl.Items.Count+1;
ddl.Items.Remove(itemtoremove.ToString());
ddl.SelectedIndex = idxselected;
idxselected++;
}
else if (r.ItemArray[3].ToString() == "0")
{
btnED.Text = "Enable";
e.Row.CssClass = "RowDisabled";
ddl.Enabled = false;
txt.Enabled = false;
foreach (DataRow r1 in dt.Rows)
{
string listitem = Convert.ToString(index);
ddl.Items.Add(listitem);
index++;
}
ddl.SelectedIndex = e.Row.RowIndex;
}
//Add button to grid
e.Row.Cells[6].Controls.Add(btnED);
}
}
}
protected void btnED_Click(object sender, EventArgs e)
{
// Coding to click event
}
protected void GetUserInfo()
{
try
{
if (UserInfo == null)
{
//Sorry...no cookie!
}
else
{
if (!string.IsNullOrEmpty(UserInfo.Values["UserID"]))
{
strUserID = UserInfo.Values["UserID"];
}
}
}
catch (Exception ex)
{
ExceptionHandling.NETException(ex, constPageID, constIsSiteSpecific);
}
}
ASPX:
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvOrder" runat="server" Width="100%" CssClass="tblBrowse" AutoGenerateColumns="False" OnRowDataBound="gvOrder_RowDataBound" OnRowCommand="gvOrder_RowCommand">
<Columns>
<asp:BoundField DataField="CurrentO" HeaderText="Curr. Order" />
<asp:TemplateField HeaderText="New Order">
<ItemTemplate>
<asp:DropDownList ID="ddlNewO" runat="server" Width="99%"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CurrentT" HeaderText="Curr. Text" />
<asp:TemplateField HeaderText="New Text">
<ItemTemplate>
<asp:TextBox runat="server" Width="98%" ID="txtNewT" Text='<%# Bind("CurrentT") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblED" runat="server" Text='<%# Bind("Enabled") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enable/Disable"></asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="btnSave" runat="server" CssClass="smallButton" OnClick="btnSave_Click" Text="Save" Width="100px" OnClientClick="Close(); return true;" />
<br />
<asp:Label ID="lblMessage" runat="server" ForeColor="Red" Visible="False"></asp:Label>
</form>
</body>
这里的问题是,您正在尝试动态添加按钮,因此每次您都必须单独构建该列。由于您使用的是gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
,因此该方法将不会在post back中调用,因为您没有在post back中执行任何gvOrder数据绑定,因此您的按钮将被错过,因为它们被添加在RowDataBound事件下。
尝试下面的代码并检查它是否工作正常:
Page_load方法:
protected void Page_Load(object sender, EventArgs e)
{
GetUserInfo();
constPageID = Convert.ToInt16(Request.QueryString["PageID"]);
if (!IsPostBack)
{
PopulateGridview();
}
contructColumn();
}
行数据绑定方法:
protected void gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
{
contructColumn();
}
构造列方法(新增方法):
private void contructColumn()
{
DataTable dt = ds.Tables[0];
DropDownList ddl = new DropDownList();
TextBox txt = new TextBox();
int index = 1;
int indexenabled = 1;
foreach (GridViewRow row in gvOrder.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
ddl = row.FindControl("ddlNewO") as DropDownList;
txt = row.FindControl("txtNewT") as TextBox;
}
if (row.RowIndex == 0)
{
ddl.Items.Add("1");
ddl.Enabled = false;
txt.Enabled = false;
}
else if (row.RowIndex != 0)
{
ddl.Items.Remove("1");
//Create ED button
if (row.RowType == DataControlRowType.DataRow)
{
Button btnED = new Button();
btnED.ID = "btnED"+row.RowIndex;
btnED.CssClass = "buttonsmall";
btnED.CommandName = "ED";
btnED.EnableViewState = true;
DataRow r = dt.Rows[row.RowIndex];
if (r.ItemArray[3].ToString() == "1")
{
btnED.Text = "Disable";
row.CssClass = "RowEnabled";
foreach (DataRow r2 in dt.Rows)
{
if (r2.ItemArray[3].ToString() == "1")
{
string listitem = Convert.ToString(indexenabled + 1);
ddl.Items.Add(listitem);
indexenabled++;
}
}
int itemtoremove = ddl.Items.Count + 1;
ddl.Items.Remove(itemtoremove.ToString());
ddl.SelectedIndex = idxselected;
idxselected++;
}
else if (r.ItemArray[3].ToString() == "0")
{
btnED.Text = "Enable";
row.CssClass = "RowDisabled";
ddl.Enabled = false;
txt.Enabled = false;
foreach (DataRow r1 in dt.Rows)
{
string listitem = Convert.ToString(index);
ddl.Items.Add(listitem);
index++;
}
ddl.SelectedIndex = row.RowIndex;
}
//Add button to grid
row.Cells[6].Controls.Add(btnED);
}
}
}
}
您可以在页面呈现器事件上处理对网格的更新,如下所示:
protected void Page_PreRender(object sender, EventArgs e)
{
if (IsPostBack)
{
PopulateGridview();
}
}
ASP。. NET页面生命周期并不总是直截了当的。从微软看这个http://msdn.microsoft.com/en-us/library/ms178472.aspx
如果ModifyColumns.aspx
是你发布的所有代码所在的同一页面,那么它也是一个明显的设计缺陷。
你需要替换:
Response.Redirect("ModifyColumns.aspx?&PageID=" + constPageID);
PopulateGridview();
in gvOrder_RowCommand
method.
除此之外,我不确定我是否完全理解你在gvOrder_RowDataBound
方法中想要实现的目标,但我认为你想要实现的目标可以在不动态添加控件的情况下完成。在添加动态控件时,您需要谨慎地在正确的时间重新创建它们,并且在已经使用服务器控件时应避免使用动态控件(这会使复杂性增加两倍)。更过网格的视图控件提供了丰富的特性集来实现各种各样的功能。
您是否尝试显式地将EnableViewState
属性设置为true
?在我看来,你的问题是由于需要维护你的ViewState
数据,但还没有人触及这一点。尝试将EnableViewState="true"
添加到GridView
控件中,看看问题是否得到解决。
True
是该属性的默认值,但它可能在其他地方被覆盖。我喜欢先介绍我的基本情况:)