0

I'm a student and I have an issue with executing an ItemCommand for a data list inside a user control, the ItemCommand doesn't seem to run at all.

<asp:DataList ID="gridViewFormat" runat="server" DataKeyField="ArtworkId" DataSourceID="gridArtworkDetails" RepeatDirection="Horizontal" CellPadding="50" RepeatColumns="3" Width="100%" CellSpacing="20" OnItemCommand="gridViewFormat_ItemCommand" OnSelectedIndexChanged="gridViewFormat_SelectedIndexChanged">
        <ItemTemplate>
            <div class="crop border border-dark rounded-2 shadow m-auto mb-2">
                <asp:ImageButton ID="imgArtwork" runat="server" ImageUrl='<%# Eval("ArtworkImg") %>' Width="250px" CommandArgument='<%# Eval("ArtworkId") %>' CommandName="gridClick" />
                    
            </div>
                <h4><%# Eval("ArtworkName") %></h4>
                <h4>Artwork By: <%# Eval("artistUsername") %></h4>
                <small class="lead">RM <%# Eval("price")%></small>
        </ItemTemplate>
    </asp:DataList>

This is the Datalist that has an ImageButton which when pressed would activate the "gridClick" command.

protected void gridViewFormat_ItemCommand(object source, DataListCommandEventArgs e)
        {
            //Response.Write("Test");
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            if (e.CommandName == "gridClick")
            {
                int artworkID = Convert.ToInt32(e.CommandArgument.ToString());
                Session["SsArtworkId"] = artworkID;
                Response.Redirect("ViewProduct.aspx");
            }
        }

This is the C# code that would run when pressed, but for some reason it does not run at all.

I placed the user control inside a placeholder in another file called Gallery.aspx and run as such:

protected void Page_Load(object sender, EventArgs e)
{
    type = "Grid";

    if(!IsPostBack)
    {
        if(type == "Grid")
        {
            ctlControl = LoadControl("GridViewFormat.ascx");
        }
        else if(type == "List")
        {
            ctlControl = LoadControl("ListViewFormat.ascx");
        }

        ph1.Controls.Add(ctlControl);
    }
}

protected void btnGridView_Click(object sender, ImageClickEventArgs e)
{
    type = "Grid";
    ctlControl = LoadControl("GridViewFormat.ascx");
    ph1.Controls.Add(ctlControl);
}

The Datalist ItemCommand works fine if I'm not using a user control, but if I still wanted to use it inside a user control, what are my options to solve this issue? Thank you very much!

0 Answers0