2011-10-11 38 views
1

我有一個Formview,當選擇下拉列表時,會填充SQL數據,但是當我單擊編輯時,它想要編輯下拉列表上的第一個項目,因爲從技術上講它仍然認爲它在第一頁上。我想把分頁連接到下拉菜單,但我不確定如何完成此操作。我有很多領域,所以我不會發布完整的Formview,但這裏是關鍵部分。使用DropDownList作爲ASP.Net中的分頁功能Formview

FormView控件項模板:

<ItemTemplate> 
      <table id="FormTable"> 
      <tr><th> 
     <asp:DropDownList ID="ProjectNameDropDown" runat="server" AutoPostBack="true" 
     DataSourceID="SqlDataSource1" 
     DataValueField="Project_Name" name="Text" OnSelectedIndexChanged="ProjectSelect" AppendDataBoundItems="true">    
     <asp:ListItem Text="Select a Project" /> 
     </asp:DropDownList> 
     <asp:Panel ID="Panel1" runat="server" Visible="false">    
      </th> 
      <th> 
      <asp:Button ID="EditButton" runat="server" CausesValidation="False" 
       CommandName="Edit" Text="Edit" /> 
      &nbsp;<asp:Button ID="DeleteButton" runat="server" CausesValidation="False" 
       CommandName="Delete" Text="Delete" /> 
      &nbsp;<asp:Button ID="NewButton" runat="server" CausesValidation="False" 
       CommandName="New" Text="New" />     
      </th></tr>    
      <tr><th>     
      Business Category: 
      </th><td> 
      <asp:Label ID="BusinessCategoryLabel" runat="server" 
       Text='<%# Bind("Business_Category") %>' 
       /> 
      </td></tr> 
      <tr><th> 
      Project Description: 
      </th><td> 
      <asp:TextBox ID="ProjectDescriptionLabel" runat="server" ReadOnly="true" 
       Text='<%# Bind("Project_Description") %>' TextMode="MultiLine" Rows="5" /> 
      </td></tr> 
      <tr><th> 
      Operations Owner: 
      </th><td> 
      <asp:Label ID="OwnerLabel" runat="server" 
       Text='<%# Bind("Operations_Owner") %>' /> 
      </td></tr> 

代碼背後:

