How to use dropdown list to update formview display
I'm having a bit of trouble figuring this one out.
I have a dropdownlist (populated from a datasource that concatenates first and last names, and the value is an "employee ID", oh and it's a sql server database).
I also have a formview, that displays all the "employee" information (same database, different sqldatasource. What I'm trying to do is when a user selects an employee from the dropdo开发者_高级运维wnlist, it finds the page with the matching employee ID and displays that page.
The formview also needs to keep the paging, to allow a user to select next or previous if needed. (It'd also be nice if the ddl would update on next and previous to show the current viewed employee).
Currently, I have the ddl inside the formview, with a
SelectedValue='<%# Eval("Employee_ID") %> (which I'm hoping will take care of the ddl showing part, haven't tested that bit as I just woke up,heh).
I have autopostback in the ddl set to true, and it is firing the OnSelectedIndexChanged event, I'm just not sure what to do next. (more specifically, I guess I don't know how in C# to find the page with the matching "employee id".
Thanks much for any help!
I think this is big example , how to bind and update the form view using drop down select item....
<asp: ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="DataSet1TableAdapters.teachersTableAdapter" UpdateMethod="Update">
<DeleteParameters>
<asp: Parameter Name="Original_PKID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp: Parameter Name="name" Type="String" />
<asp: Parameter Name="lastname" Type="String" />
<asp: Parameter Name="department" Type="Int32" />
<asp: Parameter Name="PKID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp: Parameter Name="name" Type="String" />
<asp: Parameter Name="lastname" Type="String" />
<asp: Parameter Name="department" Type="Int32" />
</InsertParameters>
</asp: ObjectDataSource>
<br />
<asp: ObjectDataSource ID="ObjectDataSource2" runat="server" DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="DataSet1TableAdapters.departmentsTableAdapter" UpdateMethod="Update">
<DeleteParameters>
<asp: Parameter Name="Original_PKID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp: Parameter Name="departmentName" Type="String" />
<asp: Parameter Name="PKID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp: Parameter Name="departmentName" Type="String" />
</InsertParameters>
</asp:ObjectDataSource></div><br />
<asp: FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="PKID"
DataSourceID="ObjectDataSource1" OnDataBound="FormView1_DataBound">
<EditItemTemplate>
PKID:
<asp: Label ID="PKIDLabel1" runat="server" Text='<%# Eval("PKID") %>'></asp:Label><br />
name:
<asp: TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
</asp: TextBox><br />
lastname:
<asp: TextBox ID="lastnameTextBox" runat="server" Text='<%# Bind("lastname") %>'>
</asp: TextBox><br />
department:
<asp: TextBox ID="departmentTextBox" runat="server" Text='<%# Bind("department") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">
</asp: LinkButton>
<asp: LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp: LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
name:
<asp: TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
</asp: TextBox><br />
lastname:
<asp: TextBox ID="lastnameTextBox" runat="server" Text='<%# Bind("lastname") %>'>
</asp: TextBox><br />
department:
<asp: TextBox ID="departmentTextBox" runat="server" Text='<%# Bind("department") %>'>
</asp: TextBox><br />
<asp: LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp: LinkButton>
<asp: LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp: LinkButton>
</InsertItemTemplate>
<ItemTemplate>
PKID:
<asp: Label ID="PKIDLabel" runat="server" Text='<%# Eval("PKID") %>'></asp:Label><br />
name:
<asp: Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp: Label><br />
lastname:
<asp: Label ID="lastnameLabel" runat="server" Text='<%# Bind("lastname") %>'></asp: Label><br />
department:
<asp: DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource2"
DataTextField="departmentName" DataValueField="PKID">
</asp: DropDownList><br />
<asp: LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp: LinkButton>
<asp: LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete"></asp: LinkButton>
<asp: LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="New"></asp: LinkButton>
</ItemTemplate>
</asp: FormView>
In the code behind i used itemdatabound event of the formview so that i set the <strong class="highlight">selected</strong> value for the dropdownlist as follows :
protected void FormView1_DataBound(object sender, EventArgs e)
{
DataRowView drv = (DataRowView)FormView1.DataItem;
((DropDownList)FormView1.FindControl("DropDownList1")).SelectedValue = drv["department"].ToString();
}
protected void FormView1_DataBound(object sender, EventArgs e)
{
DataRowView drv = (DataRowView)FormView1.DataItem;
((DropDownList)FormView1.FindControl("DropDownList1")).SelectedValue = drv["department"].ToString();
}
精彩评论