总是得不到新值,得到的总是原来的值
场景:老有关问题了,为什么在gridview编辑提交时,总是得不到新值,得到的总是原来的值
老问题了,为什么在gridview编辑提交时,总是得不到新值,得到的总是原来的值。
在网上搜索了很久,该注意的地方都注意到了,如将绑定放在!IsPostBack里,但还是得不到新值。我在gridview里用的都是模板列,以下是源码,麻烦大家帮我看看吧,我已经折腾了2天了。
ASPX页面:
<asp:GridView Width="98%" ID="myGV" runat="server" AutoGenerateColumns="false" OnPageIndexChanging="PageChanging"
OnRowCommand="RowCommand" OnSorting="Sorting" OnRowDataBound="RowDataBound" OnRowCancelingEdit="myGV_RowCancelingEdit"
OnRowEditing="myGV_RowEditing" OnRowUpdating="myGV_RowUpdating" ShowFooter="true">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ItemStyle-HorizontalAlign="center"
ReadOnly="True">
<ItemStyle BackColor="#EFEFEF" Width="40px" />
</asp:BoundField>
<asp:TemplateField HeaderText="标签" SortExpression="tags" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<%# Eval("tags") %>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<EditItemTemplate>
<asp:TextBox SkinID="TextBox_Single" runat="server" Width="180px" Font-Size="9pt"
Text='<%# Eval("tags") %>' ID="Textbox211" NAME="Textbox211" />
</EditItemTemplate>
<FooterTemplate>
<div align="center" style="margin-left: 10pt">
<asp:LinkButton runat="server" OnClick="addNewRow" Text="<img src=images/add.gif border=0> 新建"
ID="Linkbutton1" NAME="Linkbutton1" />
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="分类" SortExpression="category" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<%# Eval("category")%>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<EditItemTemplate>
<asp:HiddenField ID="hdCategory" runat="server" Value='<%# Eval("category") %>' />
<asp:DropDownList ID="dropCategory" runat="server" Width="120px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="center" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton5" runat="server" CausesValidation="false" CommandName="del"
Text="<img src=images/delete.gif border=0 >" OnClientClick="return confirm('此操作不可恢复,请确认。')"
CommandArgument='<%# Eval("ID") %>'></asp:LinkButton>
</ItemTemplate>
<ItemStyle BackColor="#EFEFEF" Width="30px" />
</asp:TemplateField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True" EditText="<img src=images/edit1.gif border=0 align=absmiddle>"
UpdateText="√" CancelText="×">
<ItemStyle BackColor="#EFEFEF" Width="30px" Font-Bold="true" Font-Size="12px" HorizontalAlign="Center" />
<ControlStyle Font-Bold="true" Font-Size="12px" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="White" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" HorizontalAlign="Right" VerticalAlign="Middle" Font-Bold="False"
Font-Underline="False" />
<HeaderStyle BackColor="Gray" Font-Bold="True" ForeColor="WhiteSmoke" Height="20px" />
<PagerSettings Position="TopAndBottom" />
<AlternatingRowStyle BackColor="#F5F5F5" />
</asp:GridView>
</div>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
ppbc.isAuthorized();
ppbc.isAdminAuthorized();
conn = new OleDbConnection(Globe.CONNECTION_STRING);
if (!IsPostBack)
{
getTagCategory();
lblSQL.Text = "select * from tags order by category,tags";
myGV.PageIndex = 0;
setBind();
}
}
void getTagCategory()
{
cmd = new OleDbCommand("select distinct category from tags order by category", conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
lblCategory.Text += dr["category"].ToString() + "|";
}
dr.Close(); conn.Close();
}
//GridView数据绑定
void setBind()
{
ds = new DataSet();
da = new OleDbDataAdapter(lblSQL.Text, conn);
da.Fill(ds, "tags");
conn.Close();
DataView dv = new DataView(ds.Tables["tags"]);
if (ViewState["SortExpression"] != null)
{
dv.Sort = ViewState["SortExpression"].ToString() + " " + ViewState["SortDirection"].ToString();
}
myGV.DataSource = dv;
myGV.DataBind();
if (myGV.PageCount == 0)
{
lblPage.Text = "";
}
else
{
lblPage.Text = "第 <b>" + (myGV.PageIndex + 1) + "</b> 页 共 <b>" + myGV.PageCount + "</b> 页";
}
}
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("dropCategory")) != null)
{
DropDownList dropCategory = (DropDownList)e.Row.FindControl("dropCategory");
dropCategory.Items.Clear();
string[] array = lblCategory.Text.Split('|');
foreach (string str in array)
{
if (str.Trim() != "") dropCategory.Items.Add(str);
}
// 选中 DropDownList
dropCategory.SelectedValue = ((HiddenField)e.Row.FindControl("hdCategory")).Value;
}
}
protected void myGV_RowEditing(object sender, GridViewEditEventArgs e)
{
myGV.EditIndex = e.NewEditIndex;
setBind();
}
protected void myGV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
myGV.EditIndex = -1;
setBind();
}
protected void myGV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = myGV.Rows[e.RowIndex].Cells[0].Text;
//就是下面两行,始终取不到我更改过的新值。
string strTags = ((TextBox)myGV.Rows[e.RowIndex].Cells[1].FindControl("Textbox211")).Text;
string strCategory = ((DropDownList)myGV.Rows[e.RowIndex].Cells[2].FindControl("dropCategory")).SelectedValue;
cmd = new OleDbCommand("update [tags] set tags=@tags,category=@category where id=" + id, conn);
cmd.Parameters.AddWithValue("@tags", strTags);
cmd.Parameters.AddWithValue("@category", strCategory);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
myGV.EditIndex = -1;
setBind();
ppbc.WebLog("信息列表", "编辑Tags Tags=" + strTags);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('存盘成功。');", true);
}
protected void addNewRow(object sender, EventArgs e)
{
cmd = new OleDbCommand("insert into Tags(tags,category) values('','')", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
myGV.PageIndex = 0;
myGV.EditIndex = 0;
setBind();
ppbc.WebLog("信息列表", "新建Tags");
}
//按钮事件
protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
string id;
string strTags;
GridViewRow gvrow;
switch (e.CommandName)
{
case "del":
id = e.CommandArgument.ToString();
gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //获取被点击的linkButton所在的GridViewRow
strTags = myGV.Rows[gvrow.RowIndex].Cells[0].Text;
conn.Open();
cmd = new OleDbCommand("delete from tags where id=" + id, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
//日志
ppbc.WebLog("信息列表", "删除Tags Tags=" + strTags);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('删除成功。');", true);
break;
}
myGV.PageIndex = 0;
setBind();
}
------解决方案--------------------
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("dropCategory")) != null)
{
DropDownList dropCategory = (DropDownList)e.Row.FindControl("dropCategory");
dropCategory.Items.Clear();
string[] array = lblCategory.Text.Split('|');
foreach (string str in array)
{
if (str.Trim() != "") dropCategory.Items.Add(str);
}
// 选中 DropDownList
dropCategory.SelectedValue = ((HiddenField)e.Row.FindControl("hdCategory")).Value;
}
}
你这个事件里不报错?应该要对行进行判断:
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState != DataControlRowState.Edit)
{
if (((DropDownList)e.Row.FindControl("dropCategory")) != null)
{
DropDownList dropCategory = (DropDownList)e.Row.FindControl("dropCategory");
dropCategory.Items.Clear();
string[] array = lblCategory.Text.Split('|');
foreach (string str in array)
{
if (str.Trim() != "") dropCategory.Items.Add(str);
}
// 选中 DropDownList
dropCategory.SelectedValue = ((HiddenField)e.Row.FindControl("hdCategory")).Value;
}
老问题了,为什么在gridview编辑提交时,总是得不到新值,得到的总是原来的值。
在网上搜索了很久,该注意的地方都注意到了,如将绑定放在!IsPostBack里,但还是得不到新值。我在gridview里用的都是模板列,以下是源码,麻烦大家帮我看看吧,我已经折腾了2天了。
ASPX页面:
<asp:GridView Width="98%" ID="myGV" runat="server" AutoGenerateColumns="false" OnPageIndexChanging="PageChanging"
OnRowCommand="RowCommand" OnSorting="Sorting" OnRowDataBound="RowDataBound" OnRowCancelingEdit="myGV_RowCancelingEdit"
OnRowEditing="myGV_RowEditing" OnRowUpdating="myGV_RowUpdating" ShowFooter="true">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" ItemStyle-HorizontalAlign="center"
ReadOnly="True">
<ItemStyle BackColor="#EFEFEF" Width="40px" />
</asp:BoundField>
<asp:TemplateField HeaderText="标签" SortExpression="tags" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<%# Eval("tags") %>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<EditItemTemplate>
<asp:TextBox SkinID="TextBox_Single" runat="server" Width="180px" Font-Size="9pt"
Text='<%# Eval("tags") %>' ID="Textbox211" NAME="Textbox211" />
</EditItemTemplate>
<FooterTemplate>
<div align="center" style="margin-left: 10pt">
<asp:LinkButton runat="server" OnClick="addNewRow" Text="<img src=images/add.gif border=0> 新建"
ID="Linkbutton1" NAME="Linkbutton1" />
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="分类" SortExpression="category" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<%# Eval("category")%>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<EditItemTemplate>
<asp:HiddenField ID="hdCategory" runat="server" Value='<%# Eval("category") %>' />
<asp:DropDownList ID="dropCategory" runat="server" Width="120px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="center" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton5" runat="server" CausesValidation="false" CommandName="del"
Text="<img src=images/delete.gif border=0 >" OnClientClick="return confirm('此操作不可恢复,请确认。')"
CommandArgument='<%# Eval("ID") %>'></asp:LinkButton>
</ItemTemplate>
<ItemStyle BackColor="#EFEFEF" Width="30px" />
</asp:TemplateField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True" EditText="<img src=images/edit1.gif border=0 align=absmiddle>"
UpdateText="√" CancelText="×">
<ItemStyle BackColor="#EFEFEF" Width="30px" Font-Bold="true" Font-Size="12px" HorizontalAlign="Center" />
<ControlStyle Font-Bold="true" Font-Size="12px" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="White" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" HorizontalAlign="Right" VerticalAlign="Middle" Font-Bold="False"
Font-Underline="False" />
<HeaderStyle BackColor="Gray" Font-Bold="True" ForeColor="WhiteSmoke" Height="20px" />
<PagerSettings Position="TopAndBottom" />
<AlternatingRowStyle BackColor="#F5F5F5" />
</asp:GridView>
</div>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
ppbc.isAuthorized();
ppbc.isAdminAuthorized();
conn = new OleDbConnection(Globe.CONNECTION_STRING);
if (!IsPostBack)
{
getTagCategory();
lblSQL.Text = "select * from tags order by category,tags";
myGV.PageIndex = 0;
setBind();
}
}
void getTagCategory()
{
cmd = new OleDbCommand("select distinct category from tags order by category", conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
lblCategory.Text += dr["category"].ToString() + "|";
}
dr.Close(); conn.Close();
}
//GridView数据绑定
void setBind()
{
ds = new DataSet();
da = new OleDbDataAdapter(lblSQL.Text, conn);
da.Fill(ds, "tags");
conn.Close();
DataView dv = new DataView(ds.Tables["tags"]);
if (ViewState["SortExpression"] != null)
{
dv.Sort = ViewState["SortExpression"].ToString() + " " + ViewState["SortDirection"].ToString();
}
myGV.DataSource = dv;
myGV.DataBind();
if (myGV.PageCount == 0)
{
lblPage.Text = "";
}
else
{
lblPage.Text = "第 <b>" + (myGV.PageIndex + 1) + "</b> 页 共 <b>" + myGV.PageCount + "</b> 页";
}
}
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("dropCategory")) != null)
{
DropDownList dropCategory = (DropDownList)e.Row.FindControl("dropCategory");
dropCategory.Items.Clear();
string[] array = lblCategory.Text.Split('|');
foreach (string str in array)
{
if (str.Trim() != "") dropCategory.Items.Add(str);
}
// 选中 DropDownList
dropCategory.SelectedValue = ((HiddenField)e.Row.FindControl("hdCategory")).Value;
}
}
protected void myGV_RowEditing(object sender, GridViewEditEventArgs e)
{
myGV.EditIndex = e.NewEditIndex;
setBind();
}
protected void myGV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
myGV.EditIndex = -1;
setBind();
}
protected void myGV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = myGV.Rows[e.RowIndex].Cells[0].Text;
//就是下面两行,始终取不到我更改过的新值。
string strTags = ((TextBox)myGV.Rows[e.RowIndex].Cells[1].FindControl("Textbox211")).Text;
string strCategory = ((DropDownList)myGV.Rows[e.RowIndex].Cells[2].FindControl("dropCategory")).SelectedValue;
cmd = new OleDbCommand("update [tags] set tags=@tags,category=@category where id=" + id, conn);
cmd.Parameters.AddWithValue("@tags", strTags);
cmd.Parameters.AddWithValue("@category", strCategory);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
myGV.EditIndex = -1;
setBind();
ppbc.WebLog("信息列表", "编辑Tags Tags=" + strTags);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('存盘成功。');", true);
}
protected void addNewRow(object sender, EventArgs e)
{
cmd = new OleDbCommand("insert into Tags(tags,category) values('','')", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
myGV.PageIndex = 0;
myGV.EditIndex = 0;
setBind();
ppbc.WebLog("信息列表", "新建Tags");
}
//按钮事件
protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
string id;
string strTags;
GridViewRow gvrow;
switch (e.CommandName)
{
case "del":
id = e.CommandArgument.ToString();
gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //获取被点击的linkButton所在的GridViewRow
strTags = myGV.Rows[gvrow.RowIndex].Cells[0].Text;
conn.Open();
cmd = new OleDbCommand("delete from tags where id=" + id, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
//日志
ppbc.WebLog("信息列表", "删除Tags Tags=" + strTags);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('删除成功。');", true);
break;
}
myGV.PageIndex = 0;
setBind();
}
------解决方案--------------------
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("dropCategory")) != null)
{
DropDownList dropCategory = (DropDownList)e.Row.FindControl("dropCategory");
dropCategory.Items.Clear();
string[] array = lblCategory.Text.Split('|');
foreach (string str in array)
{
if (str.Trim() != "") dropCategory.Items.Add(str);
}
// 选中 DropDownList
dropCategory.SelectedValue = ((HiddenField)e.Row.FindControl("hdCategory")).Value;
}
}
你这个事件里不报错?应该要对行进行判断:
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState != DataControlRowState.Edit)
{
if (((DropDownList)e.Row.FindControl("dropCategory")) != null)
{
DropDownList dropCategory = (DropDownList)e.Row.FindControl("dropCategory");
dropCategory.Items.Clear();
string[] array = lblCategory.Text.Split('|');
foreach (string str in array)
{
if (str.Trim() != "") dropCategory.Items.Add(str);
}
// 选中 DropDownList
dropCategory.SelectedValue = ((HiddenField)e.Row.FindControl("hdCategory")).Value;
}