How do I access a Controls in a DataList

I am testing code for my project.

I have a DataList in a <form>
In that DataList, I have two Controls.
One Textbox, and one DropDownList
This is in C#.

I want to access the Controls and populate them with data from a hashtable and a SqlDb Connection.
I understand the DataList1.DataSource and DataList1.BindData();  Can I get this to work with both?

Can someone help me see the proper method of choice to do this in C# and to access the DropDownList?

Code is incomplete.

<%@ Page Language="c#" runat="server" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<script language="c#" runat="server">
      public string sTextBox1;

      void Page_Load() {
            if (IsPostBack) {
            } else {
                  if (Request.QueryString["cmd"] == "create") {
                        sTextBox1 = "test";
                        dl2.Visible=true;
                        dl1.Visible=false;
                        Hashtable hTable = new Hashtable();
                        hTable.Add("PressID", "1234");
                        dl2.DataSource = hTable;
                        dl2.DataBind();
                  } else {
                        dl1.Visible = true;
                        dl1.DataSource = "";
                        dl1.DataBind();
                  }
            }
      }
</script>
<html>
<body>
<asp:DataList ID="dl1" ShowHeader="true" runat="server" ExtractTemplateRows="false">
      <headertemplate>
            <asp:Table runat="server">
                  <asp:TableRow runat="server">
                        <asp:TableCell runat="server">
                              <asp:HyperLink ID="hyperlink1" runat="server" NavigateUrl="test2.aspx?cmd=create" Text="Please Create" />
                        </asp:TableCell>
                  </asp:TableRow>
            </asp:Table>
      </headertemplate>
</asp:DataList>

<form id="myform" name="myform" runat="server">
<asp:DataList ID="dl2" ShowHeader="true" runat="server" ExtractTemplateRows="true" Visible="false">
      <headertemplate>
            <asp:Table runat="server">
                  <asp:TableRow runat="server">
                        <asp:TableCell runat="server">
                              <asp:TextBox ID="textbox1" runat="server" Text="<%#sTextBox1%>" /><br>
                              <asp:TextBox ID="textbox2" runat="server" Text='<%#((Customer)Container.DataItem).PressID%>' /><br>
                              <asp:DropDownList ID="ddl" runat="server">
                                    <asp:ListItem Text="January" Value="1" />
                                    <asp:ListItem Text="February" Value="2" />
                                    <asp:ListItem Text="March" Value="3" />
                                    <asp:ListItem Text="April" Value="4" />
                                    <asp:ListItem Text="May" Value="5" />
                                    <asp:ListItem Text="June" Value="6" />
                                    <asp:ListItem Text="July" Value="7" />
                                    <asp:ListItem Text="August" Value="8" />
                                    <asp:ListItem Text="September" Value="9" />
                                    <asp:ListItem Text="October" Value="10" />
                                    <asp:ListItem Text="November" Value="11" />
                                    <asp:ListItem Text="December" Value="12" />
                              </asp:DropDownList>
                        </asp:TableCell>
                  </asp:TableRow>
            </asp:Table>
      </headertemplate>
</asp:DataList>
</form>
</body>
</html>








Thanks,
LPPARKER
lpparkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yassercomCommented:
Hi
this is because all of your code is in the header template. the header template is used to display headers of columns. you should use itemTemplate and alternative itemTemplate tags in your DataGrid tags. and bind your data in these tags

Regards
rinkujainCommented:
hi
first thing i wanna tell you is we should use datalist/datagrid controls when we have more than 1 set of records but as in your case u r applying it on header means only one time it will display, so better you directly define textbox/dropdown on table row.
But continue with your ques, on DataBound Event (which calls after each row of dataset fills), i am fetching reference to textbox and dropdown, so then you can dealt with them as normal textbox/dropdown.


      private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
            {
                  if (e.Item.ItemType=ListItemType.Header)
                  {
                        DropDownList ddl ;
                        TextBox txt1;
                        ddl =(DropDownList)(e.Item.FindControl("ddlName"));
                        txt1 =(TextBox)(e.Item.FindControl("TextBoxName"));
//Now do whatever operation you want these control
                  }
            }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lpparkerAuthor Commented:
Hello rinkujain,

You answer seems closer where I am going.  However, I have another question... How do I access the DataList if the DataList is not the object being the sender?  For example, how do I access if I call the test2.aspx from test.aspx?

Thanks,
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

rinkujainCommented:
what you want exactly?
lpparkerAuthor Commented:
1)  Well, what I really want (i believe) is a <form> with controls in it for a user to create a new record.  The new record (form) should auto select the current Month.  I call the page like this page.aspx?cmd=create.  But if I did call it like this I don't have access to "object sender" and the args required to do "e.Item.FinControl".... Is this why you suggest me to use a <form> tag with no DataList?

2) Is there a way to use both Hashtable and a DataSet on the same control?
rinkujainCommented:
If you want to pass only few values from page1 to page2 then u can use querystring for that else you can access one form control values from other like

--onclick of button where you want to call other form
Server.Transfer("Page2.aspx");


then on page2:
Response.Write(Request.Form["TextBox1"] );
lpparkerAuthor Commented:
Thanks, it seems that there were  combinations of questions and you solved them all.

LP
rinkujainCommented:
thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.