1

I am doing a crossword puzzle and i have 100 text boxes in a panel like this : enter image description here

Every text box have an id of 00 - 99 since there is 100 of it .

enter image description here

First Row will have an id 00-09 , 2nd row will have an id of 10-19 and so on.

When user types something in some text box will be null and some text box will have values in it. How do I save values from a text box of a certain id to a database? For example the above image, HELP, text box id of 22 will have the value H , id of 23 will have the value of E , id of 24 will have value of L , id of 25 will have value of P.

I don't want to save the null values of the text box , I want to save values of the textboxes which are not null. I also need to take into account their textbox ids so that when I populate them back, I just have to insert them through ID .

I am new to C# , appreciate any help/advise/solutions on this.

Here is my code:

protected void Page_Load(object sender, EventArgs e)
{
    //hw.Write("<table>");
    for (int i = 0; i <= 9; i++)
    {
        //hw.Write("<tr>");
        for (int j = 0; j <= 9; j++)
        {
            TextBox tb = new TextBox();
            tb.MaxLength = (1);
            tb.Width = Unit.Pixel(40);
            tb.Height = Unit.Pixel(40);
            tb.ID = i.ToString() + j.ToString(); // giving each textbox a different id  00-99 
            Panel1.Controls.Add(tb); 
        }
        Literal lc = new Literal();
        lc.Text = "<br />";
        Panel1.Controls.Add(lc);
    }
}

protected void btnShow_Click(object sender, EventArgs e)
{
    foreach (Control control in Panel1.Controls)
    {
        var textBox = control as TextBox;   
        if (textBox != null)
        {
            if (string.IsNullOrEmpty(textBox.Text))
            {
                textBox.Style["visibility"] = "hidden";
            }
            // textBox.Enabled = false;
            textBox.Text = "";
        }
    } 
}

2 Answers 2

2

The proper way to do this is to wrap these textboxes inside a Repeater or Datalist controls. You can ready about these controls from here. This way when number of rows increase you will not have to change to your loop or hard-coded values.

As for your question to store values for a given Id, you can define row# and col# in your database and sort on row# and col#, this should work.

1
  • I think we are talking about the webforms here, aren't we? DataList model has not changed in years, hence it doesn't make any different
    – bazz
    Jun 24, 2013 at 3:03
1

The easiest way is to make a 2D array (or List) of your TextBoxes. In where you create your TextBoxes:

List<List<TextBox>> textBoxList = new List<List<TextBox>>();
for (int i = 0; i <= 9; i++)
{
    List<TextBox> textBoxRow = new List<TextBox>(); //this could be columns, not sure
    for (int j = 0; j <= 9; j++)
    {
        TextBox tb = new TextBox();
        ....
        textBoxRow.Add(tb);
    }
    ...
    textBoxList.Add(textBoxRow);
}

Now you can read/write to those array entries, such as:

string readValue = textBoxList[2][5].Text;
textBoxList[1][7].Text = "asdf";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.