如何将表绑定到ListView
应该使用哪个控件,
LinqDataSource
或者应该使用ObjectDataSource
什么DataSourceID
ListView
我的ListView
是
<asp:ListView ID="ListViewResult" runat="server" DataSourceID="" GroupItemCount="3">
<EmptyDataTemplate>
<table id="Table1" runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td id="Td1" runat="server" />
</EmptyItemTemplate>
<GroupTemplate>
<tr id="itemPlaceholderContainer" runat="server">
<td id="itemPlaceholder" runat="server"></td>
</tr>
</GroupTemplate>
<InsertItemTemplate>
<td id="Td2" runat="server" style="">ID:
<asp:DynamicControl ID="DynamicControl1" runat="server" DataField="ID" Mode="Insert" ValidationGroup="Insert" />
<br />Image:
<asp:DynamicControl ID="DynamicControl2" runat="server" DataField="Image" Mode="Insert" ValidationGroup="Insert" />
<br />Name:
<asp:DynamicControl ID="DynamicControl3" runat="server" DataField="Name" Mode="Insert" ValidationGroup="Insert" />
<br />Age:
<asp:DynamicControl ID="DynamicControl4" runat="server" DataField="Age" Mode="Insert" ValidationGroup="Insert" />
<br />Height:
<asp:DynamicControl ID="DynamicControl5" runat="server" DataField="Height" Mode="Insert" ValidationGroup="Insert" />
<br />Education:
<asp:DynamicControl ID="DynamicControl6" runat="server" DataField="Education" Mode="Insert" ValidationGroup="Insert" />
<br />CurrentStatus:
<asp:DynamicControl ID="DynamicControl7" runat="server" DataField="CurrentStatus" Mode="Insert" ValidationGroup="Insert" />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" ValidationGroup="Insert" />
<br />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
<br /></td>
</InsertItemTemplate>
<ItemTemplate>
<td id="Td3" runat="server" style="background-color: #FFFBD6;color: #333333;">ID:
<asp:DynamicControl ID="DynamicControl8" runat="server" DataField="ID" Mode="ReadOnly" />
<br />Image:
<asp:DynamicControl ID="DynamicControl9" runat="server" DataField="Image" Mode="ReadOnly" />
<br />Name:
<asp:DynamicControl ID="DynamicControl10" runat="server" DataField="Name" Mode="ReadOnly" />
<br />Age:
<asp:DynamicControl ID="DynamicControl11" runat="server" DataField="Age" Mode="ReadOnly" />
<br />Height:
<asp:DynamicControl ID="DynamicControl12" runat="server" DataField="Height" Mode="ReadOnly" />
<br />Education:
<asp:DynamicControl ID="DynamicControl13" runat="server" DataField="Education" Mode="ReadOnly" />
<br />CurrentStatus:
<asp:DynamicControl ID="DynamicControl14" runat="server" DataField="CurrentStatus" Mode="ReadOnly" />
<br /></td>
</ItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td4" runat="server">
<table id="groupPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr id="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr2" runat="server">
<td id="Td5" runat="server" style="text-align: center;background-color: #FFCC66;font-family: Verdana, Arial, Helvetica, sans-serif;color: #333333;">
<asp:DataPager ID="DataPager1" runat="server" PageSize="9">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<td id="Td6" runat="server" style="background-color: #FFCC66;font-weight: bold;color: #000080;">ID:
<asp:DynamicControl ID="DynamicControl15" runat="server" DataField="ID" Mode="ReadOnly" />
<br />Image:
<asp:DynamicControl ID="DynamicControl16" runat="server" DataField="Image" Mode="ReadOnly" />
<br />Name:
<asp:DynamicControl ID="DynamicControl17" runat="server" DataField="Name" Mode="ReadOnly" />
<br />Age:
<asp:DynamicControl ID="DynamicControl18" runat="server" DataField="Age" Mode="ReadOnly" />
<br />Height:
<asp:DynamicControl ID="DynamicControl19" runat="server" DataField="Height" Mode="ReadOnly" />
<br />Education:
<asp:DynamicControl ID="DynamicControl20" runat="server" DataField="Education" Mode="ReadOnly" />
<br />CurrentStatus:
<asp:DynamicControl ID="DynamicControl21" runat="server" DataField="CurrentStatus" Mode="ReadOnly" />
<br /></td>
</SelectedItemTemplate>
</asp:ListView>
我的代码是
protected void ButtonSearch_Click(object sender, EventArgs e)
{
using (WebTechManiaDataContext Data = new WebTechManiaDataContext())
{
string Education = DropDownListEducation.SelectedItem.Text.ToString();
string MaritalStatus = DropDownListMaritalStatus.SelectedItem.Text.ToString();
int From = int.Parse(DropDownListFrom.SelectedItem.Text);
int To = int.Parse(DropDownListTo.SelectedItem.Text);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
if (RadioButtonGroom.Checked == true | RadioButtonBride.Checked == false)
{
if ((From == 21) && (To == 22))
{
var FoundGroom = Data.Males.Where(Males => (Males.Age == 21 || Males.Age == 22) && (Males.Education == Education || Education.Length == 0) && (Males.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
if (!FoundGroom.Any())
{
Response.Write("<script>alert('Search result Negetive');</script>");
}
else
{
SqlCommand cmd = Data.GetCommand(FoundGroom) as SqlCommand;
SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
DataAdapter.Fill(ds);
ListViewResult.DataSource = ds;
ListViewResult.DataBind();
}
}
else if ((From == 21) && (To == 23))
{
var FoundGroom = Data.Males.Where(Males => (Males.Age == 21 || Males.Age == 22 || Males.Age == 23) && (Males.Education == Education || Education.Length == 0) && (Males.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
if (!FoundGroom.Any())
{
Response.Write("<script>alert('Search result Negetive');</script>");
}
else
{
SqlCommand cmd = Data.GetCommand(FoundGroom) as SqlCommand;
SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
DataAdapter.Fill(ds);
ListViewResult.DataSource = ds;
ListViewResult.DataBind();
}
}
else if ((From == 21) && (To == 24))
{
var FoundGroom = Data.Males.Where(Males => (Males.Age == 21 || Males.Age == 22 || Males.Age == 23 || Males.Age == 24) && (Males.Education == Education || Education.Length == 0) && (Males.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
if (!FoundGroom.Any())
{
Response.Write("<script>alert('Search result Negetive');</script>");
}
else
{
SqlCommand cmd = Data.GetCommand(FoundGroom) as SqlCommand;
SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
DataAdapter.Fill(ds);
ListViewResult.DataSource = ds;
ListViewResult.DataBind();
}
}
}
else if (RadioButtonBride.Checked == true | RadioButtonBride.Checked == false)
{
if ((From == 21) && (To == 22))
{
var FoundBride = Data.Females.Where(Females => (Females.Age == 21 || Females.Age == 22) && (Females.Education == Education || Education.Length == 0) && (Females.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
if (!FoundBride.Any())
{
Response.Write("<script>alert('Search result Negetive');</script>");
}
else
{
SqlCommand cmd = Data.GetCommand(FoundBride) as SqlCommand;
SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
DataAdapter.Fill(ds);
ListViewResult.DataSource = ds;
ListViewResult.DataBind();
}
}
else if ((From == 21) && (To == 23))
{
var FoundBride = Data.Females.Where(Females => (Females.Age == 21 || Females.Age == 22 || Females.Age == 23) && (Females.Education == Education || Education.Length == 0) && (Females.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
if (!FoundBride.Any())
{
Response.Write("<script>alert('Search result Negetive');</script>");
}
else
{
SqlCommand cmd = Data.GetCommand(FoundBride) as SqlCommand;
SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
DataAdapter.Fill(ds);
ListViewResult.DataSource = ds;
ListViewResult.DataBind();
}
}
else if ((From == 21) && (To == 24))
{
var FoundBride = Data.Females.Where(Females => (Females.Age == 21 || Females.Age == 22 || Females.Age == 23 || Females.Age == 24) && (Females.Education == Education || Education.Length == 0) && (Females.CurrentStatus == MaritalStatus || MaritalStatus.Length == 0));
if (!FoundBride.Any())
{
Response.Write("<script>alert('Search result Negetive');</script>");
}
else
{
SqlCommand cmd = Data.GetCommand(FoundBride) as SqlCommand;
SqlDataAdapter DataAdapter = new SqlDataAdapter(cmd);
DataAdapter.Fill(ds);
ListViewResult.DataSource = ds;
ListViewResult.DataBind();
}
}
}
}
}