0

I'm creating a dynamic menu that fetches dishes from a database and displays them to the user so he can add them to his order. Whenever I retrieve a row from the database I send it to my menuItems class so the columns can be read and the display format is created and inserted into a cell:

 foreach (DataRow dr in drc){
            //extract the dish data from the row into a cell
            TableCell c1 = menuItem.makeItemCell(dr);
            //display it
            tr.Controls.Add(c1);
            table1.Controls.Add(tr);
}

c1 will be a cell that has the dish information along with a quantity dropdownlist and an "add to order" button:

      public static TableCell makeItemCell(DataRow dr)
    {
        TableCell tc = new TableCell();
        Image img = new Image();

        img.ImageUrl = (string)dr["img"];
        tc.Controls.Add(img);

        //rest of information adding is omitted for brevity
        String myLiteral = "<p>" + "Name: " + (string)dr["NameofDish"] + "</br>";
        tc.Controls.Add(new LiteralControl(myLiteral));

        //add quantity label and dropdownlist populated with possible quantity values
        Label lb = new Label();
        lb.Text = "Quantity: ";
        DropDownList ddl = new DropDownList();
        for (int i = 0; i < 13; i++)
        {
            String t = i.ToString();
            ddl.Items.Add(new ListItem(t, t));
        }

        //create the add to order button
        Button btn = new Button();
        btn.Text = "Add to order";

        tc.Controls.Add(lb);
        tc.Controls.Add(ddl);
        tc.Controls.Add(btn);

        return tc;
    }

My problem is with handling the button click event. All my tries to bind it with an event handler (on the menuItems class or the codebehind itself) have failed. I could only manage to update the postbackURL of the button when it's created in the cell like this

btn.PostBackUrl = "./menu.aspx?dish=" + (string)dr["ID"] + "&quantity=" + ddl.SelectedValue;

But the .selectedvalue value is rendered when the cell is created hence is always 0

My question is this: how can I handle the button click event in a way that I can get hold of the dishID and the selected value.

((((I would prefer an event handling solution rather than querystrings if possible))))

Thank you!

EDIT: My whole problem is that the cell and its components are created in the external class and sent back to my code. How do I access the dropdownlist variable from my codebehind if I don't have the element id or anything

Sara Bahis
  • 71
  • 4
  • What is the name/id of your select list? – colecmc Apr 28 '15 at 20:49
  • possible duplicate of [Get selected value in dropdown list using JavaScript?](http://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript) – colecmc Apr 28 '15 at 20:51

0 Answers0