1

实际上,我正在尝试将数据列表中存在的按钮的命令参数重定向到另一个页面。我正在使用 Request.QueryString 方法在按钮的命令名称的帮助下访问另一个页面上的命令参数。请帮我解决它...

这是数据列表中存在的按钮代码

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

这是 DataList Item 命令函数中的代码

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

    }

这是onclick功能代码

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

这是另一个页面上的代码(content.aspx)

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

这是整个数据列表代码

    <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" />

它确实重定向到另一个页面(content.aspx),但标签不显示查询字符串文本。

4

4 回答 4

0

我认为您没有将 Request.QueryString["content"] 转换为字符串格式,试试这个

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

    
于 2020-07-25T08:43:37.560 回答
0

尝试将 DataList1_ItemCommand 事件更新为:

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

还要确保您在此页面代码后面的 Page_Load 方法中检查 IsPostBack。

于 2019-08-31T12:07:54.977 回答
0

是的,我得到了想要的输出。实际上,我还使用另一个按钮重定向到 DataList1_ItemCommand 函数中的不同页面。所以我只需要将 2 个按钮的 Response.redirects 放在 if 循环中即可。

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

    } 

谢谢大家帮助我解决这个问题

于 2019-09-01T03:42:05.437 回答
-1

您可以在本教程中找到所需的所有信息。来自@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>

顺便说一句,我认为您不需要定义 Button Click 事件。已用变量定义路径。只需将按钮转换为链接按钮并删除按钮点击事件

于 2019-08-31T11:44:04.620 回答