detailsview的更新 和 gridview的操作 detailsview的更新 protected void detailsview1_itemupdating(object sender,
detailsviewupdateeventargs e)
{
string tableid = url;
string tablename = tbname;
string m_monthcount;
string m_messagecount;
string m_remark;
string sqlupdate; textbox tb;
tb = (textbox)(detailsview1.rows[1].findcontrol("textbox2"));
m_monthcount = tb.text; tb = (textbox)(detailsview1.rows[2].findcontrol("textbox3"));
m_messagecount = tb.text; tb = (textbox)(detailsview1.rows[3].findcontrol("textbox4"));
m_remark = tb.text; sqlupdate = "update " + tablename + " set m_monthcount = '" + m_monthcount + "',m_messagecount = '" + m_messagecount + "', m_remark = '" + m_remark + "' where m_id = '" + tableid + "'"; sqlconnection con = db.createsqlcon();
con.open();
sqlcommand cmd = new sqlcommand(sqlupdate,con);
cmd.executenonquery();
con.close();////////////////////////////////////////設定detailsview預設模式為編輯模式////////////////////////////////////////////////////<asp教程:detailsview id="detailsview1" runat="server" height="55px" width="370px" autogeneraterows="false" cellpadding="4" forecolor="#333333" gridlines="none" onitemupdating="detailsview1_itemupdating" defaultmode="edit">
<fields>
<asp:templatefield headertext="公司名稱"> <edititemtemplate>
<asp:textbox id="textbox1" runat="server" width="200px" readonly="true" text='<%# bind("m_name") %>'></asp:textbox>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="總條數">
<edititemtemplate>
<asp:textbox id="textbox2" runat="server" width="200px" text='<%# bind("m_monthcount") %>'></asp:textbox>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="當前發送">
<edititemtemplate>
<asp:textbox id="textbox3" width="200px" runat="server" text='<%# bind("m_messagecount") %>'></asp:textbox>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="備忘"> <edititemtemplate>
<asp:textbox id="textbox4" runat="server" height="121px" text='<%# bind("m_remark") %>'
textmode="multiline" width="200px"></asp:textbox>
</edititemtemplate>
</asp:templatefield>
<asp:buttonfield buttontype="button" commandname="update" text="按鈕">
<headerstyle horizontalalign="center" />
</asp:buttonfield>
</fields>
<footerstyle backcolor="#507cd1" font-bold="true" forecolor="white" />
<commandrowstyle backcolor="#d1ddf1" font-bold="true" />
<editrowstyle backcolor="#2461bf" />
<rowstyle backcolor="#eff3fb" />
<pagerstyle backcolor="#2461bf" forecolor="white" horizontalalign="center" />
<fieldheaderstyle backcolor="#dee8f5" font-bold="true" />
<headerstyle backcolor="#507cd1" font-bold="true" forecolor="white" />
<alternatingrowstyle backcolor="white" />
</asp:detailsview> ////////////////////////////////////////////////gridview的操作////////////////////////////////////////////////////////////////////////*************取消編輯***************/ protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e)
{
gridview1.editindex = -1;
datagridviewbind1();
}/*******************編輯***********************/
protected void gridview1_rowediting(object sender, gridviewediteventargs e)
{
gridview1.editindex = e.neweditindex;
datagridviewbind1();
}/*********************更新操作******************************/
protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e)
{
string updatacmd = "update " + sqldatatime + " set m_monthcount='"
+ ((textbox)(gridview1.rows[e.rowindex].cells[1].controls[0])).text.tostring().trim() + "',m_messagecount='"
+ ((textbox)(gridview1.rows[e.rowindex].cells[2].controls[0])).text.tostring().trim() + "',m_remark='"
+ ((textbox)(gridview1.rows[e.rowindex].cells[3].controls[0])).text.tostring().trim() + "' where m_id='"
+ gridview1.datakeys[e.rowindex].value.tostring() + "'";
sqlconnection con = db.createsqlcon();
sqlcommand cmd = new sqlcommand(updatacmd, con); con.open();
cmd.executenonquery();
gridview1.editindex = -1;
datagridviewbind1();
con.close();
}
下面看看關於asp.net教程中detailsview的使用方法
<asp:detailsview id="detailsview1" runat="server" autogeneraterows="false" height="50px"
width="500px" onmodechanging="detailsview1_modechanging" onitemdeleting="detailsview1_itemdeleting"
onitemupdating="detailsview1_itemupdating" oniteminserting="detailsview1_iteminserting">
<fields>
<asp:templatefield headertext="id">
<itemtemplate>
<%#eval("id") %>
</itemtemplate>
<insertitemtemplate>
<asp:textbox id="txtinsertid" text="insertid" runat="server"/>
</insertitemtemplate>
<edititemtemplate>
<%#eval("id") %>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="title">
<itemtemplate>
<%#eval("title") %>
</itemtemplate>
<insertitemtemplate>
<asp:textbox id="txtinserttitle" text="inserttitle" runat="server"/>
</insertitemtemplate>
<edititemtemplate>
<asp:textbox id="txtedittitle" text='<%# eval("title") %>' runat="server"/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="context">
<itemtemplate>
<%# eval("logcontext") %>
</itemtemplate>
<insertitemtemplate>
<asp:textbox id="txtinsertlogcontext" text="insertlogcontext" runat="server"/>
</insertitemtemplate>
<edititemtemplate>
<asp:textbox id="txteditlogcontext" text='<%# eval("logcontext") %>' runat="server"/>
</edititemtemplate>
</asp:templatefield>
<asp:templatefield headertext="操作">
<itemtemplate>
<asp:button id="btnedit" runat="server" causesvalidation="false"
commandname="edit" text="編輯" />
<asp:button id="btnnew" runat="server" causesvalidation="false"
commandname="new" text="建立" />
<asp:button id="btndelete" runat="server" causesvalidation="false"
commandname="delete" text="刪除" onclientclick="return confirm('確定要更新該學生資訊嗎?');" />
</itemtemplate>
<insertitemtemplate>
<asp:button id="btninsert" runat="server" causesvalidation="true"
commandname="insert" text="插入" />
<asp:button id="btncancel" runat="server" causesvalidation="false"
commandname="cancel" text="取消" />
</insertitemtemplate>
<edititemtemplate>
<asp:button id="btnupdate" runat="server" causesvalidation="true"
commandname="update" text="更新" onclientclick="return confirm('確定要更新該學生資訊嗎?');" />
<asp:button id="btncancel2" runat="server" causesvalidation="false"
commandname="cancel" text="取消" />
</edititemtemplate>
</asp:templatefield>
</fields>
</asp:detailsview>