简体   繁体   English

如何将数据列表中按钮的命令参数传递到另一个页面?

[英]How do i pass a command argument of a button in Data List to another page?

Actually i am trying to redirect command argument of a button present in a data list to another page.实际上,我正在尝试将数据列表中存在的按钮的命令参数重定向到另一个页面。 I am using Request.QueryString method to access the command argument on another page with the help of command name of the button.我正在使用 Request.QueryString 方法在按钮的命令名称的帮助下访问另一个页面上的命令参数。 Please help me with it...请帮我解决它...

this is code of button present inside Data List这是数据列表中的按钮代码

    <asp:Button ID="Button1" runat="server" Text="Read" CommandArgument='<%# Eval("id")%>' OnClick="Button1_Click"  CommandName="content"/>

this is code present in DataList Item command function这是存在于 DataList Item 命令函数中的代码

     protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        Response.Redirect("content.aspx?content=" +e.CommandArgument.ToString());

    }

this is the onclick function code这是onclick函数代码

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("content.aspx");
    }

this is the code on another page(content.aspx)这是另一个页面上的代码(content.aspx)

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String id = Request.QueryString["content"];
            Label1.Text = id;
        }                          
    }

this is entire datalist code这是整个数据列表代码

    <asp:DataList ID="DataList1" runat="server"  DataKeyField="Id" DataSourceID="SqlDataSource1" Height="657px" RepeatColumns="4" RepeatDirection="Horizontal" Width="1248px" OnItemCommand="DataList1_ItemCommand" OnItemDataBound="DataList1_ItemDataBound">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<ItemStyle ForeColor="#000066" />
<ItemTemplate>
    <table class="auto-style2">
        <tr>
            <td style="text-align: center">
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                &nbsp;&nbsp;&nbsp;
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("Id") %>' Visible="False"></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Image ID="Image2" runat="server" Height="250px" ImageUrl='<%# Eval("image") %>' Width="250px" />
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                <br />
                <asp:ImageButton ID="ImageButton1" runat="server" CommandName="addtofav" CommandArgument='<%# Eval("id")%>' Height="30px" Width="20px" />
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Button ID="Button1" runat="server" Text="Read" CommandArgument='<%# Eval("id")%>' OnClick="Button1_Click"  CommandName="content"/>
            </td>
        </tr>
    </table
    <br />
    <br />
</ItemTemplate>
<SelectedItemStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

it does redirect to another page(content.aspx) but the label does not show the querystring text.它确实重定向到另一个页面(content.aspx),但标签不显示查询字符串文本。

Try updating the DataList1_ItemCommand event to this:尝试将 DataList1_ItemCommand 事件更新为:

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
  if (e.CommandName == "content")
  {
    Response.Redirect("content.aspx?content=" +e.CommandArgument.ToString());
  }
}

also make sure you are checking IsPostBack in Page_Load method of this page code behind.还要确保您正在检查此页面代码后面的 Page_Load 方法中的 IsPostBack。

Yes i got the desired output.是的,我得到了想要的输出。 Actually i was also using another button to redirect to a diiferent page in DataList1_ItemCommand function.实际上,我还使用另一个按钮重定向到 DataList1_ItemCommand 函数中的不同页面。 So i just needed to seperate the Response.redirects of the 2 buttons by putting them in if loop.所以我只需要通过将它们放在 if 循环中来分离 2 个按钮的 Response.redirects。

     protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "content")
        {
            Response.Redirect("content.aspx?content="+e.CommandArgument.ToString());
        }
        if (e.CommandName == "addtofav")
        {
            Response.Redirect("sortbyAZ.aspx?addtofav=" + e.CommandArgument.ToString());
        }

    } 

Thanks You everybody for helping me out with this one谢谢大家帮我解决这个问题

I think you are not casting the Request.QueryString["content"] to string format, try this我认为您没有将 Request.QueryString["content"] 转换为字符串格式,试试这个

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        String id = Request.QueryString["content"];
        Label1.Text = id;
    }                          
}

    

You can find all information you need in this tutorial.您可以在本教程中找到所需的所有信息。 from @microsoft msdn Best of luck来自@microsoft msdn 祝你好运

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>DataList Select Example</title>
<script runat="server">

  ICollection CreateDataSource() 
  {

     // Create sample data for the DataList control.
     DataTable dt = new DataTable();
     DataRow dr;

     // Define the columns of the table.
     dt.Columns.Add(new DataColumn("Item", typeof(Int32)));
     dt.Columns.Add(new DataColumn("Qty", typeof(Int32)));
     dt.Columns.Add(new DataColumn("Price", typeof(double)));

     // Populate the table with sample values.
     for (int i = 0; i < 9; i++) 
     {
        dr = dt.NewRow();

        dr[0] = i;
        dr[1] = i * 2;
        dr[2] = 1.23 * (i + 1);

        dt.Rows.Add(dr);
     }

     DataView dv = new DataView(dt);
     return dv;

  }

  void Page_Load(Object sender, EventArgs e) 
  {

     // Load sample data only once, when the page is first loaded.
     if (!IsPostBack) 
     {
        ItemsList.DataSource = CreateDataSource();
        ItemsList.DataBind();
     }

  }

  void Item_Command(Object sender, DataListCommandEventArgs e) 
  {

     // Set the SelectedIndex property to select an item in the DataList.
     ItemsList.SelectedIndex = e.Item.ItemIndex;

     // Rebind the data source to the DataList to refresh the control.
     ItemsList.DataSource = CreateDataSource();
     ItemsList.DataBind();

  }

</script>

</head>
<body>

<form id="form1" runat="server">

 <h3>DataList Select Example</h3>

  Click <b>Select</b> to select an item.

 <br /><br />

  <asp:DataList id="ItemsList"
       GridLines="Both"
       CellPadding="3"
       CellSpacing="0"           
       OnItemCommand="Item_Command"
       runat="server">

     <HeaderStyle BackColor="#aaaadd">
     </HeaderStyle>

     <AlternatingItemStyle BackColor="Gainsboro">
     </AlternatingItemStyle>

     <SelectedItemStyle BackColor="Yellow">
     </SelectedItemStyle>

     <HeaderTemplate>

        Items

     </HeaderTemplate>

     <ItemTemplate>

        <asp:LinkButton id="SelectButton" 
             Text="Select" 
             CommandName="Select"
             runat="server"/>

        Item <%# DataBinder.Eval(Container.DataItem, "Item") %>

     </ItemTemplate>

     <SelectedItemTemplate>

        Item:
        <asp:Label id="ItemLabel" 
             Text='<%# DataBinder.Eval(Container.DataItem, "Item") %>' 
             runat="server"/>

        <br />

        Quantity:
        <asp:Label id="QtyLabel" 
             Text='<%# DataBinder.Eval(Container.DataItem, "Qty") %>' 
             runat="server"/>

        <br />

        Price:
        <asp:Label id="PriceLabel" 
             Text='<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") 
%>' 
             runat="server"/>

</SelectedItemTemplate>

</asp:DataList>

</form>
</body>
</html>

By the way I think you don't need define Button Click event.顺便说一句,我认为您不需要定义 Button Click 事件。 Already defined path with variable.已经用变量定义了路径。 Just convert button to Link Button and remove Button Click Event只需将按钮转换为链接按钮并删除按钮点击事件

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

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