supportrp
asked on
Asp.net c# ScriptManager
I'm currently using the code below to get set the values for a hidden field and later executing a statement by reading the values set by the JavaScript code to the hidden field. The button in which executes this code is residing inside an Ajax UpdatePanel. When executed, we get the prompt, are able to select OK or Cancel, but will not execute execute the Page_Load to set the new values to lblStatus.
*What is the cause and what is a good fix?
protected void Page_Load(object sender, EventArgs e)
{
if (hdnFlag.Value == "OK")
{
//'run some code
lblStatus.Text = "You selected OKAY";
}
else if (hdnFlag.Value == "Cancel")
{
//'run some other code
lblStatus.Text = "You selected CANCEL";
}
}
protected void DeleteButton_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder( );
sb.Append(@"<script language='javascript'> ");
sb.Append(@"function getConfirmation() {");
sb.Append(@"var hdn = document.getElementById('h dnFlag'); ");
sb.Append(@"var result = confirm('Are you sure you want to proceed?'); ");
sb.Append(@"if (result == true) { ");
sb.Append(@"hdn.value = 'OK'; } ");
sb.Append(@"else{hdn.value = 'Cancel'; } ");
sb.Append(@"return true; } ");
sb.Append(@"getConfirmatio n(); ");
sb.Append(@"</script>");
//sb.Append(@"<script language='javascript'> confirm('testing'); </script> ");
ScriptManager.RegisterStar tupScript( this, this.GetType(), "JSScript", sb.ToString(), false);
}
*What is the cause and what is a good fix?
protected void Page_Load(object sender, EventArgs e)
{
if (hdnFlag.Value == "OK")
{
//'run some code
lblStatus.Text = "You selected OKAY";
}
else if (hdnFlag.Value == "Cancel")
{
//'run some other code
lblStatus.Text = "You selected CANCEL";
}
}
protected void DeleteButton_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(
sb.Append(@"<script language='javascript'> ");
sb.Append(@"function getConfirmation() {");
sb.Append(@"var hdn = document.getElementById('h
sb.Append(@"var result = confirm('Are you sure you want to proceed?'); ");
sb.Append(@"if (result == true) { ");
sb.Append(@"hdn.value = 'OK'; } ");
sb.Append(@"else{hdn.value
sb.Append(@"return true; } ");
sb.Append(@"getConfirmatio
sb.Append(@"</script>");
//sb.Append(@"<script language='javascript'> confirm('testing'); </script> ");
ScriptManager.RegisterStar
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was able to get the button field by using the following:
foreach (GridViewRow row in Grdview1.Rows)
{
Button btnButton = null;
btnButton = (Button)row.Cells[1].Contr ols[0];
btnButton.Attributes.Add(" onclick", sb.ToString());
}
Later inside the gridview options I added the RowCommand to be "Grdview1_RowCommand1"
protected void Grdview1_RowCommand1(objec t sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddNewTest")
{
if (hdnFlag.Value == "OK")
{
//'run some code
lblStatus.Text = "You selected OKAY";
}
else if (hdnFlag.Value == "Cancel")
{
//'run some other code
lblStatus.Text = "You selected CANCEL";
}
}
}
This is our code residing on our .aspx page
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="tr ue" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblStatus" runat="server"></asp:Label >
<asp:HiddenField ID="hdnFlag" runat="server" />
<asp:GridView ID="Grdview1" runat="server" onrowcommand="Grdview1_Row Command1">
<Columns>
<asp:ButtonField ButtonType="Button" Text="Test" CommandName="AddNewTest" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Everything else was organized as Shaun_Kline posted.
Thank You for the response.
foreach (GridViewRow row in Grdview1.Rows)
{
Button btnButton = null;
btnButton = (Button)row.Cells[1].Contr
btnButton.Attributes.Add("
}
Later inside the gridview options I added the RowCommand to be "Grdview1_RowCommand1"
protected void Grdview1_RowCommand1(objec
{
if (e.CommandName == "AddNewTest")
{
if (hdnFlag.Value == "OK")
{
//'run some code
lblStatus.Text = "You selected OKAY";
}
else if (hdnFlag.Value == "Cancel")
{
//'run some other code
lblStatus.Text = "You selected CANCEL";
}
}
}
This is our code residing on our .aspx page
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="tr
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblStatus" runat="server"></asp:Label
<asp:HiddenField ID="hdnFlag" runat="server" />
<asp:GridView ID="Grdview1" runat="server" onrowcommand="Grdview1_Row
<Columns>
<asp:ButtonField ButtonType="Button" Text="Test" CommandName="AddNewTest" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Everything else was organized as Shaun_Kline posted.
Thank You for the response.
If you use the OnRowDataBound event, you can attach the script to the button when the row is created.
Also, if you are including a delete button for each row in the gridview, you should also consider using the OnRowDeleting event of the gridview.
Beyond that, the only glaring issue that I see in your script is this line:
sb.Append(@"var hdn = document.getElementById('h dnFlag'); ");
Depending on where the hdnFlag field exists in your document structure, when .NET renders the page, it will assign an ID to that field based on any containers to which that field belongs. (Look at the rendered source code.)
You should consider using:
sb.AppendFormat(@"var hdn = document.getElementById('{ 0}');", hdnFlag.ClientID);
Also, if you are including a delete button for each row in the gridview, you should also consider using the OnRowDeleting event of the gridview.
Beyond that, the only glaring issue that I see in your script is this line:
sb.Append(@"var hdn = document.getElementById('h
Depending on where the hdnFlag field exists in your document structure, when .NET renders the page, it will assign an ID to that field based on any containers to which that field belongs. (Look at the rendered source code.)
You should consider using:
sb.AppendFormat(@"var hdn = document.getElementById('{
ASKER
In the previous post, the btnButton was pointing to the wrong cell. This is the correct version:
foreach (GridViewRow row in Grdview1.Rows)
{
Button btnButton = null;
btnButton = (Button)row.Cells[0].Contr ols[0];
btnButton.Attributes.Add(" onclick", sb.ToString());
}
foreach (GridViewRow row in Grdview1.Rows)
{
Button btnButton = null;
btnButton = (Button)row.Cells[0].Contr
btnButton.Attributes.Add("
}
ASKER