当没有从下拉列表中选择值时隐藏/显示表单视图
我的页面中有两个下拉列表、一个详细信息视图和一个表单视图控件。第一个下拉列表具有书籍类别列表,而第二个下拉列表具有基于第一个下拉列表的所选值的书籍列表,最后是显示第二个下拉列表详细信息的详细信息视图。另一方面,表单视图没有任何控件,我只是使用它从详细信息视图中获取值,以便根据第二个选定的下拉列表插入记录。所以我想要做的是,如果第二个下拉列表为空,则隐藏表单视图。
它有点不清楚,但为了简短起见,我只想隐藏表单视图,如果第二个下拉列表中没有选择值(并显示是否有)。
我已附上我的示例代码。我希望你能理解。
提前致谢
<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>
I have two dropdownlist, a detailsview and a formview control in my page. The first dropdownlist has a list of categories of books, while the second dropdownlist has a list of books based on the selected value of the first dropdownlist and lastly the detailsview which shows the details second dropdownlist. The formview on the other hand doesnt have any control and I just use it to get the value from the detailsview for inserting record based on the second selected dropdownlist. So what I want to do is hide the formview if the second dropdownlist is empty.
Its a bit unclear but to make things short I just want to hide the formview if theres no value selected in the second dropdownlist (and show if there is).
I have attached my sample code. I hope you could understand.
Thanks in advance
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 最初从标记设置 FromView
Visible="False"
。2) 将
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
分配给 DropDownList2 并设置其AutoPostBack="True"
3) 在后面的代码中添加以下内容:
1) Set FromView
Visible="False"
initially, from the markup.2) assign
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
to DropDownList2 and set itsAutoPostBack="True"
3) in the code behind add this: