asp.net detailsview 更新與使用方法

來源:互聯網
上載者:User

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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.