ASP.NET2.0:通過SqlDataSource綁定資料到普通控制項

來源:互聯網
上載者:User

ASP.NET 2.0中一些新的Data控制項,如GridView,DetailsView,FormView,當它們與SqlDataSource綁定後,可以直接實現添加、修改、刪除操作,並不需要編碼。也就是說這些控制項內建地會調用SqlDataSource對應的方法實現對應的操作。但某些條件下並不適合使用這些控制項,而需要用普通的控制項。比如有時會用DataList,或者Reapter,有時只能用幾個TextBox來實現資料的輸入維護,比如要插入一條記錄到一個資料庫表中,但是在介面展現上這條資料的所有欄位並不是聚集在一起的,比如一個頁面的頭部有幾個欄位,尾部有幾個欄位,中間有些其他的控制項,對應的是其他的資料。這種情況下就很難用DetailsView控制項或者FormView控制項,而只能我們自己來擺控制項了。這樣的話,我們當然可以不使用SqlDataSource控制項,而按照常規的方法,比如一個方法返回一個對象,在把每個textbox綁定到這個對象。使用SqlDataSource的好處就是可以減少代碼量,而且ASP.NET 2.0中都是通過DataSouce控制項來進行資料操作的,所以還是盡量用的好。用SqlDataSource綁定普通控制項,雖然還是需要一定的代碼量,但相對還是少很多了。

下面的幾個代碼片斷可以放映如何使用sqldatasource綁定普通資料控制項:

1. 綁定textbox,展現

            DataSourceSelectArguments ar = new DataSourceSelectArguments();
            DataView dv = (DataView)SqlDataSource1.Select(ar);
            this.txtClosingParagraph.Text = dv.Table.Rows[0]["ClosingParagraph"].ToString();
            this.txtOpeningParagraph.Text = dv.Table.Rows[0]["OpeningParagraph"].ToString();
            this.txtCreatedDate.Text = dv.Table.Rows[0]["CreatedDate"] == DBNull.Value ? string.Empty : ((DateTime)dv.Table.Rows[0]["CreatedDate"]).ToShortDateString();
            this.txtCreatedBy.Text = dv.Table.Rows[0]["CreatedBy"].ToString();
            this.txtDisclaimer.Text = dv.Table.Rows[0]["Disclaimer"].ToString();
            this.txtOpeningParagraph.Text = dv.Table.Rows[0]["OpeningParagraph"].ToString();
            this.txtQuoteAttn.Text = dv.Table.Rows[0]["QuoteAttn"].ToString();
            this.txtQuoteNo.Text = isNew ? DateTime.Now.Ticks.ToString() : dv.Table.Rows[0]["QuoteNo"].ToString();
            this.txtQuoteOwner.Text = dv.Table.Rows[0]["QuoteOwner"].ToString();
            this.txtQuoteTo.Text = dv.Table.Rows[0]["QuoteTo"].ToString();
            this.txtSubject.Text = dv.Table.Rows[0]["Subject"].ToString();
            this.txtTemplateID.Text = dv.Table.Rows[0]["TemplateID"].ToString();

 

2. 綁定到DataList,插入

            SqlDataSource data = (SqlDataSource)grid.Parent.FindControl("SqlDataSource3");
            data.InsertParameters["QuoteItemSurchargeID"].DefaultValue = Guid.NewGuid().ToString();
            data.InsertParameters["QuoteItemID"].DefaultValue = data.SelectParameters[0].DefaultValue;
            data.InsertParameters["TariffName"].DefaultValue = ((TextBox)grid.FooterRow.FindControl("txtTariffName")).Text.Trim();
            data.InsertParameters["Rate20"].DefaultValue = ((TextBox)grid.FooterRow.FindControl("txtRate20")).Text.Trim();
            data.InsertParameters["Rate40"].DefaultValue = ((TextBox)grid.FooterRow.FindControl("txtRate40")).Text.Trim();
            data.InsertParameters["Rate40HC"].DefaultValue = ((TextBox)grid.FooterRow.FindControl("txtRate40HC")).Text.Trim();
            data.InsertParameters["Rate45"].DefaultValue = ((TextBox)grid.FooterRow.FindControl("txtRate45")).Text.Trim();
            data.InsertParameters["CreatedDate"].DefaultValue = DateTime.Now.ToString();
            data.Insert();
            this.DataList1.DataBind();

3. 綁定到DataList,更新

        SqlDataSource2.UpdateParameters["Origin"].DefaultValue = ((TextBox)e.Item.FindControl("txtOrigin")).Text.Trim();
        SqlDataSource2.UpdateParameters["Destination"].DefaultValue = ((TextBox)e.Item.FindControl("txtDestination")).Text.Trim();
        SqlDataSource2.UpdateParameters["Rate20"].DefaultValue = ((TextBox)e.Item.FindControl("txtRate20")).Text.Trim();
        SqlDataSource2.UpdateParameters["Rate40"].DefaultValue = ((TextBox)e.Item.FindControl("txtRate40")).Text.Trim();
        SqlDataSource2.UpdateParameters["Rate40HC"].DefaultValue = ((TextBox)e.Item.FindControl("txtRate40HC")).Text.Trim();
        SqlDataSource2.UpdateParameters["Rate45"].DefaultValue = ((TextBox)e.Item.FindControl("txtRate45")).Text.Trim();
        SqlDataSource2.UpdateParameters["ETDDate"].DefaultValue = ((TextBox)e.Item.FindControl("txtETDDate")).Text.Trim();
        SqlDataSource2.UpdateParameters["Carrier"].DefaultValue = ((TextBox)e.Item.FindControl("txtCarrier")).Text.Trim();
        SqlDataSource2.UpdateParameters["TT"].DefaultValue = ((TextBox)e.Item.FindControl("txtTT")).Text.Trim();
        SqlDataSource2.UpdateParameters["QuoteItemID"].DefaultValue = (DataList1.DataKeys[e.Item.ItemIndex]).ToString();
        SqlDataSource2.Update();
        DataList1.EditItemIndex = -1;
        DataList1.DataBind();

實際上主要的還是手動調用SqlDataSource的Select() Inert() Update()方法。關於其他的細節請查看msdn SqlDataSource對應的方法,msdn都有具體的實現執行個體。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.