1

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. 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

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

    }

this is the onclick function code

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

this is the code on another page(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.

Kunal Bagam
  • 13
  • 1
  • 5

4 Answers4

0

Try updating the DataList1_ItemCommand event to this:

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.

Rashid Ali
  • 587
  • 3
  • 13
  • Sir i have already binded DataList to OnItemCommand still its not working – Kunal Bagam Aug 31 '19 at 16:33
  • can you add the entire markup of DataList into the question? – Rashid Ali Aug 31 '19 at 16:44
  • Nope sir. It still redirects to another page but the label does not show any text.And i guess it redirects because of the specified postbackurl and DataList1_ItemCommand is not working in the case of redirecting to another page with command argument. – Kunal Bagam Aug 31 '19 at 19:03
  • I think Button1 click event is still there causing this, remove OnClick="Button1_Click" from Button1 Html – Rashid Ali Aug 31 '19 at 19:08
  • When you click Link Button, can you see value on address line when redirecting? – Uzay Aug 31 '19 at 19:48
  • @Rashid Ali I removed the onClick button from html. Now it does not even redirect to another page. – Kunal Bagam Sep 01 '19 at 03:06
  • @Uzay yes i can see the value on address line while redirecting by clicking on the Link Button. – Kunal Bagam Sep 01 '19 at 03:07
  • You first page sending value as you can see. Content.aspx not firing? put Break Point on Page Load event Content.aspx and check querystring name and value. – Uzay Sep 01 '19 at 09:04
0

Yes i got the desired output. Actually i was also using another button to redirect to a diiferent page in DataList1_ItemCommand function. So i just needed to seperate the Response.redirects of the 2 buttons by putting them in if loop.

     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

Kunal Bagam
  • 13
  • 1
  • 5
0

I think you are not casting the Request.QueryString["content"] to string format, try this

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

    
-1

You can find all information you need in this tutorial. from @microsoft msdn Best of luck

<%@ 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. Already defined path with variable. Just convert button to Link Button and remove Button Click Event

Uzay
  • 809
  • 9
  • 18
  • tried the link button method sir. still doesn't work. It does redirect to another page but the label does not show the query string text. – Kunal Bagam Aug 31 '19 at 18:48