2009-03-04 27 views
0

我要實現以下情形......DetailsView控件:插入/更新單個記錄

表與此相關的問題如下:

簡檔INT(標識) 名字爲varchar(100) 姓氏varchar(100)

第1步。用戶訪問profiles.aspx頁面。在網格視圖中,他可以看到其他人的個人資料。 (我知道如何做到這一點。)

第2步。用戶訪問MyProfile.aspx頁面。由於他的配置文件不存在,所以詳細信息視圖在啓用插入按鈕時是空白的。用戶單擊插入按鈕,他只能添加他自己的配置文件。 (我不知道如何做到這一點)。

第3步。添加用戶配置文件後,它會顯示在profiles.aspx頁面上。他想更新自己的個人資料。他導航到MyProfile.aspx使用ProfileID的頁面可以說33. DetailsView能夠根據profileid啓用他的配置文件並啓用更新按鈕。 (我不知道如何做到這一點。)

有人可以幫我第2步和第3步。我不知道如何設置sqldatasource和detailsview來實現這一點。

預先感謝您。

回答

0

好的,我會給你一個簡單的解決方案,你可以改變以適應你的需求。

對於第2步: 您創建一個名爲create.aspx的頁面;在該頁面中,您將所有需要的文本框和用於註冊的按鈕。在該按鈕的點擊事件上寫下如下內容

//using System.Data; 
//using System.Data.Sql; you have to import these 

//Put connection string 
SqlConnection sqlcon = new SqlConnection("your connection string here which will be able to find in MSDN"); 
sql.Open(); 
SqlCommand sqlcmd = new SqlCommand("INSERT INTO Statement which easily find in MSDN too",sqlcon); 
sqlcmd.ExecuteNoneQuery(); 
sql.Close(); 

哪個代碼可以插入數據庫,無論你想要什麼。在INSERT INTO字符串中,你連接你的輸入,但要非常小心,可能會導致SqlInjection錯誤,這是非常危險的(請閱讀關於它的文章)。

對於第3步: 一切都是一樣的,除了放置「INSERT ....」語句,您必須使用可以在MSDN上找到的「UPDATE ...」語句。

Regards, Pooria。

0

見下例........ 源文件的.aspx 我使用的GridView和DetailsView插入,更新,編輯truckmaster表。 執行以下步驟: 1.將gridview.assign datakeynames作爲您的唯一列從table中取出。您可以爲每個字段寫入boundfields。事實上,這是更好的方法。 2.採取數據源控制。綁定到您的表。 3.將這個數據源綁定到Gridview,一旦你通過下面的代碼,你會更好地理解這個步驟。 4.獲取詳細信息視圖。將datakeynames分配爲表中的唯一列。 5.取另一個數據源。將其綁定到相同的table.declare insertcommand,selectcommand。 聲明選擇參數,updateparameter如下面的代碼。 希望它能幫助你。

<asp:GridView id     ="gridTruckMaster" 
      runat    ="server" 
      CssClass   ="GridView" 
      DataKeyNames  ="TruckType" 
      DataSourceID  ="SqlDataSource1" 
      AutoGenerateColumns ="False" 
      ShowFooter   ="True" 
      AllowPaging   ="True"> 
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></FooterStyle> 
<RowStyle VerticalAlign="Top" BackColor="#F7F6F3" ForeColor="#333333"></RowStyle> 
<Columns> 
<asp:BoundField DataField="TruckType"  HeaderText="TruckType"  SortExpression="TruckType"></asp:BoundField> 
<asp:BoundField DataField="TruckCapacity" HeaderText="TruckCapacity" SortExpression="TruckCapacity"></asp:BoundField> 
<asp:CommandField ShowSelectButton="True"></asp:CommandField> 
</Columns> 
<PagerStyle  BackColor="#284775" HorizontalAlign="Center" ForeColor="White"></PagerStyle> 
<SelectedRowStyle BackColor="#669999" Font-Bold="True"   ForeColor="White"></SelectedRowStyle> 
<HeaderStyle  BackColor="#5D7B9D" Font-Bold="True"   ForeColor="White"></HeaderStyle> 
</asp:GridView> 

<asp:SqlDataSource ID    ="SqlDataSource1" 
        runat   ="server" 
        ConnectionString ="<%$ ConnectionStrings:GodrejReporterConnectionString %>" 
        SelectCommand ="SELECT [TruckType], [TruckCapacity] FROM [tbl_TruckMaster]"></asp:SqlDataSource> 


<asp:DetailsView id    ="dtlviewTruckMaster" 
       runat   ="server"   
       CssClass   ="GridView" 
       Width   ="125px" 
       DataKeyNames  ="TruckType" 
       Height   ="50px" 
       AutoGenerateRows ="False" 
       HeaderText  ="Truck Details" 
     ><Fields> 
<asp:BoundField DataField="TruckType"  HeaderText="TruckType" ReadOnly="True"></asp:BoundField> 
<asp:BoundField DataField="TruckCapacity" HeaderText="TruckCapacity"></asp:BoundField> 
<asp:CommandField ShowInsertButton="True"></asp:CommandField> 
<asp:CommandField ShowEditButton="True"></asp:CommandField> 
</Fields> 
<FooterStyle  BackColor="#5D7B9D"  Font-Bold="True" ForeColor="White"></FooterStyle> 
<RowStyle  VerticalAlign="Top"  BackColor="#F7F6F3" ForeColor="#333333"></RowStyle> 
<EditRowStyle Width="250px"   Height="10px"/> 
<InsertRowStyle Width="250px"   Height="10px" /> 
<PagerStyle  HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle> 
<HeaderStyle  BackColor="#5D7B9D"  Font-Bold="True" ForeColor="White"></HeaderStyle> 
<CommandRowStyle BackColor="#5D7B9D" /> 
</asp:DetailsView> 

<asp:SqlDataSource ID    ="SqlDataSource2" 
        runat   ="server" 
        ConnectionString ="<%$ ConnectionStrings:GodrejReporterConnectionString %>" 
        SelectCommand ="SELECT [TruckType], [TruckCapacity] FROM [tbl_TruckMaster] where [email protected]" 
        insertcommand ="insert into tbl_TruckMaster(TruckType,TruckCapacity) values(@TruckType,@TruckCapacity)" 
        updatecommand ="update tbl_TruckMaster set [email protected] where [email protected]" 
    > 
    <SelectParameters> 
     <asp:ControlParameter ControlID="gridTruckMaster" Name="pTruckType" PropertyName="SelectedValue" /> 
    </SelectParameters> 
    <UpdateParameters> 
     <asp:ControlParameter ControlID="gridTruckMaster" Name="pType"  PropertyName="SelectedValue" /> 
    </UpdateParameters> 
</asp:SqlDataSource> 

如果您遇到問題,可以通過tbchandgude @ gmail給我發郵件。與

相關問題