Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing textbox control text in gridview cell

Status
Not open for further replies.

Pbjmam

Technical User
Jan 13, 2003
23
0
0
US
I am trying to create a fully editable gridview. the gridview is bound when a item is selected from a dropdown list.


protected void bindgridview()
{
PlayerStatEntryTableAdapters.DataTable1TableAdapter statEntryAdapter = new PlayerStatEntryTableAdapters.DataTable1TableAdapter();
GridView1.DataSource = statEntryAdapter.GetData(int.Parse(ddlGames.SelectedValue.ToString()));
GridView1.DataBind();
}

When the row is created I add a textbox control to each of the editable cells and populate the textbox with the value of the cell

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;


int i = 4;
while (i < 37)
{
System.Web.UI.WebControls.TextBox tb = new System.Web.UI.WebControls.TextBox();
tb.Text = e.Row.Cells.Text.ToString();
tb.MaxLength = 2;
tb.Width = 12;
tb.Font.Size = 7;
tb.ID = "tb";
e.Row.Cells.Controls.Add(tb);

i++;
}
}
}

There is a single save button that i use to update the database. It loops through each row and should update using the values in the textbox control.

The text in the cell is still the value pulled from the database, not the text that the user has entered.
I cant figure out how to access the value in the textbox.


SqlConnection db = new SqlConnection("server=DATABASE;integrated security=true;" + "database=db");
SqlCommand insrtPlayer = new SqlCommand("insrtPlayer", cnPubs);
insrtPlayer.CommandText = "update dr_game_stats set b_pa = " + GridView1.Rows.Cells[4].Text.ToString() + ", b_sing = " + GridView1.Rows.Cells[5].Text.ToString();

db.Open();
insrtPlayer.ExecuteNonQuery();
db.Close();


i++;
}

Any suggestions?

Thanks

d
 
the gridview is not an excel spreadsheet. So right from the start you are trying to hack the control. not that it can't be done, but it's a hack.

1. I would argue too many controls on the screen. I would break up the input into seperate screens or use some form of wizard to page through the input. not to mention they are all textboxes. these are the most dangerous controls because a user (including admins) can type whatever they want. the more keystrokes the more chance of error. if you can limit any of the controls to dropdowns, checkboxes, lists or radio buttons you should.

2. use parameterized queries to prevent a sql injection attack.

3. you are dynamically creating the textbox with each postback. You need to create the controls in the RowCreated event and then Set the value of the textbox in the RowDataBound event.

All your code is very static. why not create this in the markup?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top