[RESOLVED] Textboxes and Calendar Controls

How would I create a row by column structure dynamically that has column 1 as text boxes and column 2 as calendar controls? The number of rows is some positive integer. Is it best to store these controls in a grid?

What would the code look like?

Thanks, Dave

drl

has column 1 as text boxes and column 2 as calendar controls? 

You can use a GridView with two template fields.

<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And you need to add your code for databinding the gridview or setting text/selected date for calendar.

Santhosh, thank you for your reply. Now how would I access say the textboxes in each row of the grid? Do they all have the same name "TextBox1"? Then which is which?

Please show a line or two of code to access the textboxes in the grid. Thanks. 

If you want to show just few rows of empty textboxes and calendar, you need to bind Gridview to an integer array.

//Just to bind 5 empty rows of data
GridView1.DataSource = new[] { 1, 2, 3, 4, 5 };
GridView1.DataBind();

If you want to access values from GridView, you can do this

//Retrieving values
foreach (GridViewRow row in GridView1.Rows)
{
    TextBox TextBox1 = (TextBox)row.FindControl("TextBox1");
    Calendar Calendar1 = (Calendar)row.FindControl("Calendar1");

    //values
    string text = TextBox1.Text;
    string date = Calendar1.SelectedDate.ToShortDateString();
    //do something with values here
}

Change ControlIDs in the FindControl according to your gridview markup. Try that way and see if it works.

GridView1.DataSource = new[] { 1, 2, 3, 4, 5 };

The compiler did not like this line - type expected at the "["... any suggestions? Thanks. 

drl

type expected at the "["

Ok, just specify int.

//Just to bind 5 empty rows of data
GridView1.DataSource = new int[] { 1, 2, 3, 4, 5 };
GridView1.DataBind();

GridView1.DataSource = new int[] { 1, 2, 3, 4, 5 };

GridView1.DataBind();

I ran just that much and the textboxes are clear. They have no value.

What's up?

drl

I ran just that much and the textboxes are clear. They have no value.

Can you pls explain clearly what you are tyring to do. TextBox will not contain any text as such unless until you specify which property to bind to using Text Ex: Text='<%# Eval("Some Property")%>'

I thought you are just trying to present a tabular Data Entry screen wherein user enters value and picks a date. If that's not the requirement, explain what you are trying to do.

"I thought you are just trying to present a tabular Data Entry screen wherein user enters value and picks a date." 

Yes, this is what I want with the addition of when the page first displays, the textboxes are filled with the integers initialized in the Gridview1.Datasource = new int[] = {1,2,3,4,5} statement.

drl

when the page first displays, the textboxes are filled with the integers

Check this

<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Number" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox runat="server" Text='<%# Container.DataItem %>' ID="TextBox1"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Just to bind 5 empty rows of data
        GridView1.DataSource = new int[] { 1, 2, 3, 4, 5 };
        GridView1.DataBind();
    }
}

Works perfectly, thanks!

thoughts on "[RESOLVED] Textboxes and Calendar Controls"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>