开发者

how to pass values between two repeateres on the same page

I have two repeaters that looks like this :

Arrange By: Name | Age | Area

what I want to do now is to populate the second repeater from the database with the alphabet, and if the age was clicked then the second repeater gets populated from the database with some age ranges!

I already implemented my idea by placing a hyperlink in the first repeater, that passes -using it's NavigateUrl property- a url for the same page but with some querystring values!

I've been told that querystrings are used for passing values from one page to another, not the same page...and also I wanted to use some AJAX (script manager and update panel) but turns out that I have to use some server control within my repeater instead of html controls so it could be able to post back to the server

I hope you'd help me with a good implementation for passing the value from repeater1 to repeater2 and if you think that I've stated anything wrong Please Corret Me!

Thanks in advance :)

EDIT

my.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            string commandString1 = "SELECT [arrange_by_id], [arrange_by] FROM [arrange_by]";


            using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
            {
                using (SqlCommand cm = new SqlCommand(commandString1, cn))
                {
                    cn.Open();

                    using (SqlDataReader dr = cm.ExecuteReader())
                    {
                        ArrangeBy_rpt.DataSource = dr;
                        ArrangeBy_rpt.DataBind();
                    }
                }
            }
          }

    void arrange_lnkbtn_command(object sender, CommandEventArgs e)
    {
         Label1.Text = (e.CommandArgument).ToString();
    }

my.aspx

    <asp:Repeater ID="ArrangeBy_rpt" runat="server">
       <ItemTemplate>
          <asp:LinkButton id="arrange_lnkbtn" OnCommand="arrange_lnkbtn_command" CommandArgument='<%#Eval("arrange_by_id") %>' runat="server">
          <%# Eval("arrange_by") %>
          </asp:LinkButton>
       </ItemTemplate>
       <SeparatorTemplate> | </SeparatorTemplate>
    </asp:Repeater>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Compilation Error

Compiler Error Message: CS1061: 'ASP.sortingcontrol_ascx' does not contain a definition for 'arrange_lnkbtn_command' and no extension method 'arrange_lnkbtn_command' accepting a first argument of type 'ASP.sortingcontrol_ascx' could be found (are you missing a using directive or an assembly reference?)

Line 4:  <asp:Repeater ID="ArrangeBy_rpt" runat="server">
Line 5:     <ItemTemplate>
Line 6:        <asp:LinkButton id="arrange_lnkbtn" OnCommand="arrange_lnkbtn_command" CommandArgument='<%#Eval("arrange开发者_如何学编程_by_id") %>' runat="server">
Line 7:        <%# Eval("arrange_by") %>
Line 8:        </asp:LinkButton>


There's nothing wrong per se with passing data to the same page using the querystring, though it shouldn't be used for sensitive data that the user might change by "hacking" the URL. However, it's not the standard way of doing things in ASP.NET WebForms, since it won't postback the values of any other controls on the page (and hence they won't retain their state without you manually having to re-populate them on every page load). But if this is not a problem for you, feel free to stick with querystring.

Howecer, generally you would use a server-side control like an asp:Button or an asp:LinkButton and then handle their OnClick event. However, to pass data with one you can use the CommandName and CommandArgument properties of the button and then handle the OnCommand event.

 <asp:LinkButton id="LinkButton1" 
           Text="Click Me"
           CommandName="Age" 
           CommandArgument="18" 
           OnCommand="LinkButton_Command" 
           runat="server"/>

In code-behind:

  void LinkButton_Command(Object sender, CommandEventArgs e) 
  {
     var data = GetData(e.commandArgument); // implement a method that gets your data filtered by the argument passed from the button
     Repeater2.DataSource = data;
     Repeater2.DataBind();
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