在没有分页之前,编辑是没问题的,可分页后编辑就出问题了
编辑列的前台代码:<asp:TemplateColumn HeaderStyle-Font-Bold="True" HeaderText="排序" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="#ffffff" HeaderStyle-Width="40">
<ItemTemplate>
<asp:Label ID="lb_sort" Runat="server">
<%# DataBinder.Eval(Container.DataItem, "sort")%>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb_sort" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "sort")%>'>
</asp:TextBox>
<asp:RegularExpressionValidator id="RegularExpressionValidator1" ControlToValidate="tb_sort" ValidationExpression="[0-9]+"
Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server"> *必须是数字</asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateColumn>
出问题后代码:string typecontent_sort=((TextBox)e.Item.Cells[3].FindControl("tb_sort")).Text;//没有变为输入的值 ,得到的还是原来的值
编辑列的前台代码:<asp:TemplateColumn HeaderStyle-Font-Bold="True" HeaderText="排序" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="#ffffff" HeaderStyle-Width="40">
<ItemTemplate>
<asp:Label ID="lb_sort" Runat="server">
<%# DataBinder.Eval(Container.DataItem, "sort")%>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb_sort" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "sort")%>'>
</asp:TextBox>
<asp:RegularExpressionValidator id="RegularExpressionValidator1" ControlToValidate="tb_sort" ValidationExpression="[0-9]+"
Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server"> *必须是数字</asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateColumn>
出问题后代码:string typecontent_sort=((TextBox)e.Item.Cells[3].FindControl("tb_sort")).Text;//没有变为输入的值 ,得到的还是原来的值
试试看
解决办法是
if(!Page.IsPostBack)
{
bindData();//你定义的绑定datagrid的方法
}
这位大侠说的应该是你出现的问题.试试看.
我都说了没分页前我的可以编辑的了,分页后就编辑不了了
to: foxconn_nich()
string typecontent_sort=((TextBox)e.Item.Cells[3].FindControl("tb_sort")).Text;//这里的是3,跟踪的结果是输入的值没有改变
to:playboy840616(贝克)
当然是放在if(!Page.IsPostBack)里边了,不然没分页前怎么没问题
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
using ysdj.classes;namespace ysdj.admin_g_l
{
/// <summary>
/// typecontent_admin 的摘要说明。
/// </summary>
public class typecontent_admin : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox tb_typeid;
protected System.Web.UI.WebControls.TextBox tb_addsort;
protected System.Web.UI.WebControls.RegularExpressionValidator Regularexpressionvalidator2;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox tb_addtypecontent;
protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator2;
protected Wuqi.Webdiyer.AspNetPager AspNetPager1;
public string type_id;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(Request.QueryString["typeid"]==null || Request.QueryString["typeid"]=="")
{
error.UserMessage("你还没有添加属性,不能为属性添加值");
Response.Redirect("type_admin.aspx",true);
}
else
{
type_id=Request.QueryString["typeid"];
}
if(!Page.IsPostBack)
{
tb_typeid.Text=type_id;
string con=ConfigurationSettings.AppSettings["database_ysdj"];
SqlConnection conn=new SqlConnection(con);//连接字符串
conn.Open();//打开连接 SqlCommand myCommand=new SqlCommand();
myCommand=new SqlCommand("select count(id) from shop_goodstypecontent where goodstypeid='"+type_id+"'",conn);
AspNetPager1.RecordCount=(int)myCommand.ExecuteScalar();
conn.Close();
getdatagrid();
}
}
private void getdatagrid()
{
string con=ConfigurationSettings.AppSettings["database_ysdj"];
SqlConnection conn=new SqlConnection(con);
conn.Open(); string sqlText="select a.goodstype,b.* from shop_goodstype a,shop_goodstypecontent b where b.goodstypeid=a.id and b.goodstypeid='"+type_id+"'"+" order by b.sort";
SqlDataAdapter myCommand=new SqlDataAdapter();
myCommand=new SqlDataAdapter(sqlText,conn);
DataSet ds=new DataSet();
myCommand.Fill(ds,AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"ds_typecontent");
DataGrid1.DataSource=ds.Tables["ds_typecontent"];
DataGrid1.DataBind(); //动态设置用户自定义文本内容
AspNetPager1.CustomInfoText="记录总数:<b>"+AspNetPager1.RecordCount.ToString()+"</b>";
AspNetPager1.CustomInfoText+=" 总页数:<b>"+AspNetPager1.PageCount.ToString()+"</b>";
AspNetPager1.CustomInfoText+=" 当前页:<font color=\"red\"><b>"+AspNetPager1.CurrentPageIndex.ToString()+"</b></font>";
conn.Close();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.AspNetPager1.PageChanged += new Wuqi.Webdiyer.PageChangedEventHandler(this.AspNetPager1_PageChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
{
string con=ConfigurationSettings.AppSettings["database_ysdj"];
SqlConnection conn=new SqlConnection(con);
SqlCommand myCommand=new SqlCommand ();
myCommand.Connection=conn;
myCommand.CommandText ="add_typecontent2";
myCommand.CommandType =CommandType.StoredProcedure;
//输入参数
myCommand.Parameters.Add("@sort",SqlDbType.Int).Value=tb_addsort.Text.Trim();
myCommand.Parameters.Add("@goodstypecontent",SqlDbType.VarChar,50).Value=tb_addtypecontent.Text.Trim();
myCommand.Parameters.Add("@goodstypeid",SqlDbType.Int).Value=Convert.ToInt32(tb_typeid.Text.Trim()); //输出参数
myCommand.Parameters .Add ("@returnkey",SqlDbType.Int );
myCommand.Parameters ["@returnkey"].Direction =ParameterDirection.Output ; try
{
conn.Open();
myCommand.ExecuteNonQuery();
string lsv_returnkey=myCommand.Parameters ["@returnkey"].Value .ToString ();
if(lsv_returnkey=="0")
{
error.UserMessage("已经存在的该属性值的名称");
return;
}
else
{
error.UserMessage ("保存成功");
SqlCommand myCmd=new SqlCommand();
myCmd=new SqlCommand("select count(id) from shop_goodstypecontent where goodstypeid='"+type_id+"'",conn);
AspNetPager1.RecordCount=(int)myCmd.ExecuteScalar();
getdatagrid();
}
}
catch(Exception ex)
{
error.UserMessage (ex.Message .ToString ());
return;
}
conn.Close();
} private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton btn = (LinkButton)e.Item.Cells[6].Controls[0];
//添加当单击时弹出确认对话框的自定义属性
btn.Attributes.Add("onclick", "return confirm('您真的要删除此行吗?');");
btn.Text = "删除";
}
} private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string con=ConfigurationSettings.AppSettings["database_ysdj"];
SqlConnection conn=new SqlConnection(con);
string id=e.Item.Cells[0].Text;
string sqlText="delete from shop_goodstypecontent where id=@id";
SqlCommand myCommand = new SqlCommand(sqlText, conn);
myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
myCommand.Parameters["@id"].Value=Convert.ToInt16(id); try
{
conn.Open();
myCommand.ExecuteNonQuery();
DataGrid1.EditItemIndex=-1;
SqlCommand myCmd=new SqlCommand();
myCmd=new SqlCommand("select count(id) from shop_goodstypecontent where goodstypeid='"+type_id+"'",conn);
AspNetPager1.RecordCount=(int)myCmd.ExecuteScalar();
getdatagrid();
}
catch(Exception ex)
{
error.UserMessage (ex.Message .ToString ());
return;
}
conn.Close();
} private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex ;
getdatagrid();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string con=ConfigurationSettings.AppSettings["database_ysdj"];
SqlConnection conn=new SqlConnection(con);
string typecontent_id=e.Item.Cells[0].Text;
string typecontent_sort=((TextBox)e.Item.FindControl("tb_sort")).Text;//没有变为输入的值
string typecontent_name=((TextBox)e.Item.Cells[4].FindControl("tb_typecontent")).Text;
SqlCommand myCommand=new SqlCommand ();
myCommand.Connection=conn;
myCommand.CommandText ="update_typecontent";
myCommand.CommandType =CommandType.StoredProcedure;
//输入参数
myCommand.Parameters.Add("@id",SqlDbType.Int).Value=Convert.ToInt32(typecontent_id);
myCommand.Parameters.Add("@sort",SqlDbType.Int).Value=Convert.ToInt32(typecontent_sort);
myCommand.Parameters.Add("@goodstypecontent",SqlDbType.VarChar,50).Value=typecontent_name.Trim();
myCommand.Parameters.Add("@goodstypeid",SqlDbType.Int).Value=Convert.ToInt32(type_id); //输出参数
myCommand.Parameters .Add ("@returnkey",SqlDbType.Int );
myCommand.Parameters ["@returnkey"].Direction =ParameterDirection.Output ;
try
{
conn.Open();
myCommand.ExecuteNonQuery();
string lsv_returnkey=myCommand.Parameters ["@returnkey"].Value .ToString ();
if(lsv_returnkey=="0")
{
error.UserMessage("已经存在该商品的属性值名称");
return;
}
else
{
error.UserMessage ("保存成功");
DataGrid1.EditItemIndex=-1;
getdatagrid();
}
}
catch(Exception ex)
{
error.UserMessage (ex.Message .ToString ());
return;
}
conn.Close();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
getdatagrid();
} private void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex=e.NewPageIndex;
getdatagrid();
}
}
}
<%@ Register TagPrefix="webdiyer" Namespace="Wuqi.Webdiyer" Assembly="AspNetPager" %>
<%@ Page language="c#" Codebehind="typecontent_admin.aspx.cs" AutoEventWireup="false" Inherits="ysdj.admin_g_l.typecontent_admin" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>属性信息管理</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="../style/style.css" type="text/css" rel="stylesheet">
</HEAD>
<body bottomMargin="0" bgColor="#f2f4f2" topMargin="0">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" borderColor="#003366" cellSpacing="1" borderColorDark="#ffffff" cellPadding="1"
width="100%" border="1">
<TR>
<TD align="center">管理编号为: <b><font color="#6699ff">
<%=type_id%>
</font></b>的属性值
<hr color="#cccccc" SIZE="2">
<FONT face="宋体">
<asp:datagrid id="DataGrid1" runat="server" BorderWidth="1" AutoGenerateColumns="False" BorderColor="#336699"
Width="99%">
<HeaderStyle BackColor="DarkGray"></HeaderStyle>
<EditItemStyle ForeColor="#ff0000"></EditItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<Columns>
<asp:BoundColumn DataField="id" Visible="False" HeaderText="id" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="goodstypeid" HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#ffffff"
HeaderText="属性编号" ReadOnly="True" HeaderStyle-Width="60"></asp:BoundColumn>
<asp:BoundColumn DataField="goodstype" HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#ffffff"
HeaderText="属性名称" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderStyle-Font-Bold="True" HeaderText="排序" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="#ffffff" HeaderStyle-Width="40">
<ItemTemplate>
<asp:Label ID="lb_sort" Runat="server">
<%# DataBinder.Eval(Container.DataItem, "sort")%>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb_sort" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "sort")%>'>
</asp:TextBox>
<asp:RegularExpressionValidator id="RegularExpressionValidator1" ControlToValidate="tb_sort" ValidationExpression="[0-9]+"
Display="Dynamic" Font-Name="Arial" Font-Size="11" runat="server"> *必须是数字</asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="扩展属性值" HeaderStyle-ForeColor="#ffffff" HeaderStyle-Font-Bold="True"
HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lb_typecontent" Runat="server">
<%# DataBinder.Eval(Container.DataItem, "goodstypecontent")%>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb_typecontent" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "goodstypecontent")%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="保存" HeaderText="编 辑" HeaderStyle-ForeColor="#ffffff"
HeaderStyle-Font-Bold="True" HeaderStyle-Width="80" HeaderStyle-HorizontalAlign="Center" CancelText="取消"
EditText="编辑" ItemStyle-HorizontalAlign="Center"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" HeaderText="删除" CommandName="Delete" HeaderStyle-ForeColor="#ffffff" ItemStyle-HorizontalAlign="Center">
<HeaderStyle Width="60" HorizontalAlign="Center" Font-Bold="True"></HeaderStyle>
</asp:ButtonColumn>
</Columns>
</asp:datagrid><br>
<webdiyer:aspnetpager id="AspNetPager1" runat="server" UrlPaging="True" ShowCustomInfoSection="Left" ShowInputBox="Always"
PageSize="1"></webdiyer:aspnetpager></FONT></TD>
</TR>
<TR>
<TD><FONT face="宋体"></FONT></TD>
</TR>
<tr>
<td>
<h6 style="FONT-SIZE: 8pt; COLOR: navy">添加新的扩展属性:</h6>
<TABLE id="Table2" borderColor="#ccccc3" cellSpacing="1" borderColorDark="#ffffff" cellPadding="1"
width="99%" align="center" border="1">
<TR>
<TD><FONT face="宋体">扩展属性编号:<asp:textbox id="tb_typeid" runat="server" ReadOnly="True"></asp:textbox><font color="#ff0000">*属性编号为不能更改的项</font><br>
排序:
<asp:textbox id="tb_addsort" runat="server" Width="88px"></asp:textbox><asp:regularexpressionvalidator id="Regularexpressionvalidator2" runat="server" Font-Size="11" Font-Name="Arial"
Display="Dynamic" ValidationExpression="[0-9]+" ControlToValidate="tb_addsort">
*必须是数字</asp:regularexpressionvalidator><br>
扩展属性值名称:
<asp:textbox id="tb_addtypecontent" runat="server"></asp:textbox><asp:button id="Button2" runat="server" Text="添 加"></asp:button></FONT></TD>
</TR>
</TABLE>
</td>
</tr>
</TABLE>
</form>
</body>
</HTML>