I'm using a Gridview with some text boxes and a drop down box in it. When the user clicks on a row (text box or DDL), I want to pop up a Javascript alert that tells them what the row number is. I can get an event to fire when a user clicks on one of the text boxes, but I can't tell them which row it is inside the alert because I can't seem to figure out how to put a C# variable into a Javascript alert.
Here's what I've tried:
public void gv_instruments_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtBox1 = (TextBox)e.Row.FindControl("txt_partNumbers");
if (txtBox1 != null)
{
txtBox1.Attributes.Add("onclick", "javascript:alert('Message')"); //works
Int32 selectedRow = e.Row.RowIndex;//get row index number
string message = "You've selected row: " + selectedRow.ToString();
message = "javascript:alert('" + message + "')";
//txtBox1.Attributes.Add("onclick", message); //doesn't work
string title = "title";
//ScriptManager.RegisterStartupScript(Page, Page.GetType(),
title, "alert('" + message + "');", true); //doesn't work
//ScriptManager.RegisterClientScriptBlock(Page,Page.GetType(),
title, "alert('" + message + "');",true); //doesn't work
}
}
}
I've found pages on the internet that use the "javascript:alert('" + message + "')"; construct, but it doesn't work (or at least I can't get it to work). I've been careful with the double quotes & single quotes and I can see what looks like a valid message in the debugger (EG: javascript:alert('You've selected row: 0'), I also thought the apostrophe in "you've" might have been the problem, so I removed that & replaced it with "you have", but that doesn't work either. The only construct that I can get to work is this:
txtBox1.Attributes.Add("onclick", "javascript:alert('Message')");
What am I missing?
