0

如何将表绑定到ListView应该使用哪个控件, LinqDataSource或者应该使用ObjectDataSource什么DataSourceIDListView

我的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();
                    }
                }
            }
        }
    }
4

2 回答 2

1

我认为可能是:

ListViewResult.DataSource = ds;

利用

ListViewResult.DataSource = ds.Tables[0];

或者

ListViewResult.DataSource = ds.Tables("TableName");

请替换所有这些:

if(From = 21 && To = 24)
{
    (Males.Age == 21 || Males.Age == 22 || Males.Age == 23 || Males.Age == 24)
}

只是与

(Males.Age >= From && Males.Age <= To)
于 2013-06-06T16:37:53.193 回答
0

好的,我这样做了,我想在 ListView 上显示两个表的数据,这样我就不需要在一个页面上使用两个列表视图,

我所做的是,我插入了两个 ListViews 和两个 LinqDataSource 并附加了它们,然后这样做了。

 protected void Page_Load(object sender, EventArgs e)
    {

        if(!Page.IsPostBack)
        {

            ListViewGroom.Visible = false;
            ListViewBride.Visible = false;
        }
        else if(Page.IsPostBack)
        {
            if (RadioButtonGroom.Checked == true)
            {
                ListViewBride.Visible = false;
            }
            else if(RadioButtonBride.Checked == true)
            {
                ListViewGroom.Visible = false;
            }
        }
    }



    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);

            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())
                    {
                        ListViewGroom.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewGroom.Visible = true;
                        ListViewGroom.DataSourceID = "";
                        ListViewGroom.DataSource = FoundGroom;
                        ListViewGroom.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())
                    {
                        ListViewGroom.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewGroom.Visible = true;
                        ListViewGroom.DataSourceID = "";
                        ListViewGroom.DataSource = FoundGroom;
                        ListViewGroom.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())
                    {
                        ListViewGroom.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewGroom.Visible = true;
                        ListViewGroom.DataSourceID = "";
                        ListViewGroom.DataSource = FoundGroom;
                        ListViewGroom.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())
                    {
                        ListViewBride.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewBride.Visible = true;
                        ListViewBride.DataSourceID = "";
                        ListViewBride.DataSource = FoundBride;
                        ListViewBride.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())
                    {
                        ListViewBride.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewBride.Visible = true;
                        ListViewBride.DataSourceID = "";
                        ListViewBride.DataSource = FoundBride;
                        ListViewBride.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())
                    {
                        ListViewBride.Visible = false;
                        Response.Write("<script>alert('Search result Negetive');</script>");
                    }
                    else
                    {
                        ListViewBride.Visible = true;
                        ListViewBride.DataSourceID = "";
                        ListViewBride.DataSource = FoundBride;
                        ListViewBride.DataBind();
                    }
                }
            }
        }
    }

根据要求隐藏ListView。

于 2013-06-07T08:36:49.863 回答