简体   繁体   English

如何在同一页面上的两个中继器之间传递值

[英]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! 我已经通过在第一个转发器中放置超链接来实现我的想法,该超链接通过-使用它的NavigateUrl属性-同一页面的URL,但带有一些querystring值!

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 有人告诉我查询字符串用于将值从一个页面传递到另一页面,而不是同一页面...而且我也想使用一些AJAX(脚本管理器和更新面板),但事实证明我必须使用一些服务器中继器中的控件而不是html控件,这样它就可以回发到服务器

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! 希望您能为我提供一个很好的实现方法,以将值从Repeater1传递给Repeater2,如果您认为我说错了,请纠正我!

Thanks in advance :) 提前致谢 :)

EDIT 编辑

my.aspx.cs 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 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?) 编译器错误消息:CS1061:'ASP.sortingcontrol_ascx'不包含'arrange_lnkbtn_command'的定义,并且找不到扩展方法'arrange_lnkbtn_command'接受类型为'ASP.sortingcontrol_ascx'的第一个参数(是否缺少using指令或组装参考?)

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. 使用查询字符串将数据传递到同一页面本身没有任何错误,尽管不应将其用于用户可能会通过“入侵” 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). 但是,这不是在ASP.NET WebForms中执行操作的标准方法,因为它不会回发页面上任何其他控件的值(因此,除非您手动重新填充它们,否则它们将不会保留其状态。每次加载时)。 But if this is not a problem for you, feel free to stick with querystring. 但是,如果这对您来说不是问题,请随时使用querystring。

Howecer, generally you would use a server-side control like an asp:Button or an asp:LinkButton and then handle their OnClick event. 然而, 通常,您会使用服务器端控件(例如asp:Buttonasp:LinkBut​​ton) ,然后处理其OnClick事件。 However, to pass data with one you can use the CommandName and CommandArgument properties of the button and then handle the OnCommand event. 但是,要传递一个数据,可以使用按钮的CommandNameCommandArgument属性,然后处理OnCommand事件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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