how to use Javascript to confirm the user's choice including the value from the choice in a gridview

I am using ASP.NET 3.5's gridview with a button in a template column.
I need to:
1) display a Javascript confirm dialog box which reads:
Click OK, To Credit\n\n" & Web.UI.DataBinder.Eval(e.Row.DataItem, "Name") & " \n\nOne Test! where "Name" is the piece of data from the SQL Server database.  The "Name" data is used on this gridview.
2) After the user clicks OK in this dialog box, the code needs to collect a second piece of already available data named "num" into a session variable for use on another web page
3) do a response.redirect to the next web page possibly from the RowCommand event.

  I have included some code which I have been using as a possible starting point.
Thanks, LLJ45
the gridview code:
<asp:GridView ID="grdResults" runat="server" AutoGenerateColumns="false"
                            HorizontalAlign="Center" BorderWidth="5" CellPadding="4" >
                            <Columns>
                                <asp:BoundField DataField="Name" HeaderText="Name" />
                                <asp:BoundField DataField="Certification Date" HeaderText="Certification Date" />
                                <asp:BoundField DataField="Expiration Date" HeaderText="Expiration Date" />
                                <asp:TemplateField HeaderText="Select For Credit">
                                    <ItemTemplate>
                                        <asp:Button ID="btnSelect" Runat="server" Text="" CommandName="Click" ></asp:Button>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="num" visible="false" />
                            </Columns>
                        </asp:GridView>

RowDataBound code:
        If e.Row.RowType <> DataControlRowType.Header And e.Row.RowType <> DataControlRowType.Footer Then
            Dim btn As Button = CType(e.Row.Cells(0).FindControl("btnSelect"), Button)
            btn.Text = "Select For Credit " & Web.UI.DataBinder.Eval(e.Row.DataItem, "Name")
            btn.OnClientClick = "javascript:return confirm('Click OK, To Credit\n\n" & Web.UI.DataBinder.Eval(e.Row.DataItem, "Name") & " \n\nOne Test!');"
        End If

code to find the value of the item for the session variable:
        Dim index As Integer = grdResults.SelectedIndex
        Session("insp_num") = grdResults.DataKeys(index).Value.ToString()

perhaps this code would run in the SelectedIndexChanged event if this event actually fired; I believe selecting a regular button doesn't fire the SelectedIndexChanged event?

Open in new window

llj45Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CmdoProg2Commented:
Try this ..
I use a slight different update in the rowdatabound event.  But I use the oncommand event since the values I use are not always the datakey but can be bound to any field in the datasource.
the gridview code:
      <asp:GridView ID="grdResults" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center" BorderWidth="5" CellPadding="4">
        <Columns>
          <asp:BoundField DataField="Name" HeaderText="Name" />
          <asp:BoundField DataField="Certification Date" HeaderText="Certification Date" />
          <asp:BoundField DataField="Expiration Date" HeaderText="Expiration Date" />
          <asp:TemplateField HeaderText="Select For Credit">
            <ItemTemplate>
              <asp:Button ID="btnSelect" runat="server" Text="" CommandName="Click" CommandArgument='<%# Bind("Key") %>' OnCommand="btnSelect_Command">
              </asp:Button>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="num" Visible="False" />
        </Columns>
      </asp:GridView>


''codebehind

  Protected Sub grdResults_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdResults.RowDataBound

    'RowDataBound(code)
    If e.Row.RowType = DataControlRowType.DataRow Then
      Dim btn As Button = CType(e.Row.Cells(3).FindControl("btnSelect"), Button)
      'btn.Text = "Select For Credit " & Web.UI.DataBinder.Eval(e.Row.DataItem, "Name")
      'btn.OnClientClick = "javascript:return confirm('Click OK, To Credit\n\n" & Web.UI.DataBinder.Eval(e.Row.DataItem, "Name") & " \n\nOne Test!');"
      ' Since the row is already bound, the name is available in the row
      btn.Text = "Select For Credit " & e.Row.Cells(0).Text
      btn.OnClientClick = "return confirm('Click OK, To Credit\n\n" & e.Row.Cells(0).Text, "Name") & " \n\nOne Test!');"
    End If

  End Sub

  Protected Sub btnSelect_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
    Session("insp_num") = e.CommandArgument.ToString()
  End Sub

Open in new window

0
llj45Author Commented:
Hi, CmdoProg2

THANKS!

I have:

