ASP.NET C# controls help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djboyse
    New Member
    • Apr 2007
    • 9

    #1

    ASP.NET C# controls help

    Hi,

    I am dynamically creating a page that contains a table and within that table there is a drop down list and a button. The drop down list and button let the user rate something, so they select the rating from the list and click the button to rate.

    I have a function that creates the cells, rows and other control and returns a list of table rows.

    My problem is that as everything is dynamically created, i cant access the controls in the table.

    Heres the code

    Code:
    //page.aspx
    <asp:table ID="tbl" runat="server" />
    
    //page.aspx.cs
    
    protected void Page_Load(object sender, EventArgs e)
    {
       //Add each row to the table
       foreach(TableRow row in CreateTable())
       {
          tbl.Rows.Add(row);
       }
    }
    
    private List<TableRow> CreateTable()
    {
       //Create the row collection
       List<TableRow> rowCollection = new List<TableRow>();
    
       //Create the table rows
       TableRow tblRowRateLabel = new TableRow();
       TableRow tblRowRate = new TableRow();
    
       //Create the cells
       TableCell tblCellRateLabel = new TableCell();
       TableCell tblCellRate = new TableCell();
    
       //Add the data to the cells
       //Create the text and add to cell
       tblCellRateLable.Controls.Add(new LiteralControl("Rate"));
    
       //Create the drop down list
       DropDownList ddlRating = new DropDownList();
       for(int i = 0; i < 5; i++)
       {
          ListItem item = new ListItem((i + 1).ToString(), (i + 1).ToString);
          ddlRating.Items.Add(item);
       }
    
       //Create the button
       Button btnRate = new Button();
       btnRate.Text = "Rate";
       btnRate.Command += new CommandEventHandler(ProcessRating);
       
       //Add button and list to the cell
       tblCellRate.Controls.Add(ddlRating);
       tblCellRate.Controls.Add(btnRate);
    
       //Add the cells to the rows
       tblRowRateLabel.Cells.Add(tblCellRateLabel);
       tblRowRate.Cells.Add(tblCellRate);
    
       //Add the rows to the rowCollection
       rowCollection.Add(tblRowRateLabel);
       rowCollection.Add(tblRowRate);
    
       //Return the row collection
       return rowCollection
    }
    
    public void ProcessRating(object o, CommandEventArgs e)
    {
       //HELP - What do I do here?
    }
    Can any one tell me how i can access the drop down list (ddlRating) when the button is clicked please

    Thanks
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    have you tried the find control?

    if not, try to put it as a request variable

    Comment

    Working...