我有一个gridview,有些列是可编辑的文本字段,另一列是带有命令名称(ED)的模板字段中的按钮。
我的回发有问题,因为当我在文本框中键入下一个文本时,我需要能够单击保存按钮并保存gridview的新数据,为了做到这一点,我需要if(!IsPostBack) )在页面中加载我的populategridview以阻止它覆盖我更改的数据。
这样可以正常工作但是现在点击我的按钮时它没有点击命令名称,按钮只是消失而不是做任何事情。如果我删除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>
答案 0 :(得分:1)
您可以在页面预呈现事件上处理对网格的更新,如下所示:
protected void Page_PreRender(object sender, EventArgs e)
{
if (IsPostBack)
{
PopulateGridview();
}
}
ASP.NET页面生命周期并不总是直截了当。请参阅Microsoft http://msdn.microsoft.com/en-us/library/ms178472.aspx
答案 1 :(得分:1)
这里的问题是您尝试动态添加按钮,因此每次必须单独构造该列时。由于您使用的是gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
,因此您不会在回发中调用此方法,因为您在回发中没有进行任何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();
}
构造Column方法(新添加的方法):
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);
}
}
}
}
答案 2 :(得分:1)
如果ModifyColumns.aspx
与您发布的所有代码所在的页面相同,那么它也是明显的设计缺陷之一。
您需要替换:
Response.Redirect("ModifyColumns.aspx?&PageID=" + constPageID);
使用
PopulateGridview();
在gvOrder_RowCommand
方法中。
除此之外我不确定我是否完全理解你在gvOrder_RowDataBound
方法中想要实现的目标,但我认为你想要实现的目标可以在不必动态添加控件的情况下完成。添加动态控件时,您需要谨慎在正确的时间重新创建它们,并且在使用服务器控件时应该避免动态控件(它会增加两倍的复杂性)。网格视图控件的更多功能提供了丰富的功能,以实现各种功能。
希望这有帮助。
答案 3 :(得分:0)
您是否尝试过明确地将EnableViewState
属性明确设置为true
?在我看来,您的问题是由于需要维护您的ViewState
数据,但还没有人触及这一点。尝试将EnableViewState="true"
添加到GridView
控件中,看看问题是否已解决。
True
是属性的默认值,但可能会在其他位置覆盖它。我是第一个覆盖我的基础案例的粉丝:)