Protected Sub btnSelect_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
        Session("insp_num") = e.CommandArgument.ToString()
    End Sub

and

                        <asp:GridView ID="grdResults" runat="server" AutoGenerateColumns="false"
                            HorizontalAlign="Center" BorderWidth="5" CellPadding="4" >
                            <Columns>
                                <asp:BoundField DataField="Name" HeaderText="Name" />
                                <asp:BoundField DataField="Certification Date" HeaderText="Certification Date" />
                                <asp:BoundField DataField="Expiration Date" HeaderText="Expiration Date" />
                                <asp:TemplateField HeaderText="Select For Credit">
                                    <ItemTemplate>
                                        <asp:Button ID="btnSelect" runat="server" Text="" CommandName="Click"
                                            CommandArgument='<%# Bind("name") %>' OnCommand="btnSelect_Command">
                                        </asp:Button>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="num" visible="false" />
                            </Columns>
                        </asp:GridView>

but
the btnSelect_Command event never fires?  Any ideas?

I made syntax changes.  So, the Javascript confirm dialog box appears with the person's name using your changes.  Thanks.
LLJ45
0
CmdoProg2Commented:
Try Changing   CommandArgument='<%# Bind("name") %>'  to   CommandArgument='<%# Eval("name") %>'

I did not include the server.transfer (or response.redirect if using https) in the btnSelect_Command

Below is a snippet of my operational code that does the similar function:


<asp:GridView ID="gvPayPeriods" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="sdsOpen" Caption="Pay Periods"
      EmptyDataText="No pay periods found!" HorizontalAlign="Center" AllowPaging="True" SkinID="gvAlt">
      <RowStyle Height="50px" />
      <Columns>
        <asp:BoundField DataField="startOfPayPeriod" DataFormatString="{0:d}" HeaderText="Start Of Pay Period" HtmlEncode="False"
          SortExpression="startOfPayPeriod">
          <ItemStyle VerticalAlign="Middle" />
        </asp:BoundField>
        <asp:BoundField DataField="endOfPayPeriod" DataFormatString="{0:d}" HeaderText="End Of Pay Period" HtmlEncode="False" SortExpression="endOfPayPeriod">
          <ItemStyle VerticalAlign="Middle" />
        </asp:BoundField>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:Button ID="btnPayOff" runat="server" CssClass="formButtonBrown" OnClientClick=""
              Text="Sign-Off &gt;&gt;" CommandArgument='<%# Eval("id") %>' CommandName="SignOff" OnCommand="btnPayOff_Command" Visible='<%# Eval("hasAllReported") %>'>
            </asp:Button>
            </ItemTemplate>
          <ItemStyle VerticalAlign="Middle" CssClass="smText" />
        </asp:TemplateField>
      </Columns>
    </asp:GridView>

'' Code behind snippet
'' in RowDataBound event 
      If e.Row.RowType = DataControlRowType.DataRow Then
        Dim btnPay As Button = CType(e.Row.Cells(2).FindControl("btnPayOff"), Button)
...
            btnPay.OnClientClick = String.Format("return confirm('Are you sure you want to sign-off\n{0} - {1} pay period?')", _
                                               e.Row.Cells(0).Text, e.Row.Cells(1).Text)
...
      End If
...
  Protected Sub btnPayOff_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
      sdsSplit.SelectParameters(1).DefaultValue = e.CommandArgument
      gvOvertimes.DataBind()
  End Sub

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

llj45Author Commented:
Hi, CmdoProg2
  I am bad.  The btnSelect_Command does fire.  I had postback blocked from some earlier work.

  Now, how do we get the field on the gridview named "num" into the session variable?  Of course, selected index isn't available in this event.
This need was part of the original question.

Thanks - I really appreciate the help.  I haven't worked with the gridview very much.
LLJ45
0
CmdoProg2Commented:
Hey LLJ45,
 The value you want is stored in the CommandArgument property that you set in the button's property.  For the  "num" value, it would be CommandArgument='<%# Eval("num") %>' .  When the event fires, the value is accessible by e.CommandArgument as an object type.  The main difference between the click and command events is the command event passes the command Name and Argument.  These command names are the edit, update, delete, select, and cancel actions, but can also be custom names.  
  BTW, if you need to pass two values, I sometimes use the CommandName argument for numeric values
CmdoProg2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
llj45Author Commented:
I try to learn something new everyday!
THANKS!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.