我用 dataGrid绑订了数据库的字段,点击编辑后出现文本框,我更改后击发了updata事件,但取文本框中的值还是原来的值
例如
TextBox t = (TextBox)e.item.findctrol("tt");或
TextBox t = (TextBox)e.cells(1).ctrols(0);
string temp =t.text;
temp 还是原来的值。
以上两种方法我都试了,都是取得原来数据库的值。
怎样才能取得你修改后的值呢(既当前值)?我用的是c#(asp.net)写的!
例如
TextBox t = (TextBox)e.item.findctrol("tt");或
TextBox t = (TextBox)e.cells(1).ctrols(0);
string temp =t.text;
temp 还是原来的值。
以上两种方法我都试了,都是取得原来数据库的值。
怎样才能取得你修改后的值呢(既当前值)?我用的是c#(asp.net)写的!
解决方案 »
- asp.net服务器端取js弹出层中dropdownlist的选中值???
- 在asp.net调用oracle的procedure 且procedure的out变量是record的
- 跪求:asp.net treeview中的图标和文字的显示位置问题?
- Gridview在后台给Datafile赋值的问题
- 两个表关联查询后,绑定到Datagrid,但绑定结果不正确。如何解决?
- 怎么实现保存后再提示再做某些事情?
- 散分:大家讨论一下是否有必要在开发时拒绝IE以外的浏览器?
- asp.net網站發布的問題,請好心的大哥們幫忙看看.
- 急死人了,帮帮忙吧
- 求救,简单问题
- 求助:网页中table控件的使用方法
- 求高手指点向DataGrid Web服务器控件添加超链接列的问题
if(!IsPostBack)
{
///这里写你拌定数据库的代码
}
界面:
<form id="DataGrade" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 205px; POSITION: absolute; TOP: 136px" runat="server" AutoGenerateColumns="False" Width="219px" OnSortCommand="SortRows" AllowSorting="True" AllowPaging="True" OnPageIndexChanged="ChangeGridPage" Height="307px" OnEditCommand="DoItemEdit" OnUpdateCommand="DoItemUpdate" OnCancelCommand="DoItemCancel" DataKeyField="emp_id">
<SelectedItemStyle Font-Size="X-Small"></SelectedItemStyle>
<AlternatingItemStyle Font-Size="X-Small"></AlternatingItemStyle>
<ItemStyle Font-Size="X-Small"></ItemStyle>
<HeaderStyle Font-Size="X-Small"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="emp_id" ReadOnly="True" HeaderText="emp_id"></asp:BoundColumn>
<asp:BoundColumn DataField="fname" HeaderText="fname"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="lname">
<ItemStyle Font-Size="X-Small"></ItemStyle>
<ItemTemplate>
<asp:Label text='<%# DataBinder.Eval(Container.DataItem,"lname") %>' Runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLname" Width="60" Text='<%# DataBinder.Eval(Container.DataItem,"lname") %>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="ÐÞ¸Ä/±£´æ" CancelText="È¡Ïû" EditText="±à¼­"></asp:EditCommandColumn>
</Columns>
</asp:datagrid><asp:textbox id="TextBox1" style="Z-INDEX: 102; LEFT: 207px; POSITION: absolute; TOP: 96px" runat="server" Width="102px"></asp:textbox></form>
实现
public class DataGrade : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public string strsort="";
private void Page_Load(object sender, System.EventArgs e)
{
// ÔÚ´Ë´¦·ÅÖÃÓû§´úÂëÒÔ³õʼ»¯Ò³Ãæ
if(!Page.IsPostBack)
{
BindDataGrid();
}
}
public void BindDataGrid()
{
string sqlConn = "server=localhost;uid=sa;pwd=fishbaby;database=yuyc";
string sqlSelect = "select * from employee";
SqlConnection conn = new SqlConnection(sqlConn);
try
{
conn.Open();
//SqlCommand comm = new SqlCommand(sqlSelect,conn);
SqlDataAdapter adapter = new SqlDataAdapter(sqlSelect,conn);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet,"employee");
//SqlDataReader dataReader = comm.ExecuteReader();
DataView dataView = new DataView(dataSet.Tables["employee"]);
//dataView.Sort=TextBox1.Text;
DataGrid1.PageSize=4;
DataGrid1.DataSource=dataView;
DataGrid1.DataBind();
}
catch(Exception ex)
{
ex.ToString();
}
finally
{
if(conn != null)
{ //
conn.Close();
//conn = null; } }
}
public void DoItemUpdate(object sender,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(Page.IsValid)
{
TextBox txtFname = new TextBox();
TextBox txtLname = new TextBox();
txtLname= (TextBox)e.Item.FindControl("txtLname");
//txtFname = (TextBox)e.Item.Cells[2].Controls[0];
string strTxt = txtLname.Text;
string strFname = txtFname.Text;
string strsql = "update employee set lname='"+txtLname.Text+"' where emp_id='"+ DataGrid1.DataKeys[e.Item.ItemIndex] +"'";
string sqlConn = "server=localhost;uid=sa;pwd=fishbaby;database=yuyc";
SqlConnection conn = new SqlConnection(sqlConn);
conn.Open();
SqlCommand comm = new SqlCommand(strsql,conn);
int i = comm.ExecuteNonQuery();
if(i>0)
{
DataGrid1.EditItemIndex = -1;
BindDataGrid();
}
else
{
TextBox1.Text = "¸üÐÂʧ°Ü£¡";
}
}
//SqlDataReader dataReader = comm.ExecuteReader();
}
public void DoItemCancel(object sender,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindDataGrid();
}
public void DoItemEdit(object sender,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex =(int)e.Item.ItemIndex;
BindDataGrid();
}
public void ChangeGridPage(object sender,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindDataGrid();
}
public void SortRows(object sender,System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
strsort = e.SortExpression.ToString();
BindDataGrid();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN£º¸Ãµ÷ÓÃÊÇ ASP.NET Web ´°ÌåÉè¼ÆÆ÷Ëù±ØÐèµÄ¡£
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Éè¼ÆÆ÷Ö§³ÖËùÐèµÄ·½·¨ - ²»ÒªÊ¹ÓôúÂë±à¼­Æ÷ÐÞ¸Ä
/// ´Ë·½·¨µÄÄÚÈÝ¡£
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.ChangeGridPage);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DoItemCancel);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DoItemEdit);
this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.SortRows);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DoItemUpdate);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
请大家帮我看看啊!