简体   繁体   中英

Setting a property of an asp control and reloading the page with changes

I have a complex problem but I'll try to explain it in a simple manner. I have a master page with a LinkButton . On clicking that button it changes the DataSourceID property of ListView in the content page. The problem is that after changing the property value I load the page using Response.Redirect("contentpage.aspx"); but this does not load the page with the changes I made in the property.

MasterPage.aspx partial code

<asp:LinkButton 
                    ID="Link_ClothesMen" 
                    runat="server" 
                    OnClick="Link_ClothesMen_Click">Men</asp:LinkButton>

MasterPage.aspx.cs partial code

protected void Link_ClothesMen_Click (object sender,EventArgs e)
    {

        ListView ListViewTemp = (ListView)ContentPlaceHolder1.FindControl("ListView1");
        ListViewTemp.DataSourceID = "SqlDataSourceClothesMen";
        Response.Redirect("ContentPage.aspx"); //This is wrong. Help here
    }

ContentPage.aspx partial code

<asp:SqlDataSource ID="SqlDataSourceClothesMen" runat="server" 
            ConnectionString="Data Source=DESKTOP-1EGF4SE\SQLEXPRESS;Initial Catalog=clickstream;Integrated Security=True" 
            SelectCommand="select * from [ClothesMen]"></asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataSourceID="" <!--I'm changing this property-->
             GroupItemCount="3">

            <LayoutTemplate>

                <table style="table-layout:fixed;width:100%">
                    <tr id="groupPlaceholder" runat="server"></tr>
                </table>
            </LayoutTemplate>

            <GroupTemplate>

                <tr>
                    <td id="itemPlaceholder" runat="server">

                    </td>
                </tr>
            </GroupTemplate>

            <ItemTemplate>
                <td align="center">
                    <asp:Image runat="server" ImageUrl='<%# Eval("ImageUrl") %>' Height="20%" Width="70%" /><br />
                    <asp:Label ID="ProductTitleLabel" runat="server" Text='<%# Eval("ProductTitle") %>'></asp:Label><br />
                    <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>'></asp:Label><br />

                </td>
            </ItemTemplate>

            <GroupSeparatorTemplate>
                <tr runat="server">
                    <td colspan="3"><hr /></td>
                </tr>
            </GroupSeparatorTemplate>
        </asp:ListView>

Please help me out. I hope you got a clear idea about the problem.

Instead of redirecting (and losing the change since the control state is not transferred on a response redirection), you should only need to rebind the list view.

ListViewTemp.DataBind();

The button click is causing a postback to the server, so there shouldn't be a need for a full redirect if this is the only element on the page you're trying to update for the view.

I figured it out. There is no need of Response.Redirect("ContentPage.aspx"); Clicking on the LinkButton itself refreshes the page with changes. It's solved. I should've thought of this before posting this question. Thanks anyway!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM