[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2010-04-09
6
Medium Priority
?
319 Views
Last Modified: 2013-11-11
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

0
Comment
Question by:llj45
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:CmdoProg2
ID: 30221552
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
 

Author Comment

by:llj45
ID: 30225471
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
 
LVL 12

Expert Comment

by:CmdoProg2
ID: 30226526
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:llj45
ID: 30244991
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
 
LVL 12

Accepted Solution

by:
CmdoProg2 earned 2000 total points
ID: 30511335
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
 

Author Closing Comment

by:llj45
ID: 31712764
I try to learn something new everyday!
THANKS!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question