Assuming we are continuing from the last post?
We can't really use JavaScript here, since we have a paged GridView, and the JavaScript can only see and look at the current page.
Hence, if other pages of the GridView have checked box, then JavaScript that "just" checks the current GridView page will not suffice here.
Of course, we could change the previous design of our code, and not use a post-back, but when the text (or check box) is change, we could call a webmethod and use some "ajax" for the GridView.
However, since we have all the moving parts wired up and working?
Then best to check for no checked rows WHEN the user hits the save button.
Hence, assume I go to page 3, enter some comments, but forget to check the box. In fact, we kind of don't need the check box, and we could assume that if no comments been entered, then the user not done anything with any row of the GridView. However, for now, the design is that the user is to enter comments, and check the box. So, in theory, we don't need the check box, or in fact if the user enters some comments, then we could automatic check the box. So, consider this for the future.
So, all we have to do is add some code to the save button, and check if now rows have a checked = true.
The code looks like this:
protected void cmdSave_Click(object sender, EventArgs e)
{
// always send current grid page back to rstData
GridToTable();
// make sure user checked at least one hotel
DataRow[] drChecker = rstData.Select("[Checked] = 1");
if (drChecker.Length == 0)
{
string sJava = "alert('you must select at least one row')";
ScriptManager.RegisterStartupScript(Page,
Page.GetType(), "popit",sJava, true);
return;
}
// now send table back to database with updates
string strSQL = "SELECT * from tblHotels";
using (SqlCommand cmdSQL = new SqlCommand(strSQL,
new SqlConnection(Properties.Settings.Default.TEST4)))
{
cmdSQL.Connection.Open();
SqlDataAdapter daupdate = new SqlDataAdapter(cmdSQL);
SqlCommandBuilder cmdBuild = new SqlCommandBuilder(daupdate);
daupdate.Update(rstData);
}
}
And the result is this:
So, you can't really use "just" JavaScript to check if a row been checked, since you have a pager for the GridView, and your JavaScript can only see the current rows on the current page, but with a pager, then you have many pages.
As noted, one could introduce a web method, but for now, since we have a server side save button, then that is the best point in time to test and check for no rows having been checked.