如何在同一页面上的两个重复之间传递值

时间:2022-01-15 10:02:34

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但具有一些查询字符串值!

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,如果你认为我说错了请Corret Me!

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'的定义,并且没有可以找到接受类型'ASP.sortingcontrol_ascx'的第一个参数的扩展方法'arrange_lnkbtn_command'(您是否缺少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>

1 个解决方案

#1


1  

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.

使用查询字符串将数据传递到同一页面本身没有任何错误,但它不应该用于用户可能通过“黑客”URL更改的敏感数据。但是,它不是ASP.NET WebForms中的标准处理方式,因为它不会回发页面上任何其他控件的值(因此,如果不手动重新填充它们,它们将不会保留其状态在每页加载)。但如果这对你来说不是问题,请随意坚持使用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:Button或asp:LinkBut​​ton这样的服务器端控件,然后处理它们的OnClick事件。但是,要使用一个数据传递数据,您可以使用该按钮的CommandName和CommandArgument属性,然后处理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();
  }

#1


1  

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.

使用查询字符串将数据传递到同一页面本身没有任何错误,但它不应该用于用户可能通过“黑客”URL更改的敏感数据。但是,它不是ASP.NET WebForms中的标准处理方式,因为它不会回发页面上任何其他控件的值(因此,如果不手动重新填充它们,它们将不会保留其状态在每页加载)。但如果这对你来说不是问题,请随意坚持使用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:Button或asp:LinkBut​​ton这样的服务器端控件,然后处理它们的OnClick事件。但是,要使用一个数据传递数据,您可以使用该按钮的CommandName和CommandArgument属性,然后处理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();
  }