我有一個gridview和它自己的sqlDataSource。在頁腳行中,我有一個插入字段來創建一個新行。但是,該頁腳行相當複雜,我試圖弄清楚如何使其工作。如何基於gridview中另一個下拉列表的選擇來填充下拉列表
我想看到發生的事情是,用戶選擇OTypeName ddl。基於該選擇,啓用OSpecies ddl並基於OTypeName ddl填充值。關於OSpecies和OVariety也可以這樣說。
然後,用戶將自己的信息輸入到OAge,OYields,OPlantDate和OPlantFrom中。
頁腳行中的其餘字段是將根據最終的OVariety選項填充的標籤。
插入按鈕然後將所有這些列添加到gridview。
我只需要幫助您根據ddl選擇另一個ddl來填充一個ddl,以及如何根據ddl選擇填充標籤。
這是我在aspx文件代碼:
<%@ Page Title="" Language="C#" MasterPageFile="~/Permaculture.Master" AutoEventWireup="true" CodeBehind="OrchardMainWebForm.aspx.cs" Inherits="PermacultureOrganizer.OrchardMainWebForm" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Content" runat="server" >
<asp:GridView ID="gvOrchardData" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="OUserOrchardID" DataSourceID="dsOrchardDatabase" CellPadding="4" CellSpacing="4" ForeColor="#333333" GridLines="Vertical" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" ShowFooter="True" Font-Bold="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="OUserOrchardID" InsertVisible="False" SortExpression="OUserOrchardID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("OUserOrchardID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("OUserOrchardID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbInsert"
runat="server"
OnClick="lbInsert_Click" ForeColor="White">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OTypeName" SortExpression="OTypeName">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server"
AutoPostBack="True"
DataSourceID="dsTypeName"
DataTextField="OrchardTypeName"
DataValueField="OrchardTypeID" >
</asp:DropDownList>
<asp:SqlDataSource ID="dsTypeName" runat="server" ConnectionString="<%$ ConnectionStrings:DB_9DE518_PermacultureConnectionString %>" SelectCommand="SELECT * FROM [tblOrchardType]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("OTypeName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlTypeName"
runat="server"
AutoPostBack="True"
DataSourceID="dsTypeName"
DataTextField="OrchardTypeName"
DataValueField="OrchardTypeID"
>
</asp:DropDownList>
<asp:SqlDataSource ID="dsTypeName" runat="server" ConnectionString="<%$ ConnectionStrings:DB_9DE518_PermacultureConnectionString %>" SelectCommand="SELECT * FROM [tblOrchardType]"></asp:SqlDataSource>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OSpecies" SortExpression="OSpecies">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2"
runat="server"
SelectedValue='<%# Bind("OSpecies") %>'
AutoPostBack="True"
DataSourceID="dsSpecies"
DataTextField="Species"
DataValueField="OrchardTypeID">
<asp:ListItem>Select Species</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsSpecies" runat="server" ConnectionString="<%$ ConnectionStrings:DB_9DE518_PermacultureConnectionString %>" SelectCommand="SELECT [Species], [OrchardTypeID] FROM [tblOrchardItem]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("OSpecies") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSpecies"
runat="server"
AutoPostBack="True"
DataSourceID="dsSpecies"
DataTextField="Species"
DataValueField="OrchardTypeID"
>
<asp:ListItem>Select Species</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsSpecies" runat="server" ConnectionString="<%$ ConnectionStrings:DB_9DE518_PermacultureConnectionString %>" SelectCommand="SELECT [Species], [OrchardTypeID] FROM [tblOrchardItem]">
</asp:SqlDataSource>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OVariety" SortExpression="OVariety">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3"
runat="server"
AutoPostBack="True"
DataSourceID="dsVariety"
DataTextField="Variety"
DataValueField="OrchardTypeID">
</asp:DropDownList>
<asp:SqlDataSource ID="dsVariety" runat="server" ConnectionString="<%$ ConnectionStrings:DB_9DE518_PermacultureConnectionString %>" SelectCommand="SELECT [Variety], [OrchardTypeID] FROM [tblOrchardItem]"></asp:SqlDataSource>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlVariety"
runat="server"
AutoPostBack="True"
DataSourceID="dsVariety"
DataTextField="Variety"
DataValueField="OrchardTypeID"
>
</asp:DropDownList>
<asp:SqlDataSource ID="dsVariety" runat="server" ConnectionString="<%$ ConnectionStrings:DB_9DE518_PermacultureConnectionString %>" SelectCommand="SELECT [Variety], [OrchardTypeID] FROM [tblOrchardItem]"></asp:SqlDataSource>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("OVariety") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OAge" SortExpression="OAge">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("OAge") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBxAge" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("OAge") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OYields" SortExpression="OYields">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("OYields") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBxYields" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("OYields") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OPlantDate" SortExpression="OPlantDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("OPlantDate") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBxPlantDate" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("OPlantDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OPlantFrom" SortExpression="OPlantFrom">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("OPlantFrom") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBxPlantFrom" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("OPlantFrom") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OTreeSpacing" SortExpression="OTreeSpacing">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("OTreeSpacing") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTreeSpacing" runat="server"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("OTreeSpacing") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OFertilizingTimes" SortExpression="OFertilizingTimes">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("OFertilizingTimes") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFertilizingTimes" runat="server"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("OFertilizingTimes") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OWateringNeeds" SortExpression="OWateringNeeds">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("OWateringNeeds") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblWateringNeeds" runat="server"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("OWateringNeeds") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OPollination" SortExpression="OPollination">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("OPollination") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblPollination" runat="server"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("OPollination") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OFertilizer" SortExpression="OFertilizer">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("OFertilizer") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFertilizer" runat="server"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label13" runat="server" Text='<%# Bind("OFertilizer") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OPesticide" SortExpression="OPesticide">
<EditItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("OPesticide") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblPesticide" runat="server"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label14" runat="server" Text='<%# Bind("OPesticide") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="Black" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:SqlDataSource ID="dsOrchardDatabase" runat="server"
ConnectionString="<%$ ConnectionStrings:DB_9DE518_PermacultureConnectionString %>"
SelectCommand="SELECT * FROM [tblUserOrchard]" DeleteCommand="DELETE FROM [tblUserOrchard] WHERE [OUserOrchardID] = @OUserOrchardID" InsertCommand="INSERT INTO [tblUserOrchard] ([OTypeName], [OSpecies], [OVariety], [OAge], [OYields], [OPlantDate], [OPlantFrom], [OTreeSpacing], [OFertilizingTimes], [OPruningTimes], [OWateringNeeds], [OPollination], [OFertilizer], [OPesticide]) VALUES (@OTypeName, @OSpecies, @OVariety, @OAge, @OYields, @OPlantDate, @OPlantFrom, @OTreeSpacing, @OFertilizingTimes, @OPruningTimes, @OWateringNeeds, @OPollination, @OFertilizer, @OPesticide)" UpdateCommand="UPDATE [tblUserOrchard] SET [OTypeName] = @OTypeName, [OSpecies] = @OSpecies, [OVariety] = @OVariety, [OAge] = @OAge, [OYields] = @OYields, [OPlantDate] = @OPlantDate, [OPlantFrom] = @OPlantFrom, [OTreeSpacing] = @OTreeSpacing, [OFertilizingTimes] = @OFertilizingTimes, [OPruningTimes] = @OPruningTimes, [OWateringNeeds] = @OWateringNeeds, [OPollination] = @OPollination, [OFertilizer] = @OFertilizer, [OPesticide] = @OPesticide WHERE [OUserOrchardID] = @OUserOrchardID">
<DeleteParameters>
<asp:Parameter Name="OUserOrchardID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="OTypeName" Type="String" />
<asp:Parameter Name="OSpecies" Type="String" />
<asp:Parameter Name="OVariety" Type="String" />
<asp:Parameter Name="OAge" Type="Int32" />
<asp:Parameter Name="OYields" Type="String" />
<asp:Parameter Name="OPlantDate" Type="String" />
<asp:Parameter Name="OPlantFrom" Type="String" />
<asp:Parameter Name="OTreeSpacing" Type="String" />
<asp:Parameter Name="OFertilizingTimes" Type="String" />
<asp:Parameter Name="OPruningTimes" Type="String" />
<asp:Parameter Name="OWateringNeeds" Type="String" />
<asp:Parameter Name="OPollination" Type="String" />
<asp:Parameter Name="OFertilizer" Type="String" />
<asp:Parameter Name="OPesticide" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="OTypeName" Type="String" />
<asp:Parameter Name="OSpecies" Type="String" />
<asp:Parameter Name="OVariety" Type="String" />
<asp:Parameter Name="OAge" Type="Int32" />
<asp:Parameter Name="OYields" Type="String" />
<asp:Parameter Name="OPlantDate" Type="String" />
<asp:Parameter Name="OPlantFrom" Type="String" />
<asp:Parameter Name="OTreeSpacing" Type="String" />
<asp:Parameter Name="OFertilizingTimes" Type="String" />
<asp:Parameter Name="OPruningTimes" Type="String" />
<asp:Parameter Name="OWateringNeeds" Type="String" />
<asp:Parameter Name="OPollination" Type="String" />
<asp:Parameter Name="OFertilizer" Type="String" />
<asp:Parameter Name="OPesticide" Type="String" />
<asp:Parameter Name="OUserOrchardID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsTypeName" runat="server" ConnectionString="<%$ ConnectionStrings:DB_9DE518_PermacultureConnectionString %>" SelectCommand="SELECT * FROM [tblOrchardType]"></asp:SqlDataSource>
</asp:Content>
我不知道要放什麼東西在aspx.cs文件,但這裏是我迄今爲止關於插入按鈕:
public void lbInsert_Click(object sender, EventArgs e)
{
dsOrchardDatabase.InsertParameters["OTypeName"].DefaultValue =
((DropDownList)gvOrchardData.FooterRow.FindControl("ddlTypeName")).SelectedItem.ToString();
dsOrchardDatabase.InsertParameters["OSpecies"].DefaultValue =
((DropDownList)gvOrchardData.FooterRow.FindControl("ddlSpecies")).SelectedItem.ToString();
dsOrchardDatabase.InsertParameters["OVariety"].DefaultValue =
((DropDownList)gvOrchardData.FooterRow.FindControl("ddlVariety")).SelectedItem.ToString();
dsOrchardDatabase.InsertParameters["OAge"].DefaultValue =
((TextBox)gvOrchardData.FooterRow.FindControl("txtBxAge")).Text;
dsOrchardDatabase.InsertParameters["OYields"].DefaultValue =
((TextBox)gvOrchardData.FooterRow.FindControl("txtBxYields")).Text;
dsOrchardDatabase.InsertParameters["OPlantDate"].DefaultValue =
((TextBox)gvOrchardData.FooterRow.FindControl("txtBxPlantDate")).Text;
dsOrchardDatabase.InsertParameters["OPlantFrom"].DefaultValue =
((TextBox)gvOrchardData.FooterRow.FindControl("txtBxPlantFrom")).Text;
dsOrchardDatabase.InsertParameters["OTreeSpacing"].DefaultValue =
((Label)gvOrchardData.FooterRow.FindControl("lblTreeSpacing")).Text;
dsOrchardDatabase.InsertParameters["OFertilizingTimes"].DefaultValue =
((Label)gvOrchardData.FooterRow.FindControl("lblFertilizingTimes")).Text;
dsOrchardDatabase.InsertParameters["OWateringNeeds"].DefaultValue =
((Label)gvOrchardData.FooterRow.FindControl("lblWateringNeeds")).Text;
dsOrchardDatabase.InsertParameters["OPollination"].DefaultValue =
((Label)gvOrchardData.FooterRow.FindControl("lblPollination")).Text;
dsOrchardDatabase.InsertParameters["OFertilizer"].DefaultValue =
((Label)gvOrchardData.FooterRow.FindControl("lblFertilizer")).Text;
dsOrchardDatabase.InsertParameters["OPesticide"].DefaultValue =
((Label)gvOrchardData.FooterRow.FindControl("lblPesticide")).Text;
dsOrchardDatabase.Insert();
}
任何幫助將不勝感激!
文本的那道牆,是有辦法,你可以在較小的例子中隔離問題? – Bloodied
我瞭解Arescet。這是第一個頁腳DDL: 的 ASP:DropDownList的> <% - 連接到連接字符串 - %> FooterTemplate> 在aspx.cs,這裏就是我有: 私人無效ddlTypeName_SelectedIndexChanged(對象發件人,EventArgs e) { //需要啓用ddlSpecies //需要根據ddlTypeName選擇填充ddlSpecies } –
Grey