protected void ProjectSelect(object sender, EventArgs e) 
    { 
     DropDownList ProjectNameDropDown = (DropDownList)FormView1.FindControl("ProjectNameDropDown"); 
     Panel Panel1 = (Panel)FormView1.FindControl("Panel1"); 
     Label BusinessCategoryLabel = (Label)FormView1.FindControl("BusinessCategoryLabel"); 
     TextBox ProjectDescriptionLabel = (TextBox)FormView1.FindControl("ProjectDescriptionLabel"); 
     Label OwnerLabel = (Label)FormView1.FindControl("OwnerLabel"); 
     Label StakeholderLabel = (Label)FormView1.FindControl("StakeholderLabel"); 
     Label ReqOrgLabel = (Label)FormView1.FindControl("ReqOrgLabel"); 
     Label PriorityLabel = (Label)FormView1.FindControl("PriorityLabel"); 
     Label DateInitiatedLabel = (Label)FormView1.FindControl("DateInitiatedLabel"); 
     Label ReqCompletionDateLabel = (Label)FormView1.FindControl("ReqCompletionDateLabel"); 
     Label ProjectLOELabel = (Label)FormView1.FindControl("ProjectLOELabel"); 
     Label OELabel = (Label)FormView1.FindControl("OELabel"); 
     Label PELabel = (Label)FormView1.FindControl("PELabel"); 
     Label EMLabel = (Label)FormView1.FindControl("EMLabel"); 
     Label PARCHLabel = (Label)FormView1.FindControl("PARCHLabel"); 
     Label WindowsLabel = (Label)FormView1.FindControl("WindowsLabel"); 
     Label StorageLabel = (Label)FormView1.FindControl("StorageLabel"); 
     Label NetworkLabel = (Label)FormView1.FindControl("NetworkLabel"); 
     Label Unix2Label = (Label)FormView1.FindControl("Unix2Label"); 
     Label TSGLabel = (Label)FormView1.FindControl("TSGLabel"); 
     Label SANDLabel = (Label)FormView1.FindControl("SANDLabel"); 
     Label MOPSLabel = (Label)FormView1.FindControl("MOPSLabel"); 
     Label ACSROpsLabel = (Label)FormView1.FindControl("ACSROpsLabel"); 
     Label IMOpsLabel = (Label)FormView1.FindControl("IMOpsLabel"); 
     Label OSCOpsLabel = (Label)FormView1.FindControl("OSCOpsLabel"); 
     Label FinancialSvcsLabel = (Label)FormView1.FindControl("FinancialSvcsLabel"); 
     Label VantageLabel = (Label)FormView1.FindControl("VantageLabel"); 
     Label VoiceSysOpsLabel = (Label)FormView1.FindControl("VoiceSysOpsLabel"); 
     Label VoiceAppOpsLabel = (Label)FormView1.FindControl("VoiceAppOpsLabel"); 
     Label ACPxOpsLabel = (Label)FormView1.FindControl("ACPxOpsLabel"); 
     Label WFXOpsLabel = (Label)FormView1.FindControl("WFXOpsLabel"); 
     Label WebOpsLabel = (Label)FormView1.FindControl("WebOpsLabel"); 
     Label DBALabel = (Label)FormView1.FindControl("DBALabel"); 
     Label CapacityPlanningLabel = (Label)FormView1.FindControl("CapacityPlanningLabel"); 
     Label BCPLabel = (Label)FormView1.FindControl("BCPLabel"); 
     Label DataCenterLabel = (Label)FormView1.FindControl("DataCenterLabel"); 
     Label GoldsmithLabel = (Label)FormView1.FindControl("GoldsmithLabel"); 
     Label AmericasITOpsLabel = (Label)FormView1.FindControl("AmericasITOpsLabel"); 
     Label APACITOpsLabel = (Label)FormView1.FindControl("APACITOpsLabel"); 
     Label EMEAITOpsLabel = (Label)FormView1.FindControl("EMEAITOpsLabel"); 

     Panel1.Visible = true; 
     if (ProjectNameDropDown.Items.FindByText("Select a Project").Selected != true) 
     { 




      string myConnectionString = @"Data Source=odcsgwinsql11.devcsg.com\ss2008;Initial Catalog=hulc01;Integrated Security=True"; 
      SqlConnection myConnection = new SqlConnection(myConnectionString); 
      string MySelectQuery = "SELECT * FROM Common WHERE Project_Name = '" + ProjectNameDropDown.SelectedValue + "'"; 

      using (SqlCommand cmd = new SqlCommand(MySelectQuery)) 
      { 
       cmd.Connection = myConnection; 
       myConnection.Open(); 

       SqlDataAdapter Adapter1 = new SqlDataAdapter(cmd); 
       DataSet dset = new DataSet(); 
       Adapter1.Fill(dset); 
       BusinessCategoryLabel.Text = dset.Tables[0].Rows[0]["Business_Category"].ToString(); 
       ProjectDescriptionLabel.Text = dset.Tables[0].Rows[0]["Project_Description"].ToString(); 
       OwnerLabel.Text = dset.Tables[0].Rows[0]["Operations_Owner"].ToString(); 
       StakeholderLabel.Text = dset.Tables[0].Rows[0]["NonOps_Key_Stakeholder"].ToString(); 
       ReqOrgLabel.Text = dset.Tables[0].Rows[0]["Requesting_Organization"].ToString(); 
       PriorityLabel.Text = dset.Tables[0].Rows[0]["Priority"].ToString(); 
       DateInitiatedLabel.Text = dset.Tables[0].Rows[0]["Date_Initiated"].ToString(); 
       ReqCompletionDateLabel.Text = dset.Tables[0].Rows[0]["Required_Completion_Date"].ToString(); 
       ProjectLOELabel.Text = dset.Tables[0].Rows[0]["Project_LOE"].ToString(); 
       OELabel.Text = dset.Tables[0].Rows[0]["OE"].ToString(); 
       PELabel.Text = dset.Tables[0].Rows[0]["PE"].ToString(); 
       EMLabel.Text = dset.Tables[0].Rows[0]["EM"].ToString(); 
       PARCHLabel.Text = dset.Tables[0].Rows[0]["PARCH"].ToString(); 
       WindowsLabel.Text = dset.Tables[0].Rows[0]["Windows"].ToString(); 
       StorageLabel.Text = dset.Tables[0].Rows[0]["Storage"].ToString(); 
       NetworkLabel.Text = dset.Tables[0].Rows[0]["Network"].ToString(); 
       Unix2Label.Text = dset.Tables[0].Rows[0]["UNIX2"].ToString(); 
       TSGLabel.Text = dset.Tables[0].Rows[0]["TSG"].ToString(); 
       SANDLabel.Text = dset.Tables[0].Rows[0]["SAND"].ToString(); 
       MOPSLabel.Text = dset.Tables[0].Rows[0]["MOPS"].ToString(); 
       ACSROpsLabel.Text = dset.Tables[0].Rows[0]["ACSR_Ops"].ToString(); 
       IMOpsLabel.Text = dset.Tables[0].Rows[0]["IM_Ops"].ToString(); 
       OSCOpsLabel.Text = dset.Tables[0].Rows[0]["OSC_Ops"].ToString(); 
       FinancialSvcsLabel.Text = dset.Tables[0].Rows[0]["Financial_Svcs"].ToString(); 
       VantageLabel.Text = dset.Tables[0].Rows[0]["Vantage"].ToString(); 
       VoiceAppOpsLabel.Text = dset.Tables[0].Rows[0]["Voice_Sys_Ops"].ToString(); 
       VoiceSysOpsLabel.Text = dset.Tables[0].Rows[0]["Voice_App_Ops"].ToString(); 
       ACPxOpsLabel.Text = dset.Tables[0].Rows[0]["ACPX_Ops"].ToString(); 
       WFXOpsLabel.Text = dset.Tables[0].Rows[0]["WFX_Ops"].ToString(); 
       WebOpsLabel.Text = dset.Tables[0].Rows[0]["Web_Ops"].ToString(); 
       DBALabel.Text = dset.Tables[0].Rows[0]["DBA"].ToString(); 
       CapacityPlanningLabel.Text = dset.Tables[0].Rows[0]["Capacity_Planning"].ToString(); 
       BCPLabel.Text = dset.Tables[0].Rows[0]["BCP"].ToString(); 
       DataCenterLabel.Text = dset.Tables[0].Rows[0]["Data_Center"].ToString(); 
       GoldsmithLabel.Text = dset.Tables[0].Rows[0]["Goldsmith"].ToString(); 
       AmericasITOpsLabel.Text = dset.Tables[0].Rows[0]["Americas_IT_Ops"].ToString(); 
       APACITOpsLabel.Text = dset.Tables[0].Rows[0]["APAC_IT_Ops"].ToString(); 
       EMEAITOpsLabel.Text = dset.Tables[0].Rows[0]["EMEA_IT_Ops"].ToString(); 
      } 
     } 

我想以某種方式告訴FormView控件的頁面切換從下拉列表中選擇時,但我一直沒能圖爲此編碼。謝謝你的幫助!

+0

TL; DR在我看來。 –

+0

@UweKeim這很有幫助=) – jadarnel27

