【问题标题】:Cannot get value of UpdatePanel element无法获取 UpdatePanel 元素的值
【发布时间】:2011-05-25 08:44:31
【问题描述】:

我在更新面板中有一个中继器。当我点击“Find Books”按钮时,repeater会根据查询进行修改。

我想要的是当我点击 Add To Cart 按钮时获取 DropDown 的值。但我无法获得该值,而是 UpdatePanelAnimationExtender 再次运行。我在更新面板中添加了另一个按钮 (TestingButton)。当我单击该按钮时,UpdatePanelAnimationExtender 也会运行。

我的问题是如何将更新面板仅限于“查找图书”按钮?以及如何在更新面板之外检索 DropDown 的值?我将 UpdateMode 添加到条件中,但它也不起作用。

aspx 页面

        <p>Search by Author</p>
    <asp:TextBox ID="txtAuthor" runat="server" Text="" AutoComplete="off" /><br />
    <p>Search by Publisher</p>
    <asp:TextBox ID="txtPublisher" runat="server" Text="" AutoComplete="off" /><br />
    <asp:Button ID="btnFind" runat="server" Text="Find Books" OnClick="BtnFind_Click" />
    <br />
    <p>Search by Price</p>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        <asp:ListItem Text="Below Rs. 1000.00"></asp:ListItem>
        <asp:ListItem Text="Between Rs. 1001 to 2500"></asp:ListItem>
        <asp:ListItem Text="Above Rs. 2501.00"></asp:ListItem>
    </asp:RadioButtonList>
    <br />
    <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
            <asp:Button ID="Button2" runat="server" Text="Button" 
        onclick="Button1_Click" />


    <asp:UpdatePanel ID="udpBooks" UpdateMode="Conditional"  runat="server">
        <ContentTemplate>
            <asp:Button ID="btnTest" runat="server" Text="Testing Button" onclick="Button1_Click" />
            <asp:Repeater ID="repBooks" runat="server" DataSourceID="SqlDataSource1" 
                onitemcommand="repBooks_ItemCommand">
                <HeaderTemplate>
                </HeaderTemplate>
                <ItemTemplate>
                    <div id="bookBorder">
                        <table width="640px" height="70px" border="0">
                            <tr>
                                <td width="51%" style="padding-top: -20px">
                                    <span class="title">Description:</span> :
                                    <%# DataBinder.Eval(Container.DataItem, "description")%>
                                </td>
                                <td>
                                    <asp:DropDownList ID="DropDownList2" CssClass="dropDown" runat="server" Width="100px">
                                        <asp:ListItem>1</asp:ListItem>
                                        <asp:ListItem>2</asp:ListItem>
                                        <asp:ListItem>3</asp:ListItem>
                                    </asp:DropDownList>
                                    <br />
                                    <asp:Button ID="btnAddToCart" CssClass="button" runat="server" Text="Add to Cart"
                                        OnClick="BtnAddToCart_Click" />
                                </td>
                            </tr>
                        </table>
                    </div>
                    <br />
                </ItemTemplate>
            </asp:Repeater>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnFind" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:UpdatePanelAnimationExtender ID="upae1" runat="server" TargetControlID="udpBooks">
        <Animations>
                 <OnUpdating>
                    <Parallel Duration="0">
                        <EnableAction AnimationTarget="btnFind" Enabled="false" />
                        <FadeOut MinimumOpacity=".5" />
                    </Parallel>
                </OnUpdating>
                <OnUpdated>
                    <Parallel Duration="0">
                        <FadeIn MinimumOpacity=".8" />
                        <EnableAction AnimationTarget="btnFind" Enabled="true" />
                    </Parallel>                    
                </OnUpdated>
        </Animations>
    </asp:UpdatePanelAnimationExtender>

代码背后

    public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SqldsOrderDetails_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        string author = txtAuthor.Text;
        string publisher = txtPublisher.Text;
        string select = "";


        if (author.Equals("") && publisher.Equals(""))
            select = "select bookId, ISBN, Title, publisher, author, price, description from dbo.[book]";
        else if(author.Equals("") && !publisher.Equals(""))
            select = "select bookId, ISBN, Title, publisher, author, price, description from dbo.[book] where publisher=@publisher ";
        else if(!author.Equals("") && publisher.Equals(""))
            select = "select bookId, ISBN, Title, publisher, author, price, description from dbo.[book] where author=@author";
        else if(!author.Equals("") && !publisher.Equals(""))
            select = "select bookId, ISBN, Title, publisher, author, price, description from dbo.[book] where author=@author and publisher=@publisher";

        SqlDataSource1.SelectCommand = select;
        e.Command.Parameters.Add(new SqlParameter("@author", this.txtAuthor.Text));
        e.Command.Parameters.Add(new SqlParameter("@publisher", this.txtPublisher.Text));
    }
    protected void BtnFind_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(2000);
//        this.gvOrderDetails.DataBind();
        this.repBooks.DataBind();
    }
    protected void ul_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }

    protected void BtnAddToCart_Click(object sender, EventArgs e)
    {
        txtTest.Text = "sd";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        txtTest.Text = "sd";
    }
    protected void repBooks_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        DropDownList d = e.Item.FindControl("DropDownList2") as DropDownList;
        txtTest.Text = d.SelectedValue;
        Button b= e.Item.FindControl("btnAddToCart") as Button;
        b.Text = d.SelectedValue;
    }
}

【问题讨论】:

    标签: asp.net updatepanel repeater


    【解决方案1】:

    用于触发

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            Some content that needs to be updated here...
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="FindBook" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
    

    就DropDown到文本框的值而言,您应该将文本框放在另一个更新面板中,并在用户通过调用更新方法从下拉列表中选择值时设置其值。

    【讨论】:

    • 好吧,如果你看到我的代码,我已经这样做了。但即使我按下 UpdatePanel 内的按钮(图像中的测试按钮),它也会触发
    • @Nipuna Silva:我在问你在触发器中使用的btnFind 在哪里,你在代码中创建它,它不在你给出的代码中。
    • @Thomas:- 我将文本框放在另一个 UpdatePanel 中,它起作用了!你能告诉我如何将 asp:UpdatePanelAnimationExtender 限制为“查找书籍”按钮吗? (如果可能的话)
    • @Nipuna Silva:asp:UpdatePanelAnimationExtender 在更新面板更新时触发,它绑定到更新面板而不是按钮。所以每次更新更新面板时都会触发。
    猜你喜欢
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2019-07-06
    • 2019-11-28
    相关资源
    最近更新 更多