datagrid1中,如果使用模版的话,选择datagrid1的updateCommand,然后设立主键 DataGrid1.DataKeys(e.Item.ItemIndex) 在datagrid1.datakeyField设置关键字段 然后申明引用一个textbox控件 dim textBoxName as textbox 然后找到你datagrid1中的这个控件 textBoxName = e.Item.FindControl("textbox2") 这样就找到控件了 然后用sqlCommand对象,就可以更新了 更新他用textBoxName.text.trim---这个就是他的值了我也是初学,应该就是这样
alasunny(阿拉) 我知道你的意思啊,但是我也是这样写的阿,可是我获取不了用户更改后的值啊
你是不是在page_load的时候从数据库里取值绑定在控件上,然后点按钮引发事件,把页面上控件内改变过的值更新到数据库?如果是这样的话,点击按钮产生回调,画面生成的时候还是会从数据库里取值绑定到控件,把你填写的内容覆盖了,所以从数据库里取默认值绑定到控件要写在 if (!Page.PostBack){从数据库取数据绑定到控件} 里.
SqlConnection conn = new SqlConnection(strConnection)); SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection) conn); SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand ); SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
<asp:TemplateColumn HeaderText="Submitted By"> <ItemTemplate> <asp:TextBox runat="server" id="txtSubmittedBy" Text='<%# Container.DataItem("SubmittedByName") %>' /> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid> 之后,对DATAGRID进行一次遍历, Dim myConnection as New SqlConnection(connection string) Dim myCommand as New SqlCommand(strSQL, myConnection)Dim dgi as DataGridItem For Each dgi in dgPopularFAQs.Items 'Read in the Primary Key Field Dim id as Integer = Convert.ToInt32(dgPopularFAQs.DataKeys(dgi.ItemIndex)) Dim question as String = CType(dgi.FindControl("txtDescription"), TextBox).Text Dim submittedBy as String = CType(dgi.FindControl("txtSubmittedBy"), TextBox).Text
'Issue an UPDATE statement... Dim updateSQL as String = "UPDATE TableName SET Question = @Question, " & _ "SubmittedByName = @SubmittedByName WHERE FAQID = @ID" myCommand.Parameters.Clear() myCommand.Parameters.Add("@Question", question) myCommand.Parameters.Add("@SubmittedByName", submittedBy)
是的
事件中加上
if(!IsPostBack)
{
}
估计就ok了
DataGrid1.DataKeys(e.Item.ItemIndex)
在datagrid1.datakeyField设置关键字段
然后申明引用一个textbox控件
dim textBoxName as textbox
然后找到你datagrid1中的这个控件
textBoxName = e.Item.FindControl("textbox2")
这样就找到控件了
然后用sqlCommand对象,就可以更新了
更新他用textBoxName.text.trim---这个就是他的值了我也是初学,应该就是这样
if (!Page.PostBack){从数据库取数据绑定到控件} 里.
在asp.net中,页面打开,你点编辑,此时就发生了很多事件了,这个时候,我们得到了现在datagrid里面的控件值了,你不产生提交事件,如何获得人家输入的值??
if(!IsPostBack)
{
绑定数据;
}
在更新完毕后还要重新绑定数据!
private void Page_Load(object sender, System.EventArgs e)
{
BindGrid();
}private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
_client._ClientID=Convert.ToInt32(((Label)(e.Item.Cells[0].Controls[1])).Text);
Message(((Label)(e.Item.Cells[0].Controls[1])).Text);
_client._ClientLeading = ((CheckBox) (e.Item .Cells[1].Controls[1])).Checked ? 1 : 0;
// Message(e.Item.Cells[4].Text);
// Message(e.Item.Cells[5].Text);
// Message(e.Item.Cells[6].Text);
//_client._ClientName = ((TextBox) e.Item.Cells[1].Controls[2]).Text.Trim();
// TextBox t;
// t = (TextBox) e.Item.Cells[4].Controls[1];
// string str = t.Text;
// Message(str);
//Message(((TextBox) e.Item.Cells[1].FindControl("TextBox1")).Text.Trim());
_client._ClientDepartment = ((TextBox) e.Item.Cells[2].Controls[1]).Text;
//Message(((TextBox) e.Item.Cells[2].Controls[1]).Text);
_client._ClientEmail = ((TextBox) e.Item.Cells[3].Controls[1]).Text;
//Message(((TextBox) e.Item.Cells[3].Controls[1]).Text);
_client._ClientTelphone = ((TextBox) e.Item.Cells[4].Controls[1]).Text;
//Message(((TextBox) e.Item.Cells[4].Controls[1]).Text);
_client._ClientLevel= ((DropDownList) e.Item.Cells[5].Controls[1]).DataTextField.Trim();
//Message(((DropDownList) e.Item.Cells[5].Controls[1]).SelectedItem.Text.Trim());
_client._State= ((DropDownList) e.Item.Cells[6].Controls[1]).DataTextField.Trim();
//Message(((DropDownList) e.Item.Cells[6].Controls[1]).SelectedItem.Text.Trim());
_client._SellerName= ((TextBox) e.Item.Cells[7].Controls[1]).Text;
//Message(((TextBox) e.Item.Cells[7].Controls[1]).Text);
ChangeClient _changeclient = new ChangeClient();
if(_changeclient.Update(_client))
{
Message("更新成功");
}
else
{
Message("更新失败");
}
this.DataGrid1.EditItemIndex = -1;
TreeToBindGrid();
}
#endregion
dr["test"] = "";
dt.Rows.Add(dr);
{
_client._ClientID=Convert.ToInt32(((Label)(e.Item.Cells[0].Controls[1])).Text);
Message(((Label)(e.Item.Cells[0].Controls[1])).Text);
_client._ClientLeading = ((CheckBox) (e.Item .Cells[1].Controls[1])).Checked ? 1 : 0;
//Message(e.Item.Cells[4].Text);
//Message(e.Item.Cells[5].Text);
//Message(e.Item.Cells[6].Text);
//_client._ClientName = ((TextBox) e.Item.Cells[1].Controls[2]).Text.Trim();
//TextBox t;
//t = (TextBox) e.Item.Cells[4].Controls[1];
//string str = t.Text;
//Message(str);
//Message(((TextBox) e.Item.Cells[1].FindControl("TextBox1")).Text.Trim());
_client._ClientDepartment = ((TextBox) e.Item.Cells[2].Controls[1]).Text;
//Message(((TextBox) e.Item.Cells[2].Controls[1]).Text);
_client._ClientEmail = ((TextBox) e.Item.Cells[3].Controls[1]).Text;
//Message(((TextBox) e.Item.Cells[3].Controls[1]).Text);
_client._ClientTelphone = ((TextBox) e.Item.Cells[4].Controls[1]).Text;
//Message(((TextBox) e.Item.Cells[4].Controls[1]).Text);
_client._ClientLevel= ((DropDownList) e.Item.Cells[5].Controls[1]).DataTextField.Trim();
//Message(((DropDownList) e.Item.Cells[5].Controls[1]).SelectedItem.Text.Trim());
_client._State= ((DropDownList) e.Item.Cells[6].Controls[1]).DataTextField.Trim();
//Message(((DropDownList) e.Item.Cells[6].Controls[1]).SelectedItem.Text.Trim());
_client._SellerName= ((TextBox) e.Item.Cells[7].Controls[1]).Text;
//Message(((TextBox) e.Item.Cells[7].Controls[1]).Text);
ChangeClient _changeclient = new ChangeClient();
if(_changeclient.Update(_client))
{
Message("更新成功");
}
else
{
Message("更新失败");
}
this.DataGrid1.EditItemIndex = -1;
TreeToBindGrid();
}
#endregion
糟糕了,看不懂,我只能看看vb的
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}}
{
if(!IsPostBack)
BindGrid();
}
这种问题你调试下就知道怎么办了阿;呵呵
看来楼主不明白 IsPostBack 的用处
_client._ClientID=Convert.ToInt32(((Label)(e.Item.Cells[0].Controls[0])).Text);
试试啊 ,全部改成Controls[0]
----------
改这个有什么用哩 第3列当然就是Controls[2]
获取不了用户更改后的值
----------
这个问题本周我也碰到了!!!-_-原来写的时候能读到的 本周重写就不行了 或许是有些小细节没注意到吧
{
绑定DataGrid
}
错误啊
if(!IsPostBack)
{
}
{
绑定DataGrid
}肯定是触发更新时POSTBACK了,然后又把数据RELOAD一遍了.其它的数据能更新吗???如果不能的话,那么就试一下这个吧.放在PAGE.LOAD事件下面.
{
}绝对是这个问题。。我肯定。。
public DataSetUpdateByDataSet(DataSet ds,string strTblName,string strConnection)
{
SqlConnection conn = new SqlConnection(strConnection));
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection) conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
try { lock(this) //处理并发情况(分布式情况) { myAdapter.Update(ds,strTblName); } }
catch(Exception err)
{ conn.Close();
throw new BusinessException(err);
} return ds; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds}或public DataSet UpdateByDataSet(DataSet ds,string strTblName,string strConnection)
{
SqlConnection conn = new SqlConnection(strConnection));
SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection) conn); SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand ); SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.InsertCommand = myCommandBuilder .GetInsertCommand(); myAdapter.UpdateCommand = myCommandBuilder .GetUpdateCommand(); myAdapter.DeleteCommand = myCommandBuilder .GetDeleteCommand(); try { lock(this) //处理并发情况(分布式情况) { conn.Open(); myAdapter.Update(ds,strTblName); conn.Close(); }
return ds; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds }
catch(Exception err)
{ conn.Close();
throw new BusinessException(err);
}
}直接调用这两个方法的任意一个就OK啦,说明的一点是select * from "+strTblName是一定要的,
作用大家也应该想到了,主要是告诉 SqlDataAdapter更新哪个表
要对DATAGRID中要更新的列建立模版列,比如:
asp:datagrid id="dgPopularFAQs" runat="server"
AutoGenerateColumns="False"
...> <Columns>
<asp:BoundColumn DataField="FAQID" ItemStyle-Width="10%"
ItemStyle-HorizontalAlign="Center" HeaderText="FAQ ID" /> <asp:BoundColumn DataField="CategoryName" HeaderText="Category" />
<asp:TemplateColumn HeaderText="Question">
<ItemTemplate>
<asp:TextBox runat="server" id="txtDescription" Columns="75"
Text='<%# Container.DataItem("Description") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Submitted By">
<ItemTemplate>
<asp:TextBox runat="server" id="txtSubmittedBy"
Text='<%# Container.DataItem("SubmittedByName") %>' />
</ItemTemplate>
</asp:TemplateColumn> </Columns>
</asp:datagrid>
之后,对DATAGRID进行一次遍历,
Dim myConnection as New SqlConnection(connection string)
Dim myCommand as New SqlCommand(strSQL, myConnection)Dim dgi as DataGridItem
For Each dgi in dgPopularFAQs.Items
'Read in the Primary Key Field
Dim id as Integer = Convert.ToInt32(dgPopularFAQs.DataKeys(dgi.ItemIndex))
Dim question as String = CType(dgi.FindControl("txtDescription"), TextBox).Text
Dim submittedBy as String = CType(dgi.FindControl("txtSubmittedBy"), TextBox).Text
'Issue an UPDATE statement...
Dim updateSQL as String = "UPDATE TableName SET Question = @Question, " & _
"SubmittedByName = @SubmittedByName WHERE FAQID = @ID"
myCommand.Parameters.Clear()
myCommand.Parameters.Add("@Question", question)
myCommand.Parameters.Add("@SubmittedByName", submittedBy)
myCommand.ExecuteNonQuery()
Next
我的情况是
第一次 把ID字段设成了不显示,然后e.Item.Cells[0]里面就找不到任何的Controls
第二次 把ID字段设成了显示、只读,然后e.Item.Cells[0].Text能获得ID