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>
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: