C# 更新面板问题

C# 更新面板问题,c#,asp.net,C#,Asp.net,我有一个下拉菜单,里面有表单加载时db中的值。我想使用下拉菜单作为过滤器,使用updatepanel更新datagrid,我成功地实现了这一点。我正在表单加载中绑定datagrid,并在所选索引上进行更改,如图所示。我还想使用相同的下拉列表来更新使用DataValueField的formview,因为某些原因,它不起作用。原因可能是我使用了两个不同的UpdatePanel和同一个asynchpostbacktrigger吗?我应该在哪里将数据绑定到formview?您的意见将不胜感激!先谢谢你

我有一个下拉菜单,里面有表单加载时db中的值。我想使用下拉菜单作为过滤器,使用updatepanel更新datagrid,我成功地实现了这一点。我正在表单加载中绑定datagrid,并在所选索引上进行更改,如图所示。我还想使用相同的下拉列表来更新使用DataValueField的formview,因为某些原因,它不起作用。原因可能是我使用了两个不同的UpdatePanel和同一个asynchpostbacktrigger吗?我应该在哪里将数据绑定到formview?您的意见将不胜感激!先谢谢你

protected void Page_Load(object sender, EventArgs e)
{
       SqlConnection conn = new SqlConnection(GetConnectionString());
        conn.Open();

        SqlCommand cmd3 = new SqlCommand("SELECT c.Email As CompanyEmail, c.Telephone AS Telephone, m.Email AS AdminEmail, m.FirstName + ' ' + m.LastName AS CompanyAdmin FROM Company c, Member m WHERE m.CompanyID = c.CompanyID AND m.CompanyID = '" + company_id + "' AND m.CompanyRole = 'Admin'", conn);
        SqlCommand cmd = new SqlCommand("SELECT CompanyName, CompanyID FROM Company ORDER BY CompanyName", conn);
        SqlCommand cmd2 = new SqlCommand("SELECT p.ProjectName AS ProjectName, p.ProjectID, p.CompanyID, p.Status AS Status FROM Project p, Company c WHERE p.CompanyID = c.CompanyID AND c.CompanyID = '" + company_id + "' ORDER BY ProjectName", conn);

        cmd.CommandType = CommandType.Text;

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        if (!Page.IsPostBack)
        {

            company_list.DataSource = ds;
            company_list.DataTextField = "CompanyName";
            company_list.DataValueField = "CompanyID";
            company_list.DataBind();

            company_list.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-- Please Select Company --"));

        }
        //cmd2.Connection.Open();

                cmd2.CommandType = CommandType.Text;

                SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd2);
                DataSet ds2 = new DataSet();
                sqlAdapter.Fill(ds2);

                Gridview1.DataSource = ds2;
                Gridview1.DataBind();

                FormView1.DataSource = cmd3.ExecuteReader();
                FormView1.DataBind();

                conn.Close();

            //}
        //cmd2.Connection.Close();
        //cmd2.Connection.Dispose();
    }

}
protected void company_list_SelectedIndexChanged(object sender, EventArgs e)
{

    company_id = company_list.SelectedValue;

    SqlConnection conn = new SqlConnection(GetConnectionString());
    conn.Open();


    SqlCommand cmd2 = new SqlCommand("SELECT p.ProjectName AS ProjectName, p.ProjectID, p.CompanyID, p.Status AS Status FROM Project p, Company c WHERE p.CompanyID = c.CompanyID AND c.CompanyID = '" + company_id + "' ORDER BY ProjectName", conn);


        cmd2.CommandType = CommandType.Text;

        SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd2);
        DataSet ds2 = new DataSet();
        sqlAdapter.Fill(ds2);

        Gridview1.DataSource = ds2;
        Gridview1.DataBind();

        SqlCommand cmd = new SqlCommand("SELECT c.Email As CompanyEmail, c.Telephone AS Telephone, m.Email AS AdminEmail, m.FirstName + ' ' + m.LastName AS CompanyAdmin FROM Company c, Member m WHERE m.CompanyID = c.CompanyID AND m.CompanyID = '" + company_id + "' AND m.CompanyRole = 'Admin'", conn);
        cmd.CommandType = CommandType.Text;

        FormView1.DataSource = cmd.ExecuteReader();
        FormView1.DataBind();

        conn.Close();


}
ASP.NET:

<div style="float:left;">
                       <table>
                        <tr>
                            <td class="style1">Select Company:</td>
                            <td><asp:DropDownList ID="company_list" runat="server" 
                               onselectedindexchanged="company_list_SelectedIndexChanged" width="185" AutoPostBack="true" /></td>
                        </tr>
                        <tr>
                            <td colspan="2" align="right">&nbsp;</td>
                        </tr>
                        </table>

                         <asp:UpdatePanel ID="UpdateInfo" runat="server">
                        <ContentTemplate>
                        <asp:FormView ID="FormView1" runat="server">
                        <ItemTemplate>
                        <table>
                        <tr>
                            <td>Company Admin:</td>
                            <td><asp:TextBox Text='<%# Eval("Email") %>' CssClass="input input1" ID="co_admin" width="150" runat="server" ReadOnly="True" /></td>
                        </tr>
                        <tr>
                            <td>Admin Email:</td>
                            <td><asp:TextBox Text='<%# Eval("AdminEmail") %>' CssClass="input input1" ID="ad_email" width="150" runat="server" ReadOnly="True" /></td>
                        </tr>
                        <tr>
                            <td>Company Email:</td>
                            <td><asp:TextBox Text='<%# Eval("CompanyEmail") %>' CssClass="input input1" ID="co_email" width="150" runat="server" ReadOnly="True" /></td>
                        </tr>
                        <tr>
                            <td>Telephone:</td>
                            <td><asp:TextBox Text='<%# Eval("Telephone") %>' CssClass="input input1" ID="telephone" width="150" runat="server" ReadOnly="True" /></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><asp:Button CssClass="button_style" width="170" ID="export" Text="EXPORT TO EXCEL" runat="server" /></td>
                        </tr>
                        </table>
                       </ItemTemplate>
                       </asp:FormView> 
                       </ContentTemplate>
                       <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="company_list" />
                        </Triggers>
                       </asp:UpdatePanel>

                       </div>

                       <center>


                            <asp:UpdatePanel ID="UpdateGrid" runat="server">
                <ContentTemplate>
                           <asp:gridview ID="Gridview1" runat="server" ShowFooter="True"
                       AutoGenerateColumns="False" GridLines="None">

                    <Columns>
                    <asp:TemplateField HeaderText="Project Name">
                    <ItemTemplate>
                        <asp:TextBox Text='<%# Eval("ProjectName") %>' CssClass="input input1" ID="project_name" width="150" runat="server" ReadOnly="True"></asp:TextBox>
                    </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Status">
                    <ItemStyle Width="150" />
                    <ItemTemplate>
                        <center><asp:Image ImageUrl='<%# GetStatusImage(Eval("Status").ToString()) %>' ID="status" runat="server"/></center>
                    </ItemTemplate>
               </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="company_list" />
                </Triggers>
                </asp:UpdatePanel>

我尝试用以下代码重新创建您的场景。它是有效的

protected void Page_Load(object sender, EventArgs e)
{

    var foos = GetFoos();
    Gridview1.DataSource = foos;
    Gridview1.DataBind();

    DetailsView1.DataSource = foos;
    DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
    DetailsView1.DataBind();

    DropDownList1.Items.AddRange(foos.Select(f => new ListItem(f.Name, f.Name)).ToArray());
    DropDownList1.Items.Insert(0,new ListItem("-Select-"));


}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    var selectedValue = DropDownList1.SelectedValue;

    var foos = GetFoos().Where(f => f.Name == selectedValue);

    Gridview1.DataSource = foos;
    Gridview1.DataBind();

    DetailsView1.DataSource = foos;
    DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
    DetailsView1.DataBind();
}


static List<Foo> GetFoos()
{
    var list = new List<Foo>();
    for(int i=0;i<10;i++)
    {
        list.Add(new Foo {City = "City" + i, Name = "Name" + i});
    }
    return list;
}

class Foo
{
    public string Name { get; set; }

    public string City { get; set; }
}
这是标记

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:GridView runat="server" ID="Gridview1">
        </asp:GridView>
        </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="DropDownList1" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px">
        </asp:DetailsView>

        </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="DropDownList1" />
        </Triggers>
    </asp:UpdatePanel>

</asp:Content>

所以,这是与formview有关的内容。您可以切换到使用DetailsView吗?

我尝试用以下代码重新创建您的场景。它是有效的

protected void Page_Load(object sender, EventArgs e)
{

    var foos = GetFoos();
    Gridview1.DataSource = foos;
    Gridview1.DataBind();

    DetailsView1.DataSource = foos;
    DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
    DetailsView1.DataBind();

    DropDownList1.Items.AddRange(foos.Select(f => new ListItem(f.Name, f.Name)).ToArray());
    DropDownList1.Items.Insert(0,new ListItem("-Select-"));


}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    var selectedValue = DropDownList1.SelectedValue;

    var foos = GetFoos().Where(f => f.Name == selectedValue);

    Gridview1.DataSource = foos;
    Gridview1.DataBind();

    DetailsView1.DataSource = foos;
    DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
    DetailsView1.DataBind();
}


static List<Foo> GetFoos()
{
    var list = new List<Foo>();
    for(int i=0;i<10;i++)
    {
        list.Add(new Foo {City = "City" + i, Name = "Name" + i});
    }
    return list;
}

class Foo
{
    public string Name { get; set; }

    public string City { get; set; }
}
这是标记

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:GridView runat="server" ID="Gridview1">
        </asp:GridView>
        </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="DropDownList1" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px">
        </asp:DetailsView>

        </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="DropDownList1" />
        </Triggers>
    </asp:UpdatePanel>

</asp:Content>

所以,这是与formview有关的内容。能否切换到使用DetailsView?

是的,我正在表单加载和selectedindexchange中绑定gridview和formview。该下拉列表旨在更新这两个UpdatePanel…是的,我正在表单加载和selectedindexchange中绑定gridview和formview。该下拉列表旨在更新这两个UpdatePanel…您在哪里绑定到FormView?我看到了Gridview1.DataBind,但FormView没有看到任何内容。您在哪里绑定到FormView?我看到Gridview1.DataBind,但FormView没有看到任何内容。