我的页面中有两个下拉列表,一个detailsview和一个formview控件。第一个下拉列表包含书籍类别列表,而第二个下拉列表包含基于第一个下拉列表的选定值的书籍列表,最后是详细信息视图,其中显示详细信息第二个下拉列表。另一方面,formview没有任何控制权,我只是使用它从detailsview中获取值,以便根据第二个选定的下拉列表插入记录。所以我想要做的是在第二个下拉列表为空时隐藏formview。
它有点不清楚,但为了缩短我只想隐藏formview,如果在第二个下拉列表中没有选择值(并显示是否有)。
我附上了我的示例代码。我希望你能理解。
提前致谢
<p>
Book Reservation</p>
<p>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="categoryDataSource" DataTextField="name"
DataValueField="categoryid" >
<asp:ListItem Selected="True" Value="">-- Choose a category --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="categoryDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]" >
</asp:SqlDataSource>
</p>
<p>
<asp:DropDownList ID="DropDownList2" runat="server"
AutoPostBack="True" DataSourceID="booktitleDataSource"
DataTextField="booktitle" DataValueField="bookid" OnDataBound="DetailsView1_DataBound" >
<asp:ListItem Selected="True" Value="-1">-- Choose a book --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="booktitleDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [bookid], [booktitle], [categoryid] FROM [TblBooks] WHERE ([categoryid] = @categoryid)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="categoryid"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="bookid" DataSourceID="bookdetailsDataSource" >
<HeaderTemplate>
<b>BOOK DETAILS</b>
</HeaderTemplate>
<FooterTemplate>
<b>RESERVED BY</b>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="reservationid"
DataSourceID="reserveDataSource">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee PIN"
SortExpression="EmployeeID" />
<asp:BoundField DataField="reservedate" HeaderText="Reserve date"
SortExpression="reservedate" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="resereDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT dbo.BookReservation.reservationid,dbo.BookReservation.bookid, dbo.BookReservation.EmployeeID,
dbo.BookReservation.reservedate, dbo.BookReservation.isapproved, dbo.BookReservation.reschedule, dbo.BookReservation.isdeleted,
dbo.TblBooks.booktitle FROM dbo.BookReservation INNER JOIN dbo.TblBooks ON dbo.BookReservation.bookid = dbo.TblBooks.bookid WHERE (dbo.BookReservation.isdeleted IS NULL) OR (dbo.BookReservation.deleted = 0)">
</asp:SqlDataSource>
</FooterTemplate>
<Fields>
<asp:BoundField DataField="bookid" HeaderText="ISBN" ReadOnly="True"
SortExpression="bookid" />
<asp:BoundField DataField="booktitle" HeaderText="Title"
SortExpression="booktitle" />
<asp:BoundField DataField="lastname" HeaderText="Author"
SortExpression="lastname" />
<asp:BoundField DataField="firstname" HeaderText=""
SortExpression="firstname" />
<asp:BoundField DataField="description" HeaderText="Description"
SortExpression="description" />
<asp:BoundField DataField="name" HeaderText="Category"
SortExpression="name" />
<asp:BoundField DataField="quantity" HeaderText="Quantity"
SortExpression="quantity" />
<asp:BoundField DataField="dateadded" HeaderText="Date added"
SortExpression="dateadded" />
<asp:CheckBoxField DataField="isdeleted" HeaderText="Deleted"
SortExpression="isdeleted" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="bookdetailsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT dbo.TblBooks.bookid, dbo.TblBooks.booktitle, dbo.TblBooks.lastname, dbo.TblBooks.firstname, dbo.TblBooks.description, dbo.TblBooks.categoryid, dbo.TblBooks.dateadded, dbo.TblBooks.statusid, dbo.TblBooks.quantity, dbo.TblBooks.isdeleted, dbo.BookStatus.statusname, dbo.TblCategory.name, dbo.LendTable.EmployeeID, dbo.LendTable.dateborrowed, dbo.LendTable.expdateofreturn, dbo.LendTable.datereturned FROM dbo.TblBooks INNER JOIN dbo.TblCategory ON dbo.TblBooks.categoryid = dbo.TblCategory.categoryid INNER JOIN dbo.BookStatus ON dbo.TblBooks.statusid = dbo.BookStatus.statusid INNER JOIN dbo.LendTable ON dbo.TblBooks.bookid = dbo.LendTable.bookid WHERE (dbo.TblBooks.bookid = @bookid) ">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList2" Name="bookid"
PropertyName="SelectedValue" Type="Int64" />
</SelectParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="reservationid"
DataSourceID="reserveDataSource" DefaultMode="Insert"
OnDataBound="FormView1_DataBound" >
<EditItemTemplate>
reservationid:
<asp:Label ID="reservationidLabel1" runat="server"
Text='<%# Eval("reservationid") %>' />
<br />
bookid:
<asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
<br />
EmployeeID:
<asp:TextBox ID="EmployeeIDTextBox" runat="server"
Text='<%# Bind("EmployeeID") %>' />
<br />
reservedate:
<asp:TextBox ID="reservedateTextBox" runat="server"
Text='<%# Bind("reservedate") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Reserve date:
<asp:TextBox ID="reservedateTextBox" runat="server"
Text='<%# Bind("reservedate") %>' />
<a href="#" onclick="cdp1.showCalendar(this, 'ctl00$ContentPlaceHolder1$FormView1$reservedateTextBox'); return false;">Date Picker</a>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="reservedateTextBox" ErrorMessage="* " ValidationGroup="reservebook">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:Button ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
<%--ISBN:--%>
<asp:TextBox ID="bookidTextBox" runat="server" Visible="false"
Text='<%# Bind("bookid") %>' />
<br />
<%--Employee PIN:--%>
<asp:TextBox ID="EmployeeIDTextBox" runat="server" Visible="false"
Text='<%# Bind("EmployeeID") %>' />
<br />
</InsertItemTemplate>
答案 0 :(得分:2)
1)最初从标记设置FromView Visible="False"
。
2)将OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
分配给DropDownList2并设置其AutoPostBack="True"
3)在后面的代码中加上这个:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if(!DropDownList2.SelectedValue.Equals("-1"))
FromView1.Visible=true;
else
FromView1.Visible=false;
}