+0

其實,你還有哪些選項可以告訴原始的海報而不是作爲評論呢? –

回答

2

這樣做的一種方法(我過去的做法)將移動FormView之外的數據綁定DropDownList。然後,將您的FormView綁定到不同的SQLDataSource,這取決於DropDownList的SelectedValue。所以,你會與你的所有的項目名稱的DDL:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="SqlDataSource1" DataTextField="Project_Name" 
    DataValueField="Project_Name"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="Your Connection String" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT DISTINCT [Project_Name] FROM [ProjectTable]"> 
</asp:SqlDataSource> 

和一個FormView這取決於什麼是在DDL選擇:

<asp:FormView ID="FormView1" runat="server" AllowPaging="True" 
    DataKeyNames="Project_Name" DataSourceID="SqlDataSource2"> 
... 
</asp:FormView> 

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="Your Connection String" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT * FROM [ProjectTable] WHERE [email protected]_Name"> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="DropDownList1" Name="Project_Name" 
      PropertyName="SelectedValue" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

這樣,當你點擊「編輯」按鈕在您的FormView中,您正在編輯您想要編輯的記錄。

作爲一個側面說明,您的代碼隱藏讓您非常容易受到SQL注入攻擊。我會小心這個。而不是使用字符串連接來生成該SELECT查詢,您應該使用參數化查詢

+0

完美工作。我甚至不需要後面的代碼來填充字段。再次感謝你的幫助!我打算讓MIS修復我所有的安全漏洞...... =) –

+0

@Chris哈哈,真棒。很高興我能幫上忙! – jadarnel27