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

2.7k views Asked by At

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.

4

There are 4 answers

1
Uzay On

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

8
Rashid Ali On

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.

0
Kunal Bagam On

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

0
Vinay K Chaubey On

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